[Patches] [ python-Patches-440487 ] int to the negative power -> float

noreply@sourceforge.net noreply@sourceforge.net
Sat, 14 Jul 2001 11:07:47 -0700


Patches item #440487, was opened at 2001-07-11 12:27
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=440487&group_id=5470

Category: core (C code)
Group: None
>Status: Closed
>Resolution: Fixed
Priority: 5
Submitted By: Guido van Rossum (gvanrossum)
>Assigned to: Guido van Rossum (gvanrossum)
Summary: int to the negative power -> float

Initial Comment:
Currently, int to the negative int power raises an
exception.

This was one of the two problems that the VPython users
complained about. (The other was that integer division
returns a truncated integer, but that's another story.
:-)

I propose to implement this in Python 2.2.  Anybody
have a problem with this?


----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-07-12 04:29

Message:
Logged In: YES 
user_id=6380

OK, checked in.  Also updated the docs.

Question: should we do a similar thing to negative_float to
the fractional_power ?  That could return a complex number
rather than raising an exception.  But it breaks the rule
that we don't return complex numbers unless the user
explicitly asks for it (otherwise the cmath module should be
merged with the math module as well).


----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2001-07-11 18:16

Message:
Logged In: YES 
user_id=31435

Marked Accepted and back to Guido.  The comments should 
say "the" instead of "both", since there are 3 arguments.

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-07-11 16:18

Message:
Logged In: YES 
user_id=6380

OK, here's a new patch, for int and long.

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2001-07-11 14:46

Message:
Logged In: YES 
user_id=31435

Back to Guido.

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2001-07-11 13:23

Message:
Logged In: YES 
user_id=31435

I liked the first patch better.  *Looks* like under the new 
patch

pow(2, -3, 1)

will raise a ValueError but

2 ** -3 % 1

will return 0.125.  That's close to inexplicable.

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2001-07-11 13:04

Message:
Logged In: YES 
user_id=31435

Tough luck for you, you mean <wink>.

I considered z != None and didn't care.  They're going to 
be just as surprised by i**j returning a float if j 
accidentally becomes negative.  When making a formerly 
exceptional case "mean something", potential surprise is 
unavoidable, and if in the new world

2 ** -3 % 1

returns

0.125

is will be just as surprising if

pow(2, -3, 1)

does not return 0.125.  The new rules should be consistent 
with *themselves* more than with oddball cases across 
releases.

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-07-11 12:59

Message:
Logged In: YES 
user_id=6380

New patch that only falls back to float when 3rd arg is
None.

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-07-11 12:52

Message:
Logged In: YES 
user_id=6380

Tough luck. :-)

There may be something else wrong with the patch: it
probably shouldn't fall back to float when z is not None.
After all, pow(x, y, z) should compute x**y % z, and I'm
sure that someone using this would be quite surprised to get
a floating point number back if they accidentally let y
become negative!

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2001-07-11 12:46

Message:
Logged In: YES 
user_id=31435

No objection so far as it goes, but surely long_pow() 
should also attempt the same kind of thing then.  Should 
add some test cases and docs too (yes, I'm going to hold 
you to the same stds as contributors <wink>).


----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=440487&group_id=5470