Am 18.03.2013 20:47, schrieb Victor Bazarov:
Uh... Wait. To represent 0.1f exactly how many do you need digits?
max(floor(log10(0.1f)),0)+1 is 1, so you need just 1 digit before the
decimal separator, and mumeric_limits<float>::digits is 24, so you need
max(24-log2(0.1),0) *decimal* digits _after_ the decimal separator?
log2(0.1) is -3.3, so you need 20 (or 21) *decimal* digits? Did I get
that right?
First of all, let me mention that I should have said "you need up to"
instead of just "you need". Second, 24-(-3.3) is about 27. ;-)
Your example works as follows:
0.1 (the decimal number you start with)
Now, if you write this as binary number you get
0.000110011001100110011001100110... (with a period of 4 bits)
Picking the closest IEEE-754 single float value basically means limiting
the number of consecutive significant bits to 24 with a leading one
that's not explicitly stored for a normalized number:
0.000110011001100110011001101 (LSB rounded up)
^^^^^^^^^^^^^^^^^^^^^^^^
(24 significant bits)
This is the number a float-type variable will actually store.
Now, since 2 is a factor of 10 we are able to express this number in
decimal _exactly_ :
0.100000001490116119384765625
That's a chunk of 27 consecutive decimal digits that starts and ends
with something other than zero. So, 27 is in fact the lowest possible
precision value to pass to the std::setprecision manipulator in order to
see _this_number_.
Of course, if you just want to see 0.1 being printed then
numeric_limits<float>::digits10 is what you would want to use for the
printing precision. digits10 for float on my machine is 6:
0.10000000149...
^^^^^^
6 digits
But this thread is not about that. This thread is about printing the
_exact_ value of a floating point number in decimal. Why? I guess it's
just for educational purposes ... to make students understand that
floating point numbers are typically just approximations and cannot
exactly represent a number like 0.1. It's not uncommon for students to
confuse the different kinds of roundings that happen during the
conversions. OFten students think that writing 0.1 will yield a value of
type double that exactly represents one tenth. In other cases I noticed
students were convinced that the number printed on screen in decimal
represents the exact value the floating point variable stores. We know
that in both it's a false assumption.
Cheers!
SG