jeaq
jeaq

Reputation: 43

Why double and %f don't want to print 10 decimals?

I am learning c programming language and am figuring out format specifiers, but it seems as if double and %f are not working corectly.

Here is my code

#include <stdio.h>
int main(void)
{ 
    double a = 15.1234567899876;
    printf("%13.10f", a);
}

In my textbook it's stated that in "%13.10f" 13 stands for total number of digits we want to be printed(including dot) and 10 is number of decimals. So i expected to get 15.1234567899 but didn't.

After running it I get 15.1234567900. It's not just not enough decimals, but decimals are not printed correctly. Variable a has 8 after 7 and before 9, but printed number does not.

Can someone please tell me where am I wrong.

Thank you. Lp

Upvotes: 1

Views: 135

Answers (1)

Steve Summit
Steve Summit

Reputation: 47915

printf is supposed to round the result to the number of digits you asked for.

  you asked: 15.1234567899876
    you got: 15.1234567900
digit count:    1234567890

So printf is behaving correctly.

You should beware, though, that both types float and double have finite precision. Also their finite precision is as a number of binary bits, not decimal digits. So after about 7 digits for a float, and about 16 digits for a double, you'll start seeing results that can seem quite strange if you don't realize what's going on. You can see this if you start printing more digits:

printf("%18.15f\n", a);

  you asked: 15.1234567899876
    you got: 15.123456789987600

So that's okay. But:

printf("%23.20f\n", a);

  you asked: 15.1234567899876
    you got: 15.12345678998759979095

Here we see that, at the 15th digit, the number actually stored internally begins to differ slightly from the number you asked for. You can read more about this at Is floating point math broken?


Footnote: What was the number actually stored internally? It was the hexadecimal floating-point number 0xf.1f9add3b7744, or expressed in C's %a format, 0x1.e3f35ba76ee88p+3. Converted back to decimal, it's exactly 15.1234567899875997909475699998438358306884765625. All those other renditions (15.1234567900, 15.123456789987600, and 15.12345678998759979095) are rounded to some smaller number of digits. The internal value makes the most sense, perhaps, expressed in binary, where it's 0b1111.0001111110011010110111010011101101110111010001000, with exactly 53 significant bits, of which 52 are explicit and one implicit, per IEEE-754 double precision.

Upvotes: 6

Related Questions