Bizarre arithmetic results

Daniel Fetchinson fetchinson at googlemail.com
Thu Feb 11 13:49:37 CET 2010


On 2/11/10, Terrence Cole <list-sink at 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?

Try this and think about operator precedence:

Python 3.1.1 (r311:74480, Dec 13 2009, 16:50:25)
[GCC 4.4.2 20091027 (Red Hat 4.4.2-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> (-0.1)**0.1
(0.7554510437117542+0.2454609236416552j)
>>>

I.e. -0.1**0.1 is not the same as (-0.1)**0.1, because it's first
0.1**0.1 and then a minus sign.

HTH,
Daniel


-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown



More information about the Python-list mailing list