This is a separate issue from my example, but since it was raised I will clarify it.
The context is important.
== is a valid operator for floats. It is appropriate when exact equality of the stored floating-point values is the intended comparison.
It was used correctly and as intended in my example comparing the constant to a mathematical expression.
Your example uses == in a different context, where repeated addition accumulates floating-point approximation.
Experienced users would recognize the observed output as the expected result.
Expanding your code example:
The float is stored in binary as an approximation, then converted to decimal for printing.
Processing’s println(f) uses Java’s default float-to-text conversion.
That conversion prints a short rounded decimal form, so the stored float for 0.1 appears as “0.1”.
The second println uses nf(f, 1, 20) to print 20 decimal places.
Printing 20 decimal places expands the decimal display of the stored binary float.
These extra digits show the decimal representation of the stored value, NOT extra precision.
Each += adds a binary floating-point approximation of 0.1 and stores the rounded result back in f.
// The float is stored in binary as an approximation, then converted to decimal for printing.
// Processing's println(f) uses Java's default float-to-text conversion.
// That conversion prints a short rounded decimal form, so the stored float for 0.1 appears as "0.1".
// The second println uses nf(f, 1, 20) to print 20 decimal places.
// Printing 20 decimal places expands the decimal display of the stored binary float.
// These extra digits show the decimal representation of the stored value, NOT extra precision.
// Each += adds a binary floating-point approximation of 0.1 and stores the rounded result back in f.
float f = 0;
for(int cnt=0; cnt < 20; cnt++)
{
f += 0.1;
println(f);
println(nf(f, 1, 20));
println();
if(f == 0.7)
println("match");
}
Output:
0.1
0.10000000149011612000
0.2
0.20000000298023224000
0.3
0.30000001192092896000
0.4
0.40000000596046450000
0.5
0.50000000000000000000
0.6
0.60000002384185790000
0.70000005
0.70000004768371580000
0.8000001
0.80000007152557370000
0.9000001
0.90000009536743160000
1.0000001
1.00000011920928960000
1.1000001
1.10000014305114750000
1.2000002
1.20000016689300540000
1.3000002
1.30000019073486330000
1.4000002
1.40000021457672120000
1.5000002
1.50000023841857900000
1.6000003
1.60000026226043700000
1.7000003
1.70000028610229500000
1.8000003
1.80000030994415280000
1.9000003
1.90000033378601070000
2.0000002
2.00000023841857900000
Additional references here:
Processing float values: decimal output and IEEE 754 bit patterns
Caveat: The expanded decimal output helps display the stored float, but typing that decimal back into code still creates a new float by converting the decimal literal to the nearest representable binary value.
Bonus code
float f=0;
for(int cnt=0; cnt < 20; cnt++)
{
int i = 3; // Try with 1, 2. 3, ...
f += 1.0/(1<<i);
println(f);
println(nf(f, 1, 20));
if(f == 1.75)
println("match");
println();
}
:)