[Python-ideas] math.inf and math.nan constants

Steven D'Aprano steve at pearwood.info
Sat Jan 10 11:23:38 CET 2015


On Sat, Jan 10, 2015 at 01:35:36AM -0800, Andrew Barnert wrote:
> On Jan 10, 2015, at 0:33, Steven D'Aprano <steve at pearwood.info> wrote:
> > The payloads SANE used (adjusted to Python syntax where appropriate):
> > 
> > 
> > Name        Payload        Example
> > =============    =======        =========
> > NANSQRT        1        sqrt(-1)
> > NANADD        2        +INF + (-INF)
[...]

> Out of curiosity, where did you find this? Do you still have the Apple 
> Numerics Manual in a box with all the Inside Mac and HIG books, or is 
> it online somewhere and I just couldn't find it?

I have the Numerics Manual on my book shelf, not quite in arms reach but 
close.

I came across a PDF version on Apple's website a long time ago, but lost 
it, and it no longer appears to be available.

> >> If you wanted the all-zeros NaN, that was a separate function.
> > 
> > I don't think there is an all-zeroes NAN for single and double format. I 
> > believe they would be +INF or -INF depending on the sign bit.
> 
> According to the 2008 standard, the quiet bit is part of the bits that 
> distinguish nan from inf, and that bit is set for quiet; therefore, a 
> qnan with payload 0 exists, and is not an inf.

Ah, well if you count the quiet bit as separate from the payload, that 
makes sense. I'm used to thinking of the flag that distinguishes quiet 
from signalling as part of the payload.

The Apple Numerics Manual gives the following three fields for 64-bit 
doubles:

Bit 1: sign bit s
Bits 2-12: biased exponent e
Bit2 13-64: fraction f

0 < e < 2047, any f: normalised
e = 0, f!= 0: denormalised
e = 0, f = 0: zero
e = 2047, f = 0: infinity
e = 2047, f != 0: NAN

so I'm used to thinking of the whole 52 bits of f as the payload. For 
the longest time, I assumed that the sign bit distinguished signalling 
from quiet NANs, which would leave all 52 bits of f for payload, rather 
than the most significant bit of f.

As it stands now, there are two of each NAN, one with the sign bit set 
and one without, and the standard doesn't specify any meaning to that.


> Pre-2008, that was already the way most platforms worked, but not all. 
> I remember some code I had to deal with that had "ifdef MIPS 
> 1s-complement the payload" all over the place (in fact, too all over 
> the place; when reading a float out of storage it would flip the 
> payload twice, giving you -inf where you should have has nan, which 
> was the bug I has to fix that forced me to learn all this stuff in the 
> first place…). From what I remember, the original spec didn't even 
> require that the quiet/signaling bit has to count as part of the bits 
> that separate nan from inf, allowed platforms to reserve any bits for 
> internal use as long as 9 contiguous bits were available for library 
> payloads, and all kinds of other craziness, but I never dealt with 
> anything more deviant than MIPS doing the quiet/signaling bit 
> backward.

Thanks for that bit of history.

Are there now any restrictions or standards for what the NAN payloads 
mean?
 

-- 
Steve


More information about the Python-ideas mailing list