...
Code Block | ||
---|---|---|
| ||
for (float x = 0.1f; x <= 1.0f; x += 0.1f) { System.out.println(x); } |
This Because 0.1f
is rounded to the nearest representable value in the value set of the float
type, the actual quantity added to x
on each iteration is somewhat larger than 0.1
; consequently, the loop executes only nine times , and produces fails to produce the following output:
...
expected output.
Compliant Solution
This compliant solution uses an integer loop counter from which the desired floating-point value is derived.
...
This compliant solution uses an integer loop counter from which the floating-point value is derived. Additionally, it uses a double
so to ensure that the available precision suffices to represent the desired values.
...