Mathematics in Python are not correct
Erik Max Francis
max at alcyone.com
Thu May 8 21:15:54 EDT 2008
wxPythoner at gmail.com wrote:
> Have a look at this:
>
>>>> -123**0
> -1
>
>
> The result is not correct, because every number (positive or negative)
> raised to the power of 0 is ALWAYS 1 (a positive number 1 that is).
>
> The problem is that Python parses -123**0 as -(123**0), not as
> (-123)**0.
>
> I suggest making the Python parser omit the negative sign if a
> negative number is raised to the power of 0. That way the result will
> always be a positive 1, which is the mathematically correct result.
>
> This is a rare case when the parser is fooled, but it must be fixed in
> order to produce the correct mathematical result.
Others have pointed out why this is not a parse error, as it's parsed as
-(123**0), not (-123)**0, which is apparently what you meant.
Note, however, that even normal mathematical conventions follow this
same rule. If you were to write
2
-x
as part of a mathematical equation, this means (translated to Python)
-(x**2), not (-x)**2.
--
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
I sleep and dream that life is / All beauty
-- Lamya
More information about the Python-list
mailing list