Reputation: 75
I need to calculate a ratio between very large numbers as shown in the code:
import scipy.special
x=2000
n=range(8,x+1)
q=[]
for i in range(0,len(n)):
q.append(((n[i]-6)*26**(n[i]-7)-scipy.special.binom(n[i]//7+n[i]%7,n[i]//7))*(26**n[i])**(-1))
I get errors since numbers are too large:
OverflowError: long int too large to convert to float
how can I handle such large numbers?
Upvotes: 1
Views: 256
Reputation: 53029
You can use scipy.special.comb
with the exact
kwd and replace *large^(-1) with /large:
import scipy.special
x=2000
n=range(8,x+1)
q=[]
for i in range(0,len(n)):
q.append(((n[i]-6)*26**(n[i]-7)-scipy.special.comb(n[i]//7+n[i]%7,n[i]//7, exact=True))/(26**n[i]))
print(q[-10:])
# [2.4714229501232675e-07, 2.4726679994684175e-07, 2.473913048813568e-07, 2.4751580981587185e-07, 2.4764031475038685e-07, 2.477648196849019e-07, 2.478893246194169e-07, 2.4801382955393194e-07, 2.4813833448844694e-07, 2.48262839422962e-07]
Division itself doesn't seem to be a problem:
(3**100)/(4**88)
# 5.380792381981753e-06
Note: The above appplies to Python3. On Python2 division between ints defaults to return an int. You can either use __truediv__
q.append(((n[i]-6)*26**(n[i]-7)-scipy.special.comb(n[i]//7+n[i]%7,n[i]//7, exact=True)).__truediv__((26**n[i])))
or use a future import
from __future__ import division
Upvotes: 2