float_loop_counter
Finds a while loop counted in floats (while t < 1.0 { t += 0.1; }).
Category pedantic, allow by default.
Off by default; the example is shown with the rule enabled.
#import "Basic";
main :: () {
t := 0.0;
while t < 1.0 {
print("%\n", t);
t += 0.1;
}
}warning[float_loop_counter]: `t` counts this loop in floats, so rounding error decides how many times it runs
--> example.jai:6:11
|
6 | while t < 1.0 {
| ^
|
= help: count with an integer and compute the float from it: `for i: 0..n-1 { x := start + i * step; }`#import "Basic";
main :: () {
for i: 0..9 {
print("%\n", i * 0.1);
}
}Adding 0.1 ten times does not give 1.0, so whether the loop runs ten or eleven times depends on rounding error, which grows with the iteration count.