semantics of ** (unexpected/inconsistent?)

Discussion in 'Python' started by Esmail, Nov 30, 2009.

  1. Esmail

    Esmail Guest

    Ok, this is somewhat unexpected:

    Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
    [GCC 4.3.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.


    >>> -3**2

    -9

    >>> x = -3


    >>> x**2

    9
    >>>


    I would have expected the same result in both cases.

    Initially I would have expected -3**2 to yield 9, but I can accept
    that ** binds tighter than the unary -, but shouldn't the results
    be consistent regardless if I use a literal or a variable?
     
    Esmail, Nov 30, 2009
    #1
    1. Advertising

  2. * Esmail:
    > Ok, this is somewhat unexpected:
    >
    > Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
    > [GCC 4.3.3] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.
    >
    >
    > >>> -3**2

    > -9
    >
    > >>> x = -3

    >
    > >>> x**2

    > 9
    > >>>

    >
    > I would have expected the same result in both cases.
    >
    > Initially I would have expected -3**2 to yield 9, but I can accept
    > that ** binds tighter than the unary -, but shouldn't the results
    > be consistent regardless if I use a literal or a variable?


    It is.

    >>> -3**2

    -9
    >>> x = 3
    >>> -x**2

    -9
    >>>


    :)


    I guess you expect your expression "x**2" to somehow be evaluated as "-3**2".
    But x doesn't contain text, it contains an integer value that presumably (I
    don't know) is represented in the binary number system, so it's evaluated as
    "(-3)**2". If x contained text and was evaluated as such, pure text replacement,
    then you should be able to write 2 x and have that evaluated as "2 -x"...


    Cheers & hth.,

    - Alf
     
    Alf P. Steinbach, Nov 30, 2009
    #2
    1. Advertising

  3. Esmail

    Mel Guest

    Esmail wrote:
    > Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
    > [GCC 4.3.3] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.
    >
    >
    > >>> -3**2

    > -9
    >
    > >>> x = -3

    >
    > >>> x**2

    > 9
    > >>>

    >
    > I would have expected the same result in both cases.
    >
    > Initially I would have expected -3**2 to yield 9, but I can accept
    > that ** binds tighter than the unary -, but shouldn't the results
    > be consistent regardless if I use a literal or a variable?


    When you say ** binds tighter than unary -, you're also saying that -3 isn't
    a literal: it's an expression. Try

    y=3
    -y**2

    Mel.
     
    Mel, Nov 30, 2009
    #3
    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. =?Utf-8?B?RGltaXRyaXMgUGFudGF6b3BvdWxvcw==?=

    it's all about semantics

    =?Utf-8?B?RGltaXRyaXMgUGFudGF6b3BvdWxvcw==?=, Jul 13, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    333
    =?Utf-8?B?RGltaXRyaXMgUGFudGF6b3BvdWxvcw==?=
    Jul 14, 2004
  2. Luigi Donatello Asero

    Semantics extractor

    Luigi Donatello Asero, Feb 14, 2004, in forum: HTML
    Replies:
    5
    Views:
    548
    Eric Bohlman
    Feb 14, 2004
  3. Esmail
    Replies:
    6
    Views:
    283
    inhahe
    Nov 30, 2009
  4. Esmail
    Replies:
    3
    Views:
    305
    Albert van der Horst
    Dec 6, 2009
  5. Jon
    Replies:
    3
    Views:
    222
    Ray at
    Oct 29, 2003
Loading...

Share This Page