'Divide by zero encountered in true_divide but no zero found in denominator
I am running a script that does some calculations and at some point it does
lambda_FSLE = np.log(r)/t_FSLE
with t_FSLE being a 1D array and r being a float. When I run the script that does this operation, I sometimes get a "divide by zero encountered in true_divide" runtime warning. I say sometimes because I tried running the same script with the same data several times (as show in the screenshot) and I do not get the error every time. The array printed in the screenshot is t_FSLE, and as you can see there are no zeros in it, so no zero denominator.
Does any have a clue why I get this warning then?
Here is the relevant part of the script:
data=np.load(path_fsle+name) t_FSLE=data['tau_av'] delta=data['d'] # print(d_FSLE[-1]) npairs=data['npairs'] r=data['r'] # lambda lambda_FSLE=np.log(r)/t_FSLE print('First lambda: %.4f'%lambda_FSLE[0]) print('Average lambda: %.4f'%(sum(lambda_FSLE[:8])/len(lambda_FSLE[:8])))
Solution 1:[1]
So more information would be helpful to solve this problem, but i'm assuming at some point thos .xxxxx numbers are being converted into integers and then the divide is occuring. Try seeing if they are being converted into ints at some point. Look at the functionality of floats here
>>> f=.841123123123123
>>> int(f)
0
This could account for the divide by zero. If you drop your entire script, I could take a second look.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | tomvonheill |
