Hi Esteban,

> It is not for printing but for testing. I want to assert that a
> certain calculation gives the expected result.
> And then it fails because of the difference above when it is
> "semantically" correct.

If you want reliable precision tests with IEEE floats, you should use
rounding to powers of 2, not 10. Round to 1/8 or 1/16, but not to 1/10.

The fundamental issue is that 1/10 does not have an exact finite
representation in base 2. No matter how I/O libraries handle the
conversion between base 2 (in the binary representation of IEEE floats)
and base 10 (in the text representation), there will always be bad
surprises because the expectations we have from working in base 10
cannot all be met with an internal representation in base 2. 

So if your goal is not fundamentally related to I/O, forget about I/O
and design your code to work the binary level, in particular for
testing.

However, if your problem specification explicitly requires precision
guarantees in base 10 (which seems to be your case, from a quick glance
at the Wikipedia page you cite), then it's best to avoid binary floats
completely. Rationals (fractions) are one option, scaled integers
another one.

Konrad.

Reply via email to