On Saturday 16 July 2011 19:23:12 Daniel Murphy wrote:
> "Jonathan M Davis" <[email protected]> wrote in message
> news:[email protected]...
> 
> > Hmm. Good to know. But assuming that two floating point values have the
> > same
> > bit pattern, shouldn't == return true for them? So, if == is failing,
> > then two
> > floating point values aren't identical, correct?
> > 
> > - Jonathan M Davis
> 
> Unless one or both of the values are nans, yes.  If I had to guess I'd say
> you've been swapping the padding bytes too.

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

Reply via email to