Reputation: 509
I have written a simple code to approximate exponential integral function (Ei function). This function has already been implemented in other libraries (like mpmath) but for some reasons, I do not want to use them. In this approximation there are some mathematical implementation (like factorial). My code works fine for relatively small values when the number of required terms for function approximation is less than 100 but for approximation of large numbers (For example 200) I need to increase the number of terms to above 200-300 terms. In these cases I get this error:
OverflowError: int too large to convert to float
I also searched about this error. People have suggested to use Decimal to resolve this issue but I could not get it to work. I appreciate any help to fix this issue. Here is my code:
#This code has been written for approximation of Ei(200) with 150 terms that gives the error
import numpy as np
gamma = 0.5772156649 # gamma is a constant
def Ei(X, k, b): # Ei (X: number , k : number of terms in approximation, b: Approximation of entire function which is part of approximation)
for s in range(1, k + 1): # Factorial
fact = 1
for i in range(1, s + 1):
fact = fact * i
b += (pow(-1., s + 1) * pow(-X, s) / (s * fact))
return gamma + np.log(X) - b
# Compare with Ei function in mpmath
from mpmath import ei
print (ei(200))
B = Ei(200, 150, 0) # Gives OverflowError Error
print (B)
Upvotes: 0
Views: 91