Alastair Houghton <[EMAIL PROTECTED]> wrote:
> On 3 Oct 2006, at 17:47, James Y Knight wrote:
>
> > On Oct 3, 2006, at 8:30 AM, Martin v. Löwis wrote:
> >> As Michael Hudson observed, this is difficult to implement, though:
> >> You can't distinguish between -0.0 and +0.0 easily, yet you should.
> >
> > Of course you can. It's absolutely trivial. The only part that's even
> > *the least bit* sketchy in this is assuming that a double is 64 bits.
> > Practically speaking, that is true on all architectures I know of,
>
> How about doing 1.0 / x, where x is the number you want to test? On
> systems with sane semantics, it should result in an infinity, the
> sign of which should depend on the sign of the zero. While I'm sure
> there are any number of places where it will break, on those
> platforms it seems to me that you're unlikely to care about the
> difference between +0.0 and -0.0 anyway, since it's hard to otherwise
> distinguish them.
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...
We can observe it directly, for example on x86:
>>> import struct
>>> struct.pack('d', -0.0)
'\x00\x00\x00\x00\x00\x00\x00\x80'
>>> struct.pack('d', 0.0)
'\x00\x00\x00\x00\x00\x00\x00\x00'
>>>
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 */
...
}
}
- Josiah
_______________________________________________
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com