Going past the float size limits?

Hrvoje Niksic hniksic at xemacs.org
Sat Oct 27 01:54:58 CEST 2007


jimmy.musselwhite at gmail.com writes:

> The calculation looks like this
>
> A = 0.35
> T = 0.30
> C = 0.25
> G = 0.10
>
> and then I basically continually multiply those numbers together. I
> need to do it like 200,000+ times but that's nuts. I can't even do it
> 1000 times or the number rounds off to 0.0. I tried taking the inverse
> of these numbers as I go but then it just shoots up to "inf".

>>> import gmpy
>>> A = gmpy.mpf('0.35')
>>> B = gmpy.mpf('0.30')
>>> C = gmpy.mpf('0.25')
>>> D = gmpy.mpf('0.10')
>>> result = gmpy.mpf(1)
>>> for n in xrange(200000):
...   result *= A
...   result *= B
...   result *= C
...   result *= D
...
>>> result
mpf('7.27023409768722186651e-516175')

It's reasonably fast, too.  The above loop took a fraction of a second
to run on an oldish computer.



More information about the Python-list mailing list