question about math module notation

Paul Rubin http
Fri Jul 27 02:29:26 CEST 2007


Dan Bishop <danb_83 at yahoo.com> writes:
> > I was surprised to find that gives an exact (integer, not
> > floating-point) answer.  Still, I think it's better to say 2**64
> > which also works for (e.g.) 2**10000 where math.pow(2,10000)
> > raises an exception.
> 
> It *is* binary floating point.  Powers of 2 are exactly
> representable.  Of course, it doesn't give an exact answer in general.
> 
> >>> int(math.pow(10, 23))
> 99999999999999991611392L

Oh yikes, good point.  math.pow(2,64) is really not appropriate for
what the OP wanted, I'd say.  If you want integer exponentiation, then
write it that way.  Don't do a floating point exponentiation that just
happens to not lose precision for the specific example.

    >>> int(math.pow(3,50)) # wrong
    717897987691852578422784L
    >>> 3**50  # right
    717897987691852588770249L



More information about the Python-list mailing list