[Python-ideas] math.inf and math.nan constants
Stephen J. Turnbull
stephen at xemacs.org
Mon Jan 12 17:05:41 CET 2015
Chris Barker - NOAA Federal writes:
> > But it's not a singleton, not even in the class of NaNs. It's a named
> > constant, like zero or pi.
>
> Right, it's a particular value of a float -- more that that, it's a
> bunch if values, so even less a singleton than pi, even.
AFAIK, the proposed math.nan is *not* a bunch of values. It's a
particular float value that happens to be a NaN, just as math.pi is a
particular float value that happens not to be a NaN. The use cases
are very different, however. math.pi is used when you want a value
that is a close IEEE 754 float approximation to mathematical pi in
concrete computations, while math.nan is used when you want a value
that "takes you out" of the world of concrete computation and can be
interpreted as an "error value". IEEE 754 provides a huge number of
NaN values but only defines that "error value" role for them (AIUI,
actually four such roles, the combinations of positive vs negative and
signaling vs quiet).
> >> math.isnan(x)
> >
> > In Python we spell [the operator form of] that "isinstance", not "is".
>
> Except that nan is not a type.
True, "NaN" is not a type currently implemented in Python, but it
could be a subtype of float. I imagine getting the Python class
definition right would be a bit fiddly and not at all useful, though.
Compare the "bool" type as a subtype of "int".
> In way, it's like how there are two bit patterns for the value 0.0 in
> floats (-0.0 and 0.0).
As far as "typeness" goes, that analogy is exact. Both "Zero" and
"NaN" are (non-singleton) subtypes of (IEEE 754) float. You could say
that "Pi" is a singleton containing only math.pi, too, but that's not
very useful as a description.
More information about the Python-ideas
mailing list