Comparing floating point: == vs is

Daniel Murphy yebblies at nospamgmail.com
Sat Jul 16 04:19:27 PDT 2011


"Jonathan M Davis" <jmdavisProg at gmx.com> wrote in message 
news:mailman.1690.1310809801.14074.digitalmars-d-learn at puremagic.com...
> I tried both casting to an integer of the appropriate size and swapping 
> that
> and doing this:
>
> private T swapEndianImpl(T)(T val)
>    if(isFloatingPoint!T)
> {
>    import std.algorithm;
>
>    union Union
>    {
>        Unqual!T        _floating;
>        ubyte[T.sizeof] _array;
>    }
>
>    Union u;
>    u._floating = val;
>    std.algorithm.reverse(u._array[]);
>
>    return u._floating;
> }
>
> I was testing the function by reversing the values twice, and casting 
> seemed
> to fry NaNs, whereas the union/array trick seems to usually result in the
> correct values when I print them (at least for the ones I've looked at) 
> and is
> was returning true for most of them, but == has been failing.
>
> I don't know anything about padding bytes in floating point though, so 
> maybe
> that's part of the problem.
> http://stackoverflow.com/questions/2782725/converting-float-values-from-big-
> endian-to-little-endian/2782742#2782742 seemed to indicate that I could 
> just
> swap the bytes using the union/array trick, but I'm not understanding
> something here and/or something is off with either my implementation or 
> the
> compiler.
>
> - Jonathan M Davis

The padding issue only applies to reals.  On x86 they're 10 bytes with 0 
bytes padding (windows) or 2 bytes padding (linux) or 6 bytes padding (osx).

I don't know why it would be failing for floats or doubles though.  Do you 
have some tests that fail? 




More information about the Digitalmars-d-learn mailing list