Comparing floating point: == vs is
Jonathan M Davis
jmdavisProg at gmx.com
Sat Jul 16 02:49:39 PDT 2011
On Saturday 16 July 2011 19:23:12 Daniel Murphy wrote:
> "Jonathan M Davis" <jmdavisProg at gmx.com> wrote in message
> news:mailman.1689.1310808023.14074.digitalmars-d-learn at puremagic.com...
>
> > 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
More information about the Digitalmars-d-learn
mailing list