开发者

Why is Python saying pow only has 2 arguments

Why is python telling me "TypeError: pow expected 2 arguments, got 3" despite it working in IDLE (sometimes it tells me that in IDLE as well)? im simply doing pow(a,b,c). my program is very short and i do not change the definition of 开发者_JAVA百科pow at any time since i need to use it for some exponentiation.

NOTE: This is the pow from __builtin__, not Math


Built-in pow takes two or three arguments. If you do from math import * then it is replaced by math's pow, which takes only two arguments. My recommendation is to do import math, or explicitly list functions you use in import list. Similar issue happens with open vs. os.open.


If you are using math functions a lot and the three parameter version of pow infrequently a way around this in python 2.7 is to import __builtin__ and call __builtin__.pow for the 3 paramete


http://docs.python.org/release/2.6.5/library/functions.html

pow(x, y[, z]) Return x to the power y; if z is present, return x to the power y, modulo z (computed more efficiently than pow(x, y) % z). The two-argument form pow(x, y) is equivalent to using the power operator: x**y.

The arguments must have numeric types. With mixed operand types, the coercion rules for binary arithmetic operators apply. For int and long int operands, the result has the same type as the operands (after coercion) unless the second argument is negative; in that case, all arguments are converted to float and a float result is delivered. For example, 102 returns 100, but 10-2 returns 0.01. (This last feature was added in Python 2.2. In Python 2.1 and before, if both arguments were of integer types and the second argument was negative, an exception was raised.) If the second argument is negative, the third argument must be omitted. If z is present, x and y must be of integer types, and y must be non-negative. (This restriction was added in Python 2.2. In Python 2.1 and before, floating 3-argument pow() returned platform-dependent results depending on floating-point rounding accidents.)

Perhaps you're violating the bold portion?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜