[Python-Dev] Caching float(0.0)

Alastair Houghton alastair at alastairs-place.net
Wed Oct 4 10:05:56 CEST 2006

On 4 Oct 2006, at 02:38, Josiah Carlson wrote:

> Alastair Houghton <alastair at alastairs-place.net> wrote:
> There is, of course, the option of examining their representations in
> memory (I described the general technique in another posting on this
> thread).  From what I understand of IEEE 764 FP doubles, -0.0 and +0.0
> have different representations, and if we look at the underlying
> representation (perhaps by a "*((uint64*)(&float_input))"), we can
> easily distinguish all values we want to cache...

Yes, though a trip via memory isn't necessarily cheap, and you're  
also assuming that the machine doesn't use an FP representation with  
multiple +0s or -0s.  Perhaps they should be different anyway though,  
I suppose.

> And as I stated before, we can switch on those values.  Alternatively,
> if we can't switch on the 64 bit values directly...
>     uint32* p = (uint32*)(&double_input)
>     if (!p[0]) { /* p[1] on big-endian platforms */
>         switch p[1] { /* p[0] on big-endian platforms */
>             ...
>         }
>     }

That's worse, IMHO, because it assumes more about the  
representation.  If you're going to look directly at the binary, I  
think all you can reasonably do is a straight binary comparison.  I  
don't think you should poke at the bits without first knowing that  
the platform uses IEEE floating point.

The reason I suggested 1.0/x is that it's one of the few ways (maybe  
the only way?) to distinguish -0.0 and +0.0 using arithmetic, which  
is what people that care about the difference between the two are  
going to care about.

Kind regards,



More information about the Python-Dev mailing list