floating point verification using is?
Steven Schveighoffer
schveiguy at yahoo.com
Fri Dec 18 18:53:24 PST 2009
On Fri, 18 Dec 2009 21:47:22 -0500, bearophile <bearophileHUGS at lycos.com>
wrote:
> Unfortunately the asm produced by ldc shows the call to the array
> equality function still. Fifth try:
>
>
> bool isInitNaN(T)(T x) if (isFloatingPoint!T) {
> union FPData {
> T f;
>
> static if (T.sizeof == uint.sizeof) {
> uint data;
> } else static if (T.sizeof == ulong.sizeof) {
> ulong data;
> } else static if (T.sizeof == 10) {
> ulong data1;
> ushort data2;
> } else static if (T.sizeof == 12) {
> ulong data1;
> uint data2;
> } else static if (T.sizeof == 16) {
> ulong data1;
> ulong data2;
> } else {
> ubyte[T.sizeof] data;
> }
> }
>
> static FPData fnan, fpd;
> fpd.f = x;
>
> static if (T.sizeof == 10 || T.sizeof == 12 || T.sizeof == 16)
> return fnan.data1 == fpd.data1 && fnan.data2 == fpd.data2;
> else
> return fnan.data == fpd.data;
> }
>
> Bye,
> bearophile
Are you still working on this? :) I think this proves my point. The
compiler does not provide an easy way to compare floats bitwise, so this
means convoluted hard-to-write code. When we have two operators that do
equality -- and one of those means bitwise compare in all other contexts
-- I think this is a no-brainer.
-Steve
More information about the Digitalmars-d-learn
mailing list