I try to do a simple loop in Python. The loop is supposed to stop if d <= 4. Unfortunately, due to the Floating Point Arithmetic problem, the last calculus gives 4.000000000000001 (instead of 4) and then stop the loop. Is there a simple solution for this kind of problem ?
Input
import matplotlib.pyplot as plt
import numpy as np
A = 4
B = 2
C = 0.1
d = 0
while d <= A:
d += B*C
print(d)
Output
0.2
0.4
0.6000000000000001
0.8
1.0
1.2
1.4
1.5999999999999999
1.7999999999999998
1.9999999999999998
2.1999999999999997
2.4
2.6
2.8000000000000003
3.0000000000000004
3.2000000000000006
3.400000000000001
3.600000000000001
3.800000000000001
4.000000000000001