[issue17336] Complex number representation round-trip doesn't work with signed zero values

Eric Wieser report at bugs.python.org
Wed Aug 5 08:03:02 EDT 2020


Eric Wieser <wieser.eric at gmail.com> added the comment:

> BTW I don't want repr() of a complex number to use the complex(..., ...)

A compromise would be to only use this notation if signed zeros are involved.

---

Another option would be to use slightly unusual reprs for these complex numbers, which at least round-trip:

    def check(s, v):
        c = eval(s)
        # use string equality, because it's the easiest way to compare signed zeros
        cs = f"complex({c.real}, {c.imag})"
        vs = f"complex({v.real}, {v.imag})"
        assert vs == cs, f' expected {vs} got {cs}'

    check("-(0+0j)", complex(-0.0, -0.0))
    check("(-0.0-0j)", complex(-0.0, 0.0))  # non-intuitive
    check("-(-0.0-0j)", complex(0.0, -0.0))  # non-intuitive

Which I suppose would extend to complex numbers containing just one signed zero

    check("(-0.0-1j)", complex(-0.0, -1))
    check("-(0.0-1j)", complex(-0.0, 1))
    check("-(1+0j)", complex(-1, -0.0))
    check("-(-1+0j)", complex(1, -0.0))

Only two of these reprs are misleading for users who don't understand what's going on, the rest will just strike users as odd.

----------
nosy: +Eric Wieser

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue17336>
_______________________________________


More information about the Python-bugs-list mailing list