Bizarre arithmetic results

Discussion in 'Python' started by Terrence Cole, Feb 11, 2010.

  1. Can someone explain to me what python is doing here?

    Python 3.1.1 (r311:74480, Feb 3 2010, 13:36:47)
    [GCC 4.3.4] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> -0.1 ** 0.1

    -0.7943282347242815
    >>> a = -0.1; b = 0.1
    >>> a ** b

    (0.7554510437117542+0.2454609236416552j)
    >>> -abs(a ** b)

    -0.7943282347242815

    Why does the literal version return the signed magnitude and the
    variable version return a complex?

    Cheers,
    Terrence
     
    Terrence Cole, Feb 11, 2010
    #1
    1. Advertising

  2. Terrence Cole

    Peter Otten Guest

    Terrence Cole wrote:

    > Can someone explain to me what python is doing here?
    >
    > Python 3.1.1 (r311:74480, Feb 3 2010, 13:36:47)
    > [GCC 4.3.4] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.
    >>>> -0.1 ** 0.1

    > -0.7943282347242815
    >>>> a = -0.1; b = 0.1
    >>>> a ** b

    > (0.7554510437117542+0.2454609236416552j)
    >>>> -abs(a ** b)

    > -0.7943282347242815
    >
    > Why does the literal version return the signed magnitude and the
    > variable version return a complex?


    Operator precedence.

    >>> -0.1**0.1

    -0.7943282347242815
    >>> (-0.1)**0.1

    (0.7554510437117542+0.2454609236416552j)

    Quoting http://docs.python.org/3.1/reference/expressions.html:

    """
    The power operator binds more tightly than unary operators on its left; it
    binds less tightly than unary operators on its right.
    """

    Peter
     
    Peter Otten, Feb 11, 2010
    #2
    1. Advertising

  3. On Feb 11, 12:44 am, Terrence Cole <list-
    > wrote:
    > Can someone explain to me what python is doing here?


    > >>> -0.1 ** 0.1

    > -0.7943282347242815


    Here you're computing -(0.1 ** 0.1). The exponentiation operator
    binds more strongly than the negation operator.

    > >>> a = -0.1; b = 0.1
    > >>> a ** b

    > (0.7554510437117542+0.2454609236416552j)


    Here you're computing (-0.1) ** 0.1.

    --
    Mark
     
    Mark Dickinson, Feb 11, 2010
    #3
  4. Terrence Cole writes:

    > Can someone explain to me what python is doing here?
    >
    > Python 3.1.1 (r311:74480, Feb 3 2010, 13:36:47)
    > [GCC 4.3.4] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.
    > >>> -0.1 ** 0.1

    > -0.7943282347242815
    > >>> a = -0.1; b = 0.1
    > >>> a ** b

    > (0.7554510437117542+0.2454609236416552j)
    > >>> -abs(a ** b)

    > -0.7943282347242815
    >
    > Why does the literal version return the signed magnitude and the
    > variable version return a complex?


    The minus sign is not part of the literal syntax. Python takes the
    expression as -(0.1 ** 0.1), the binary operator binding tighter than
    the unary.

    Try (-0.1) ** 0.1, and try a = 0.1, then -a ** 0.1.
     
    Jussi Piitulainen, Feb 11, 2010
    #4
  5. On 2010-02-11, Terrence Cole <> wrote:
    > Can someone explain to me what python is doing here?
    >
    > Python 3.1.1 (r311:74480, Feb 3 2010, 13:36:47)
    > [GCC 4.3.4] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.
    >>>> -0.1 ** 0.1

    > -0.7943282347242815
    >>>> a = -0.1; b = 0.1
    >>>> a ** b

    > (0.7554510437117542+0.2454609236416552j)
    >>>> -abs(a ** b)

    > -0.7943282347242815
    >
    > Why does the literal version return the signed magnitude and the
    > variable version return a complex?


    Didn't we just do this one last week?

    --
    Grant Edwards grante Yow! Hello? Enema Bondage?
    at I'm calling because I want
    visi.com to be happy, I guess ...
     
    Grant Edwards, Feb 11, 2010
    #5
  6. Terrence Cole

    Aahz Guest

    In article <hl1j4a$j61$>,
    Grant Edwards <> wrote:
    >
    >Didn't we just do this one last week?


    Let's do the Time Warp again!
    --
    Aahz () <*> http://www.pythoncraft.com/

    "At Resolver we've found it useful to short-circuit any doubt and just
    refer to comments in code as 'lies'. :)"
     
    Aahz, Feb 12, 2010
    #6
  7. In article <>,
    Terrence Cole <> wrote:
    >Can someone explain to me what python is doing here?
    >
    >Python 3.1.1 (r311:74480, Feb 3 2010, 13:36:47)
    >[GCC 4.3.4] on linux2
    >Type "help", "copyright", "credits" or "license" for more information.
    >>>> -0.1 ** 0.1


    Python 4.0
    Warning: misleading blank space, expected:
    - 0.1**0.1

    >-0.7943282347242815
    >>>> a = -0.1; b = 0.1
    >>>> a ** b

    >(0.7554510437117542+0.2454609236416552j)
    >>>> -abs(a ** b)

    >-0.7943282347242815
    >
    >Why does the literal version return the signed magnitude and the
    >variable version return a complex?
    >
    >Cheers,
    >Terrence
    >



    --
    --
    Albert van der Horst, UTRECHT,THE NETHERLANDS
    Economic growth -- being exponential -- ultimately falters.
    albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst
     
    Albert van der Horst, Feb 22, 2010
    #7
  8. On Mon, 22 Feb 2010 18:01:44 +0000, Albert van der Horst wrote:

    > In article <>,
    > Terrence Cole <> wrote:
    >>Can someone explain to me what python is doing here?
    >>
    >>Python 3.1.1 (r311:74480, Feb 3 2010, 13:36:47) [GCC 4.3.4] on linux2
    >>Type "help", "copyright", "credits" or "license" for more information.
    >>>>> -0.1 ** 0.1

    >
    > Python 4.0
    > Warning: misleading blank space, expected:
    > - 0.1**0.1
    >
    >>-0.7943282347242815



    Making spaces significant in that fashion is mind-bogglingly awful. Let's
    look at a language that does this:

    [steve@sylar ~]$ cat ws-example.rb
    def a(x=4)
    x+2
    end

    b = 1
    print (a + b), (a+b), (a+ b), (a +b), "\n"


    [steve@sylar ~]$ ruby ws-example.rb
    7773




    --
    Steven
     
    Steven D'Aprano, Feb 23, 2010
    #8
  9. On Feb 23, 8:11 am, Steven D'Aprano
    <> wrote:
    > Making spaces significant in that fashion is mind-bogglingly awful. Let's
    > look at a language that does this:
    >
    > [steve@sylar ~]$ cat ws-example.rb
    > def a(x=4)
    >     x+2
    > end
    >
    > b = 1
    > print (a + b), (a+b), (a+ b), (a +b), "\n"
    >
    > [steve@sylar ~]$ ruby ws-example.rb
    > 7773


    Hmm. That's pretty nasty, all right. Not that Python can claim to be
    immune to such behaviour:

    >>> 3 .real

    3
    >>> 3. real

    File "<stdin>", line 1
    3. real
    ^
    SyntaxError: invalid syntax


    Though the fact that one of the cases raises an exception (rather than
    silently giving some different behaviour) ameliorates things a bit.

    --
    Mark
     
    Mark Dickinson, Feb 23, 2010
    #9
  10. On Tue, 23 Feb 2010 05:48:09 -0800, Mark Dickinson wrote:

    > On Feb 23, 8:11 am, Steven D'Aprano
    > <> wrote:
    >> Making spaces significant in that fashion is mind-bogglingly awful.
    >> Let's look at a language that does this:
    >>
    >> [steve@sylar ~]$ cat ws-example.rb
    >> def a(x=4)
    >>     x+2
    >> end
    >>
    >> b = 1
    >> print (a + b), (a+b), (a+ b), (a +b), "\n"
    >>
    >> [steve@sylar ~]$ ruby ws-example.rb
    >> 7773

    >
    > Hmm. That's pretty nasty, all right. Not that Python can claim to be
    > immune to such behaviour:
    >
    >>>> 3 .real

    > 3
    >>>> 3. real

    > File "<stdin>", line 1
    > 3. real
    > ^
    > SyntaxError: invalid syntax
    >
    >
    > Though the fact that one of the cases raises an exception (rather than
    > silently giving some different behaviour) ameliorates things a bit.


    It ameliorates it *completely* -- you won't get silent errors in Python
    because you add or delete whitespace around a dot.


    "I find it amusing when novice programmers believe their main job is
    preventing programs from crashing. ... More experienced programmers
    realize that correct code is great, code that crashes could use
    improvement, but incorrect code that doesn't crash is a horrible
    nightmare."

    http://www.pphsg.org/cdsmith/types.html


    The edge case occurs because dot does double-duty as an operator and as
    part of float literals. However, float literals never include whitespace:

    >>> 1.5

    1.5
    >>> 1 . 5

    File "<stdin>", line 1
    1 . 5
    ^
    SyntaxError: invalid syntax

    and likewise for 1. 5 and 1 .5 -- the only way to get a float literal
    with a decimal point is by not including whitespace in it. So there is
    never any ambiguity about floats. You can even do this:

    >>> 1.5.__str__()

    '1.5'


    And since . is an operator outside of float literals, you can do this:

    >>> import sys
    >>> sys . platform

    'linux2'


    although why you'd want to escapes me :)

    This actually is a feature, since it is useful when calling methods on
    int literals. However this is a very rare thing to do.



    --
    Steven
     
    Steven D'Aprano, Feb 24, 2010
    #10
  11. Terrence Cole

    Aahz Guest

    In article <>,
    Steven D'Aprano <> wrote:
    >
    >[steve@sylar ~]$ cat ws-example.rb


    Ahhh, you're a Heroes fan. ;-)
    --
    Aahz () <*> http://www.pythoncraft.com/

    "Many customs in this life persist because they ease friction and promote
    productivity as a result of universal agreement, and whether they are
    precisely the optimal choices is much less important." --Henry Spencer
     
    Aahz, Feb 27, 2010
    #11
    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. Chris

    Bizarre profiler results

    Chris, Aug 1, 2003, in forum: Java
    Replies:
    3
    Views:
    367
    Roedy Green
    Aug 1, 2003
  2. Stuart McGraw

    bizarre id() results

    Stuart McGraw, Dec 15, 2005, in forum: Python
    Replies:
    3
    Views:
    317
    Chris Lambacher
    Dec 15, 2005
  3. joshc
    Replies:
    5
    Views:
    588
    Keith Thompson
    Mar 31, 2005
  4. dizzy
    Replies:
    1
    Views:
    291
    James Kanze
    Sep 10, 2008
  5. Robert Kern

    Re: Bizarre arithmetic results

    Robert Kern, Feb 11, 2010, in forum: Python
    Replies:
    5
    Views:
    296
    Jussi Piitulainen
    Feb 12, 2010
Loading...

Share This Page