3

I find that for large integers, math.pow() does not successfully give its integer version.

(I got a buggy Karatsuba multiplication when implemented with math.pow).

For instance:

>>> a_Size=32
>>> pow(10,a_size) * 1024
102400000000000000000000000000000000
>>> math.pow(10,a_size) * 1024
1.024e+35
>>> int(math.pow(10,a_size) * 1024)
102400000000000005494950097298915328

I went with 10 ** a_size with correct results for large integers.

For floats, visit Difference between the built-in pow() and math.pow() for floats, in Python?

Please explain why this discrepancy is seen for math.pow. It is observed only from 10 power of 23 and higher.

smci
  • 32,567
  • 20
  • 113
  • 146
Rock
  • 177
  • 3
  • 11
  • `pow(float(10), 32) * 1024 -> 1.024e+35`, the answer is in the question you linked to, *math.pow() implicitly converts its arguments to float:* – Padraic Cunningham Oct 10 '16 at 14:13
  • `help(math)` "It provides access to the mathematical functions defined by the C standard." – Nizam Mohamed Oct 10 '16 at 14:20
  • Unlike the built-in ** operator, math.pow() converts both its arguments to type float. **Use ** or the built-in pow() function for computing exact integer powers**. [https://docs.python.org/3/library/math.html#math.pow](https://docs.python.org/3/library/math.html#math.pow) – Nizam Mohamed Oct 10 '16 at 14:34

1 Answers1

5

math.pow() always returns a floating-point number, so you are limited by the precision of float (almost always an IEEE 754 double precision number). The built-in pow() on the other hand will use Python's arbitrary precision integer arithmetic when called with integer arguments.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841