I have this code that throws a math domain error exception:
v = -1.0
for i in range (201):
print acos (v)
v += 0.01
But if I change it to this, it works:
v = -100
for i in range (201):
print acos (v / 100.0)
v += 1
Is this because of rounding?
How to best solve this in Python? Or should I just do it like my last example?
If you do:
you can see that
0.01(as a floating point number with double precision) is bigger than the number0.01you learned at school.So, when you sum it 100 times, the error gets bigger:
And that’s enough to give a math domain error.
What can you do?
round()it to less decimal points:.
13 or 14 may be enough.