December 18, 2009
Hello Steven,

> From my very hazy memory, I think there can even be multiple binary
> representations of nan, but I'm not sure.
> 
> -Steve
> 

yes, IIRC NaNs can have a few bits of payload as well as there being signaling and non-signaling NaNs.


December 18, 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));
}

Bye,
bearophile
December 18, 2009
This may be better, I have not taken a look at the resulting asm yet:

import std.traits: isFloatingPoint;
import std.c.stdio: printf;

bool isInitNan(T)(T f) if (isFloatingPoint!T) {
    union FPInt {
        T f;
        ubyte[T.sizeof] a;
    }
    static FPInt fnan, fx;
    fx.f = f;
    return fnan.a == fx.a;
}

void main() {
    printf("%d\n", isInitNan(2.5));
    float x;
    printf("%d\n", isInitNan(x));
    x = 2.5;
    printf("%d\n", isInitNan(x));
}
1 2
Next ›   Last »