Prev: Unicode strings
Next: exception in Tkinter on Ubtunu...
From: Jussi Piitulainen on 11 Feb 2010 07:37 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.
From: Tim Chase on 11 Feb 2010 07:47 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? I think this recently showed up on the list and the answer involved the order of operations and precedence of "" vs. "**". To check, try >>> (0.1) ** 0.1 >>> (0.1 ** 0.1) The first one is what the assignmenttovariables gains you, but I think "**" has a higher precedence than the unary"" so it gets performed first. I don't have Py3 on my machine here, and 2.5 rejects the first form: Python 2.5.4 (r254:67916, Nov 19 2009, 19:46:21) [GCC 4.3.4] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> (0.1**0.1) 0.79432823472428149 >>> (0.1)**0.1 Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: negative number cannot be raised to a fractional power But perhaps Py3 changes evaluation, returning an complex number. tkc
From: Daniel Fetchinson on 11 Feb 2010 07:49 On 2/11/10, Terrence Cole <listsink(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? 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.27)] 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
From: Mark Dickinson on 11 Feb 2010 09:21 On Feb 11, 1:38 pm, Duncan Booth <duncan.bo...(a)invalid.invalid> wrote: > Tim Chase <python.l...(a)tim.thechases.com> wrote: > > But perhaps Py3 changes evaluation, returning an complex number. > > Yes, the change is documented athttp://docs.python.org/3.1/reference/expressions.html#thepoweroperator > > If it is in any of the "What's new in Python x.xx" documents or in a PEP > somewhere I haven't spotted it. Not in the 'what's new' documents, as far as I can tell, but this change was introduced as part of implementing PEP 3141. http://www.python.org/dev/peps/pep3141/ Here's an extract from the PEP, describing the 'Complex' abstract base class: class Complex(Number): """Complex defines the operations that work on the builtin complex type. In short, those are: conversion to complex, bool(), .real, .imag, +, , *, /, **, abs(), .conjugate(), ==, and !=. If it is given heterogenous arguments, and doesn't have special knowledge about them, it should fall back to the builtin complex type as described below. """ <snip lots of other abstractmethods here> @abstractmethod def __pow__(self, exponent): """a**b; should promote to float or complex when necessary.""" raise NotImplementedError  Mark
From: Robert Kern on 11 Feb 2010 10:55 On 20100211 06:31 AM, Shashwat Anand wrote: > Do you really believe that 0.1 ** 0.1 is a valid computational problem > ? Can you raise a negative number to a fractional power ? > Output on my console (python 2.6) > > >>> .1 ** .1 > 0.79432823472428149 > >>> a,b = .1,.1 > >>> a**b > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > ValueError: negative number cannot be raised to a fractional power > >>> abs(a**b) > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > ValueError: negative number cannot be raised to a fractional power > > There is a little issue here that '>>> .1 ** .1' should give you error > message. That is it. No, fractional powers of negative numbers are perfectly valid mathematically. The result is a complex number. In Python 3 (what the OP is using), this has been implemented, but not in Python 2.6.  Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth."  Umberto Eco
First

Prev

Next

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