-1

I wanted to see the difference in how many digits i get when using float and when using double but i get the same results

#include <stdio.h>

int main()
{
    float x=1.2222222222222222f;
    printf("%f %d", x,sizeof(x)); // This is what it prints out 1.222222 4
    return 0;
}
#include <stdio.h>

int main()
{
    double x=1.2222222222222222;  
    printf("%f %d", x,sizeof(x));  // This is what it prints out 1.222222 8
    return 0;
}

It prints out the same value even tho double is obviously double the size and should save more digits. What am i doing wrong?

Foch29
  • 15
  • 6
  • 2
    Any smaller floating point type is promoted to `double` when passing to a function with variable arguments list like `printf`. You will only see a difference if the values you assign get rounded differently for `float` and `double`. – Gerhardh Mar 03 '22 at 11:40
  • 3
    `"%f"` is equivalent to `"%.6f"` [C11 7.21.6.1](https://port70.net/~nsz/c/c11/n1570.html#7.21.6.1): "f,F: ... If the precision is missing, it is taken as 6 ..." – pmg Mar 03 '22 at 12:31

2 Answers2

3

sizeof returns size_t. To print size_t you need %zu instead of %d

If you want to see the real difference between float and double you need to print more digits using %.NUMBERf

Like:

#include <stdio.h>

int main(void)
{
    float x=1.2222222222222222f;
    printf("%.70f %zu\n", x,sizeof(x)); 
    double y=1.2222222222222222;  
    printf("%.70f %zu\n", y,sizeof(y)); 
    return 0;
}

Output:

1.2222222089767456054687500000000000000000000000000000000000000000000000 4
1.2222222222222220988641083749826066195964813232421875000000000000000000 8
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • we use zu because sizeof return a size_t. The z is needed because size_t is an unsigned that can have different dimension. But what is the size_t dimension dependent on? Also from what i read when i write for example `float x=17.4` x will be a double and to explicitly get a float i should write `float x=17.4f` . But when i printed the two cases i got the same results, as if they were both float?? – Foch29 Mar 03 '22 at 15:41
  • @Foch29 Using an `f` suffix with `float = (some FP constant)` vs. `float = (some FP constant)f` rarely, but sometimes makes a value [difference](https://stackoverflow.com/q/66631288/2410359). – chux - Reinstate Monica Mar 03 '22 at 16:49
0

It prints out the same value even tho double is obviously double the size and should save more digits.

When passing a float as a ... argument in printf(), it is first promoted to a double. "%f" prints that double to a rounded 6 places after the ..

Since the original values do not differ when rounded to 6 places after the decimal point, they appear the same.

What am i doing wrong?

Expecting the default precision of 6 is insufficient to distinguish.


Easiest to see different with "%a".

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

0x1.38e38e38e38e3p+0
0x1.38e38ep+0

or with sufficient decimal places in exponential notation.

printf("%.*e\n", DBL_DECIMAL_DIG - 1, 1.2222222222222222);
printf("%.*e\n", DBL_DECIMAL_DIG - 1, 1.2222222222222222f);

1.2222222222222221e+00
1.2222222089767456e+00
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256