Determining output (printing) of float with %f in C/C++ -
I have gone through the earlier discussions on temporary point numbers but it did not clear my problem , I knew this floating point issue could be normal in every forum but my question is not related to floating point arithmetic or comparison. I'm curious about its representation and output% F.
The question is straight forward: "How to determine the exact output:
float = & lt; some_value & gt; F; printf ("% f \ n", & lt; float_valable & gt;);
lets us consider this code snippet:
Float f = 43.2f, f1 = 23.7f, f2 = 58.89f, f3 = 0.7f; Printf ("f1 =% f \ n", f); printf ("f2 =% f \ n", f1); Printf ("f3 =% f \ n", f2); printf ("f4 =% f \ N", f3);
Output:
f1 = 43.200001 f2 = 23.700001 f3 = 58.889999 F 4 = 0.700000
I know that% f (for double) is a default preci of 6 Sai also I know that the problem Or (in this case) can be fixed using double but I'm curious about the output f2 = 23.700001
and f3 = 58.889999
.
Edit: I know that the floating point number can not be displayed properly, but what is the rule of obtaining the nearest representable value?
IPEE 754 is talking about float, which has 24 binary accuracy. Points: Represent the number in binary (of course) and make the number round for the 24th most important digit. The result will be the nearest floating point.
For example, 23.7
representation in binary
10111.1011001100110011001100110011 ...
after the rounding You will get
10111.1011001100110011010
that is in decimal
23.700000762939453125
After roaming in the sixth decimal place, you will have
23.700001
which is actually your printf
.
Comments
Post a Comment