14,932,748 members
See more:
This is the code. (C)

C++
```float f  = 3.1415926535897932384626433832795;
double d = 3.1415926535897932384626433832795;

printf("float  = %.20f \n", f);
printf("double = %.20f \n", d);```

According to wikipedia article, double has 2x precision than float. But the output I get is this.

```float  = 3.14159274101257320000
double = 3.14159265358979310000```

There isn't a big difference between float and double except the double has rounded the last digit.

How is this possible if float has only a 7 digits precision ?

What I have tried:

C++
```float f  = 3.1415926535897932384626433832795;
double d = 3.1415926535897932384626433832795;

printf("float  = %.20f \n", f);
printf("double = %.20f \n", d);```
Posted
Updated 29-Jul-18 23:23pm

## Solution 4

You have specified that 20 digits after the decimal point should be printed. And so they are. But that does not mean that these digits are correct and meaningful.

You have also noticed that the last four digits are zeroes. That is because a `double` has that precision and the additional digits are set to zero by the `printf()` function.

The reason that both printed values have these 16 digits is sourced by the fact that the `printf()` function only supports `double` values as floating point arguments and passed `float` arguments are converted to `double` behind the scenes. The compiler is processing it as:
`printf("float  = %.20f \n", (double)f);`

The resulting values printed with appropriate resolution are:
```input  = 3.1415926535897932384626433832795
double = 3.14159265358979311
float  = 3.141592741```
As you can see the `float` differs from the input at the 8th digit and the `double` at the 17th digit.
Jochen Arndt 30-Jul-18 6:57am

Why should I?

## Solution 2

How is this possible if float has only a 7 digits precision ?

According to wikipedia article, double has 2x precision than float. But the output I get is this.

float = 3.14159274101257320000
double = 3.14159265358979310000

(It still only has "7 digits of precision").
M­­ar­­­­k 29-Jul-18 12:18pm

Oh I didn't see that. But where did those wrong numbers came from ? Shouldn't they be 00000 after 7th position in float?
Gerry Schmitz 29-Jul-18 16:16pm

They are only "wrong" if you use them :)

You "asked" for 20; the displayed result is "calculated".

Check out the "decimal" type: 28-29 significant digits.

## Solution 3

Quote:
But where did those wrong numbers came from ? Shouldn't they be 00000 after 7th position in float?

That is because the values are stored internally as sub-powers of 2 and the value is not exact.
See: Pi and e In Binary - Exploring Binary[^]

## Solution 1

M­­ar­­­­k 29-Jul-18 11:38am

I have read it and it doesn't answer my question. Even the answer in that question says float has ~7 digits and double has ~16 digits. But in the code given above both float and double turned out to have 16 digits.