Precision issue in python

Shashwat Anand anand.shashwat at gmail.com
Sat Feb 20 13:44:29 EST 2010


@Mark,

The str(...).split('.') here doesn't do a good job of extracting the
> integer part when its argument is >= 1e12, since Python produces a
> result in scientific notation.  I think you're going to get strange
> results when k >= 13.
>

Yeah, you were correct. I tested it for k >= 13, and there were issues i.e.
only one character is printed.
Shashwat-Anands-MacBook-Pro:Desktop l0nwlf$ python CALCULAT.py
3
1000000 12 12
826393051664 000000000000
1000000 13 13
8 0000000000000
100 15 4
9 0000

The logic I tried was : for
alpha = n!
log(alpha) = log(n!)
                = log(n) + log(n-1) + .. log(2) + log(1)
                = e [where e = log(n) + .... + log(1)]

frac_e =  {e} (fractional part of e, like {1.23} = .23)
now last k digits of alpha = 10**frac_e * 10**(k - 1)

As I can see, the method is mathematically sound, and there is precision
issues. Time for code modification.

~l0nwlf
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100221/ac701748/attachment-0001.html>


More information about the Python-list mailing list