3-arg float pow()

Gareth McCaughan Gareth.McCaughan at pobox.com
Wed Sep 5 00:41:32 CEST 2001


Tim Peters wrote:

> That WRT Python's builtin pow(x, y, z) on builtin types, z can be non-None
> only if x, y and z are all of integer types, and y >= 0, and z != 0.  From
> current CVS (which already implements this):
> 
> >>> pow(3., 500)
> 3.6360291795869935e+238
> >>> pow(3., 500., 7.)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: 3rd argument to floating pow() must be None

Can I put in a request for "or omitted", or something of
the kind, to go on the end of that error message?

> >>> pow(2, -5, .1)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: integer pow() arg 3 must not be specified when arg 2 is < 0

And for the messages in those two cases to be made more
consistent?

I suggest

    TypeError: 3rd argument to floating pow() must be None or absent
    TypeError: 3rd argument to pow() must be None or absent when arg 2 is < 0

although longer and less cryptic versions might be better:

    TypeError: 3rd argument to pow() must be None or absent
               when other arguments are not integers
    TypeError: 3rd argument to pow() must be None or absent
               when second argument is negative

or, alternatively, regard the fact that one can say pow(x, y, None)
instead of pow(x, y) as an implementation detail (which it is,
sort of) and say

    TypeError: 3-argument pow() is illegal when other args not integers
    TypeError: 3-argument pow() is illegal when second arg is negative

Oh, one other thing. Giving a third argument of 0 should
probably be equivalent to giving a third argument of None
(except that it could reasonably be forbidden for "bad"
values of args 1,2). But then, I think x % 0 == x should
be true for all numbers x, too, and I don't think any
programming language agrees with me...

(Rationale:

  - "x % y" means "the smallest thing that's congruent to x
    modulo y", for some interpretation of "smallest".

  - "x == z mod y" means "y divides x-z", which means "x-z
    is a multiple of y".

  - When y=0, this reduces to "x=z".

  - The smallest -- and indeed the only -- thing that's
    congruent to x mod 0 is therefore x itself.)

-- 
Gareth McCaughan  Gareth.McCaughan at pobox.com
.sig under construc



More information about the Python-list mailing list