bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: support for bitwise comparison of floats


From: Eric Blake
Subject: Re: support for bitwise comparison of floats
Date: Sun, 25 Mar 2007 16:11:19 -0600
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.10) Gecko/20070221 Thunderbird/1.5.0.10 Mnenhy/0.7.4.666

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

According to Bruno Haible on 3/25/2007 9:57 AM:
> Eric Blake wrote:
>> Is this any more portable, by avoiding floating point division altogether?
>>
>> /* return true iff the representation of d needs a leading '-' */
>> bool
>> is_negative (long double d)
>> {
>>   if (d == 0)
>>     {
>>       union {
>>         long double d;
>>         long l;
>>       } u;
>>       u.d = d;
>>       u.l |= 1;
>>       return u.d < 0;
>>     }
>>   return d < 0;
>> }
> 
> You are picking a particular bit in a 'long double' representation. If you
> picked any bit different from the sign bit, this code is fine (assuming
> !isnanl(d) is already known). If you picked the sign bit, +0.0 will be
> considered negative too.

Actually, if I picked the sign bit, then both -0 and +0 would be
considered positive, since neither 0 compares less than 0.

> It's an endianness issue and depends on the bit
> storage order in words. You might have picked the wrong bit for m68k...
> It's safer to use   u.l |= 8;   since noone will put the sign bit at
> bit 3 or 28 (except the HP-PA designers perhaps :-)).

Yes, I like your idea of using a bit other than the LSB as a way to ensure
that the sign bit is not hit.  But you also have to pick a bit that does
not fall in the 2-byte padding of the 10-byte x86 long double.

- --
Don't work too hard, make some time for fun as well!

Eric Blake             address@hidden
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGBvOH84KuGfSFAYARAnsVAKDBDe0uwPWfPZ7j8/RYKbQqVsqIfgCdGI+Y
gHxqppJppVeJPVQyLSq0r30=
=vvsY
-----END PGP SIGNATURE-----




reply via email to

[Prev in Thread] Current Thread [Next in Thread]