repr(complex) in Py3.1

Discussion in 'Python' started by Stefan Behnel, Oct 24, 2009.

  1. Hi,

    in Python 3.1.1, I get this:

    Python 3.1.1 (r311:74480, Oct 22 2009, 19:34:26)
    [GCC 4.3.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 2j

    2j
    >>> -2j

    -2j
    >>> -0-2j

    -2j
    >>> (-0-2j)

    -2j
    >>> -(2j)

    (-0-2j)

    The last line differs from what earlier Python versions printed here:

    Python 2.6.2 (r262:71600, Oct 22 2009, 20:58:58)
    [GCC 4.3.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 2j

    2j
    >>> -2j

    -2j
    >>> -0-2j

    -2j
    >>> (-0-2j)

    -2j
    >>> -(2j)

    -2j

    I know at least that the float repr() was modified in Py3.1, but is the
    above behaviour intentional? It certainly breaks doctests, and I don't see
    a good reason for that.

    Stefan
     
    Stefan Behnel, Oct 24, 2009
    #1
    1. Advertising

  2. On Oct 24, 3:26 pm, Stefan Behnel <> wrote:
    > Hi,
    >
    > in Python 3.1.1, I get this:
    >
    >   Python 3.1.1 (r311:74480, Oct 22 2009, 19:34:26)
    >   [GCC 4.3.2] on linux2
    >   Type "help", "copyright", "credits" or "license" for more information..
    >   >>> 2j
    >   2j
    >   >>> -2j
    >   -2j
    >   >>> -0-2j
    >   -2j
    >   >>> (-0-2j)
    >   -2j
    >   >>> -(2j)
    >   (-0-2j)
    >
    > The last line differs from what earlier Python versions printed here:

    [...]
    > I know at least that the float repr() was modified in Py3.1, but is the
    > above behaviour intentional? It certainly breaks doctests, and I don't see
    > a good reason for that.


    Yes, it's intentional. The problem with the 2.6 complex repr is that
    it doesn't accurately represent negative zeros, so that if you try
    to eval the result you don't end up with the same complex number:

    Python 2.6.1 (r261:67515, Jul 7 2009, 23:51:51)
    [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> z = -(2j)
    >>> z.real

    -0.0
    >>> w = complex(repr(z))
    >>> w.real

    0.0

    This was part of a set of changes made to ensure that complex(repr(z))
    round-tripping worked correctly, even when z involves negative zeros,
    nans, or infinities. (However, note that eval(repr(z)) doesn't
    round-trip correctly; that's not a problem that's easy to solve.)

    Mark
     
    Mark Dickinson, Oct 24, 2009
    #2
    1. Advertising

  3. > I know at least that the float repr() was modified in Py3.1, but is the
    > above behaviour intentional? It certainly breaks doctests, and I don't see
    > a good reason for that.


    I don't know whether it was intentional, but it looks right to me.
    2j is the complex number +0.0+2.0j (right?). Then, -(2j) is the negated
    value of this, i.e. -0.0-2.0j (*). It seems that the complex type made
    no distinction between +0.0 and -0.0 in the past, but it should (just as
    the float type does).

    Regards,
    Martin

    (*) this is different from -2j, where the sign applies to the imaginary
    part only.
     
    Martin v. Löwis, Oct 24, 2009
    #3
  4. Mark Dickinson, 24.10.2009 16:44:
    > On Oct 24, 3:26 pm, Stefan Behnel wrote:
    >> in Python 3.1.1, I get this:
    >>
    >> Python 3.1.1 (r311:74480, Oct 22 2009, 19:34:26)
    >> [GCC 4.3.2] on linux2
    >> Type "help", "copyright", "credits" or "license" for more information.
    >> >>> -(2j)

    >> (-0-2j)
    >>
    >> The last line differs from what earlier Python versions printed here.
    >> I know at least that the float repr() was modified in Py3.1, but is the
    >> above behaviour intentional? It certainly breaks doctests, and I don't see
    >> a good reason for that.

    >
    > Yes, it's intentional. The problem with the 2.6 complex repr is that
    > it doesn't accurately represent negative zeros, so that if you try
    > to eval the result you don't end up with the same complex number


    Ok, thanks. I guess we'll have to work around that in Cython's doctest
    suite then.

    Stefan
     
    Stefan Behnel, Oct 24, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.amnet.net.au
    Replies:
    1
    Views:
    588
    =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=
    Apr 13, 2004
  2. Stanimir Stamenkov
    Replies:
    2
    Views:
    764
    Stanimir Stamenkov
    Oct 25, 2005
  3. Robert Mark Bram
    Replies:
    0
    Views:
    700
    Robert Mark Bram
    Feb 4, 2007
  4. Raymond Hettinger

    Fate of the repr module in Py3.0

    Raymond Hettinger, Mar 19, 2008, in forum: Python
    Replies:
    1
    Views:
    271
    Colin J. Williams
    Mar 21, 2008
  5. Kottiyath

    How complex is complex?

    Kottiyath, Mar 18, 2009, in forum: Python
    Replies:
    22
    Views:
    778
Loading...

Share This Page