[ python-Bugs-1025872 ] X to the power of 0 may give wrong answer
SourceForge.net
noreply at sourceforge.net
Fri Sep 10 16:07:58 CEST 2004
Bugs item #1025872, was opened at 2004-09-10 09:53
Message generated for change (Comment added) made by tim_one
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1025872&group_id=5470
Category: Python Interpreter Core
>Group: Not a Bug
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: Nick Coghlan (ncoghlan)
Assigned to: Nobody/Anonymous (nobody)
Summary: X to the power of 0 may give wrong answer
Initial Comment:
According to Python 2.3 and 2.4, -1 ** 0 = -1 ! It
appears to do this for any negative value.
The answer to x ** 0 should be 1, regardless of the
sign of x.
----------------------------------------------------------------------
>Comment By: Tim Peters (tim_one)
Date: 2004-09-10 10:07
Message:
Logged In: YES
user_id=31435
Precedence. From the language ref: "The power operator
binds more tightly than unary operators on its left; it binds
less tightly than unary operators on its right.".
Python doesn't have negative numeric literals. -1 is the
unary minus operator applied to the positive literal 1. So
-1**0
parses as
-(1**0)
and -1 is the correct result. Add parens if you want a
different evaluation order:
>>> (-1)**0
1
>>>
----------------------------------------------------------------------
Comment By: Nick Coghlan (ncoghlan)
Date: 2004-09-10 10:00
Message:
Logged In: YES
user_id=1038590
The plot thickens. . . if I use the operator module, it works:
Python 2.3.4 (#1, Jun 15 2004, 21:38:43)
[GCC 3.4.0 20040613 (Red Hat Linux 3.4.0-5)] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>> -4.3 ** 0
-1.0
>>> from operator import __pow__
>>> __pow__(-1, 0)
1
>>> -1 ** 0
-1
>>>
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1025872&group_id=5470
More information about the Python-bugs-list
mailing list