www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Order of float declaration changes NaN throwing behavior

import std.math;

void main()
{
    float foo;
    FloatingPointControl fpc;
    fpc.enableExceptions(FloatingPointControl.allExceptions);

    //~ float foo;  // put it here instead and it throws
    auto x = foo / 0;
}


If you compile and run this, nothing is thrown. If you comment out the
first declaration of foo and replace it with the second one, then it
throws. This looks like a bug to me.

This only seems to affect NaNs. Declaaration order does not seem to
affect other invalid operations, for example:

import std.math;

void main()
{
    float foo = 0.0f;
    FloatingPointControl fpc;
    fpc.enableExceptions(FloatingPointControl.allExceptions);

    auto x = foo / 0;  // throws, as it should
}

And disabling exceptions also works regardless of declaration order:

import std.math;

void main()
{
    float foo = 0.0f;
    FloatingPointControl fpc;
    fpc.disableExceptions(FloatingPointControl.allExceptions);  //
note: disableExceptions

    auto x = foo / 0;  // nothing thrown, good because we've disabled
all exceptions
}

So it only seems to affect NaNs.
Jul 11 2011