[Python-Dev] Representation of 'nan'
shredwheat at gmail.com
Fri Jun 8 05:31:23 CEST 2007
The repr() for a float of 'inf' or 'nan' is generated as a string (not a
string literal). Perhaps this is only important in how one defines repr().
I've filed a bug, but am not sure if there is a clear solution.
# Repr with a tuple of floats
>>> repr((1.0, 2.0, 3.0))
'(1.0, 2.0, 3.0)'
(1.0, 2.0, 3.0)
# Repr with a tuple of floats, plus nan
>>> repr((1.0, float('nan'), 3.0))
'(1.0, nan, 3.0)'
NameError: name 'nan' is not defined
There are a few alternatives I can think are fairly clean. I think I'd
prefer any of these over the current 'nan' implementation. I don't think it
is worth adding a nan literal into the language. But something could be
changed so that repr of nan meant something.
Best option in my opinion would be adding attributes to float, so that
float.nan, float.inf, and float.ninf are accessable. This could also help
with the odd situations of checking for these out of range values. With that
in place, repr could return 'float.nan' instead of 'nan'. This would make
the repr string evaluatable again. (In contexts where __builtins__ has not
Another option could be for repr to return 'float("nan")' for these, which
would also evaluate correctly. But this doesn't seem a clean use for repr.
Is this worth even changing? It's just an irregularity that has come up and
surprised a few of us developers.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-Dev