Barry Schwarz said:
But in the practical sense, that doesn't seem to matter when trying to
use the values. With %30.20f, my system shows
300.02999999999997000000 and that remains unchanged even after
subtracting 2.8E-14 which should have had some effect on that 7.
Multiplying by 100 produces 30002.99999999999600000000.
Both serve to show that you cannot depend on the last non-zero digit
being well-behaved once you exceed DECIMAL_DIG.
It shows that you can't depend on the last non-zero digit of the output
produced by printf. [...]
Apparently the authors of the glibc implementation of printf went
to some effort to have it produce output with excessive precision.
The 300.02999999999997000000 you're seeing is just an artifact
of your particular printf implementation. As you can see from the
(admittedly crude) print_double() function, the actual stored values,
when interpreted precisely as decimal, end in 5.
For the record, the (C11) standard has some words on what you can expect
from printf, under a "Recommended practice" heading:
For e, E, f, F, g, and G conversions, if the number of significant
decimal digits is at most DECIMAL_DIG, then the result should be
correctly rounded.283) If the number of significant decimal digits is
more than DECIMAL_DIG but the source value is exactly representable
with DECIMAL_DIG digits, then the result should be an exact
representation with trailing zeros. Otherwise, the source value is
bounded by two adjacent decimal strings L < U, both having DECIMAL_DIG
significant digits; the value of the resultant decimal string D should
satisfy L <= D <= U, with the extra stipulation that the error should
have a correct sign for the current rounding direction.
(N1570 7.21.6.1p13)
Assuming a DECIMAL_DIG of 17 (reasonable for a system where the widest
float type is an IEEE-754 double), "300.02999999999997" is an output
conforming with that recommended practice: it is between the adjacent
strings with that many significant digits ("300.02999999999997" and
"300.02999999999998"), and rounded in the correct direction. (As is, of
course, the exact value apparently produced by glibc.)
(If __STDC_IEC_559__ is defined, the standard requires "IEC 60559
binary-decimal conversions" (N1570 F.3p1), which might well provide more
strict restrictions, or at least make the above requirements mandatory;
I have not checked.)