[Issue 20451] comparing identical floating points does not work on Win32 and FreeBSD32.

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Dec 15 19:16:01 UTC 2019


https://issues.dlang.org/show_bug.cgi?id=20451

jacob <look.at.me.pee.please at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |look.at.me.pee.please at gmail
                   |                            |.com

--- Comment #1 from jacob <look.at.me.pee.please at gmail.com> ---
Comparing floating point numbers with "==" is in itself an error. You shouldn't
be comparing approximation of numbers to see if they are exactly the same.

Anyways this works:

extern(C) export void thethingy()
{
    immutable real x = 46;
    immutable float xf = x;
    immutable double xd = x;
    immutable short neg2 = -2;
    immutable long neg8 = -8;

    writefln!"%.70f"(pow(xd, neg2));
    writefln!"%.70f"(cast(double) (1 / (x * x)));

    assert(pow(xd, neg2) == cast(real) (1 / (x * x))); // cast(real) for same
type.

    // can't just cast(double) here cause pow() returns value on the FPU
    // so the conversion never happens otherwise
    double a = pow(xd, neg2);
    assert(a == cast(double) (1 / (x * x)));
}

You are comparing different types, pow() returns real, and you were comparing
it with a "double".

The reason you get the same numbers when printed is because Windows doesn't
actually support 80-bit floats. Their 'long double' type is just an alias for
double.

You guessed it writefln() under the hood uses Windows' snprintf().

https://github.com/dlang/phobos/blob/v2.089.1/std/format.d#L2660

Which is incapable of printing "real" (aka long double, 80-bit floats).

--


More information about the Digitalmars-d-bugs mailing list