abjer / isds2020

Introduction to Social Data Science 2020 - a summer school course abjer.github.io/isds2020
58 stars 92 forks source link

Overflow error: int too large to convert to float #6

Open fxj483 opened 3 years ago

fxj483 commented 3 years ago

Hi everyone, I have come across this error when trying to execute the code for problem 0.3.5 : " int too large to convert to float" The feedback tells that the result I got from calculating the Eulers constant is too large. image I googled online and tried to use Decimal() to convert the result into decimal form with a precision of 28 digits, then it showed the there was a type error as I tried to calculate values with values both in decimal and float form. I added "Decimal()" before the codes which I used for calculating natural_logarithm. It did not work. image image

Does anyone know how to solve this problem? Thank you in advance!

jsr-p commented 3 years ago

Hi @fxj483, so the problem is that you are getting an integer overflow error when running your function eulers_e(). The variable y is shrinking towards zero at a high rate while the variable x**i is exploding towards infinity.
I tried to evaluate your function (screenshot) with x=3 and n=1000 and at iteration i=646 i got a value of x^2=1.66e+308 and a value of y practically equal to 0. In the following line you are then diving a very large number (x^646) with a small floating point number (y) which then makes python throw the error.

The module decimal is not necessary to solve the exercise. You will have to fix your eulers_e() function before proceeding with the natural logarithm exercise.

The thing is that you can evaluate the limit, as written mathematically in the notebook, for a given value of x and n with only 1 line of code :)

fxj483 commented 3 years ago

@jsr-p Thank you for such a detailed explanation! I used factorial() function instead and it works now. The only thing is that I didn't pass the code test of “assert round(answer_033, 2) == 2.69” for problem 0.3.3. The result I got when using (1,50) is close to 2.72. I'm not sure if my code went wrong. This is my code: import math def eulers_e(x,n): n = n + 1 z = 1 if x != 0: for i in range(1,n): z = z+(x**i)/math.factorial(i)

else:
    z = 1
return z

answer_033 = eulers_e(1,50)

jsr-p commented 3 years ago

Technically, your function is correct :) The thing is that in the exercise you are specifically asked to base your function on the limit approximation of e^x. Your code is based on the infinite series approximation of e^x. Those approximations are asymptotically equivalent but for finite n they are not equal, in particular, they are not equal for n=50 :) Thus the assert statement raises an AssertionError.

Take a look at this screenshot. which shows the difference between the two. Don't hesitate to ask if something in the screenshot is unclear :)

fxj483 commented 3 years ago

@jsr-p Thank you very much!! I really appreciate the comparison you made to explain the problem. It's very clear and understandable. Now I am pretty clear about why I got wrong before. Hope you enjoy the summer holiday!