pow() works but sqrt() not!?

Dan Bishop danb_83 at yahoo.com
Thu Jan 4 20:55:19 EST 2007


On Jan 4, 10:00 am, "siggi" <smusnmrNOS... at yahoo.com> wrote:
> Thanks for that, too!
>
> Would be interesting to learn how these different algorithms [for pow] influence the
> precision of the result!?

For an integer (i.e., int or long) x and a nonnegative integer y, x**y
is exact:

>>> 1000001 ** 12
1000012000066000220000495000792000924000792000495000220000066000012000001L
(73 significant digits, correctly ending in "000001")

math.pow uses floating-point arithmetic (even if you pass it integers),
and so has limited precision:

>>> print '%.73f' % math.pow(1000001, 12)
1000012000066000238472777842004463257260700063242258506335663988477526016
(Only the first 17 digits are correct.)

For floats, the ** operator does the same thing math.pow does.




More information about the Python-list mailing list