From: Terrence Cole on
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

From: Mark Dickinson on
On Feb 11, 12:44 am, Terrence Cole <list-
s...(a)trainedmonkeystudios.org> 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
From: Andre Engels on
On Thu, Feb 11, 2010 at 1:44 AM, Terrence Cole
<list-sink(a)trainedmonkeystudios.org> 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?

It's an issue of precedence of operators:

-0.1 ** 0.1

is interpreted as

-(0.1 ** 0.1)

and not as

(-0.1) ** 0.1






--
André Engels, andreengels(a)gmail.com
From: Peter Otten on
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
From: Christian Heimes on
Terrence Cole wrote:
>>>> -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 binary power operator has a higher precedence than the unary
negative operator. -0.1 ** 0.1 is equal to -(0.1**0.1)

Christian

 |  Next  |  Last
Pages: 1 2 3 4 5
Prev: Unicode strings
Next: exception in Tkinter on Ubtunu...