Seal's System: Home page - English - Capriolo - system seals
To write strong floating point code, you need to look into measurement theory, and how to propagate measurement errors throughout your formulas. This also means that you'll have to replace the C type (bit comparison) equality with a "equals within the bounds of error".
47691 sealreplacement
This is because of the way floating-point numbers are represented in JavaScript (and many other programming languages). The result of 0.1 + 0.2 is not exactly 0.3 due to precision limitations with binary floating-point representation. The result is very close to 0.3, but it's not an exact match.
As I'm sure your are aware the errors are dependent on the value and its magnitude. As such, there are a few cases where the errors align such that floating point numbers seem to work for equality, but the general case is that such comparisons in general are faulty, because they fail to account for errors.
I think what happens is, because C uses the round-to-even rounding mode, so after rounding, it happens to be true, is my understanding correct?
47691 sealequivalent
It doesn't take long to realize that while your program is correct, you can't trust the technique because the technique, in general, won't return the right values.
But, the fundamental point remains that the IEEE-754 binary floating-point used by float (single-precision) and double (double-precision) is really fast to calculate and useful for lots of things, but inherently imprecise for some values, and so you get that kind of issue. With float, you get true for 0.1 + 0.2 == 0.3, but you get false for 0.1 + 0.6 == 0.7:
47691 sealfits what vehicle
47691 sealskf
0.1 + 0.2: This equals 0.3, but in floating-point: (0.1 + 0.2) == 0.3 is false. This is because 0.1, 0.2 and 0.3 cannot be precisely represented in base 2 floating-point.
It evaluates 0.1 + 0.2 = 0.3, that's okay, but in the right hand side, the 0.3 doesn't works as you're expecting it to be, as already mentioned, it's declared as double by default.
Generally, all the float rvalues are declared as double (e.g. 0.5, 11.332, 8.9, etc.) Thus, when you write the following statement:
In a Floating point sense, 0.1 + 0.2 == 0.3 is only true if error in representing 0.1 + error in representing 0.2 == error in representing 0.3
If my understanding is correct, then there must be some specified float-point number won't be true in this case, because there is still a small chance that rounding might fail. So that must be some combination as
47691 sealcross reference
I get false for your program, not true as you indicate in your question. (0.3 is a double literal, so my guess is that when you tested this locally, you used a float variable instead of a 0.3 literal.) If you actually use float (== 0.3f instead of == 0.3), you get true as the output because it just so happens that with float, 0.1 + 0.2 == 0.3 is true.
Note that you cannot build a system that auto-handles the error in the program perfectly, because to do so would require an exact storage approach of finite size for any fractional number of possibly infinite repeating digits. As a result, error estimation is typically used, and the result is typically compared within an approximation boundary that is tuned for the values involved.