I'm having an infinite loop when I run this code:
intensity = 0.50
while intensity != 0.65:
print(intensity)
intensity = intensity + 0.05
intensity values should be like 0.50 -> 0.55 -> 0.60 -> 0.65 then it should gets out of the loop. Why the program is doing an infinite loop instead?
Because of floating point imprecision, you may not end up with exactly 0.65
.
To solve this, use <
rather than !=
(a):
intensity = 0.50
while intensity < 0.65:
print(intensity)
intensity = intensity + 0.05
The output of that (showing what I mean by imprecision as well) is:
0.5
0.55
0.6000000000000001
If you kept going, you'd see:
0.6500000000000001
0.7000000000000002
0.7500000000000002
0.8000000000000003
which is why it's never equal to 0.65
.
(a) You may be wondering what the situation would be if the next value was 0.6499...9
rather than 0.650..1
, and you would probably be right to be concerned.
While using <
will fix the latter case, you'll almost certainly get one iteration too many for the former case.
You can fix that by a number of possible different strategies, some of which are:
round(intensity, 2)
.10-4
(for example) - something like if abs(intensity - 0.65) < 0.0001
.intensity
to 50
, adding 5
, comparing to 65
, and printing round(intensity / 100, 2)
.The Python documentation has an interesting article that you can read to further understand these limitations.
while True:
if intensity>0.65:
break
print(intensity)
intensity+=0.05
This is due to the way Python (and some other languages like C) handle floating-point provisions. See this or this. In general, you should avoid floating point loop counters.
If you still wanted to use one, you can round it off:
intensity = 0.50
while round(intensity,2) != 0.65:
print(round(intensity,2))
intensity = intensity + 0.05
Look at your output:
0.5
0.55
0.6000000000000001
0.6500000000000001
0.7000000000000002
0.05 cannot be exactly represented with a terminating binary float number.
See is floating point math broken?
If you want to check whether the value is close, then simply set your desired tolerance:
while abs(intensity - 0.65) < my_tolerance:
Better yet, use the built-in function, setting either relative or absolute tolerance:
isclose(intensity, 0.65, rel_tol=1e-9, abs_tol=0.0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With