[issue5816] Simplify parsing of complex numbers and make complex('inf') valid.
report at bugs.python.org
Thu Apr 23 10:35:55 CEST 2009
Mark Dickinson <dickinsm at gmail.com> added the comment:
4. also simplify complex printing: no special handling for nans or
infinities. This means that e.g. complex(1, inf) prints as
"1 + infj" instead of "1+inf*j".
This might look ugly, but I think it's better than what was there
before. Complex reprs are now more uniform and easier to parse
by external code. And the expression "1 + inf*j" doesn't give
the right thing anyway, even if you replace j by 1j and inf by
>>> inf = float('inf')
>>> 1 + inf*j
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'j' is not defined
>>> 1 + inf*1j # expect (1+infj), but get nan in real part:
Furthermore, with these simplifications to printing and
parsing, roundtripping now works: the output of repr(z)
is valid input to
5. Print real part if it's negative zero: z =complex(-0.0, 0.0)
now prints as -0 + 0j instead of just 0j, so again roundtripping
works: complex(repr(z)) recovers z exactly.
6. Change PyOS_ascii_strtod to always output a sign when requested,
even for nans. As far as I can tell, the complex formatting is
the only place that's affected by this. Eric?
Added file: http://bugs.python.org/file13746/simplify_complex_parsing_v2.patch
Python tracker <report at bugs.python.org>
More information about the Python-bugs-list