floating point verification using is?

bearophile bearophileHUGS at lycos.com
Fri Dec 18 13:03:17 PST 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));

More information about the Digitalmars-d-learn mailing list