floating point verification using is?

bearophile bearophileHUGS at lycos.com
Fri Dec 18 13:00:09 PST 2009

Steven Schveighoffer:
> That's great, but I'm trying to verify that my array building code  
> correctly appends T.init.  isNaN returns true no matter what the bit  
> representation of nan is.  I want to *specifically* compare bit  
> representations of floating point numbers to ensure the code I'm writing  
> is doing what I think it's doing.

I see :-) Let's try again (but next time please explain the full problem in your first post, and not a slice of it):

import std.c.stdio: printf;

bool isNanInit(T)(T f) if (is(T == double) || is(T == float)) {
    union FPInt {
        T f;
        static if (is(T == float))
            uint u;
        static if (is(T == double))
            ulong u;
    static FPInt fnan, fx;
    fx.f = f;
    return fnan.u == fx.u;

void main() {
    printf("%d\n", isNanInit(2.5));
    float x;
    printf("%d\n", isNanInit(x));
    x = 2.5;
    printf("%d\n", isNanInit(x));


More information about the Digitalmars-d-learn mailing list