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: Bruno Haible
Subject: Re: support for bitwise comparison of floats
Date: Sun, 25 Mar 2007 17:57:52 +0200
User-agent: KMail/1.5.4

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. 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 :-)).

More generally, for some operations such as isnanl(), one needs to know
the precise bit positions of the sign, exponent and mantissa in the binary
representation. But when it can be avoided, I prefer to look at the binary
representation as a black box, knowing only its size.

Bruno





reply via email to

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