1

i believe that double have x2 precision of float .

in my calculator 10/3 is 3.3333333333333333333333333333333

when i do the following code :

#include <stdlib.h>
#include <stdio.h>


void main() {


    double var = (double )10 / (double )3;
    float var2 = (float )10 / (float )3;


    printf("%f %f \n" , var , var2 );

}

i get the same number of digits after 3, :

3.3333333333333335 3.3333332538604736

why do i get the same number of digits? and why the value is different ? and how to do a division for double and float and get all the numbers and digits in c (like in my calculator) ?

  • 2
    You do realize there's an infinite number of digits in 3.33333... If you want them "all" it's going to take a while. – tadman Sep 03 '20 at 20:15
  • Because `10/3` cannot be exactly represented by a floating point variable. The code is not printing `10/3` but the result of trying to store it in a variable with finite storage space. Your calculator is geared to the precision required by its display. – Weather Vane Sep 03 '20 at 20:41
  • @WeatherVane and others, Curious, can you elaborate more on that? Windows calculator displays 31 "3" decimal places. Any idea if it's using some kind of larger/more precise internal data type than `double`? Maybe it has a lookup table of common fraction ==> decimal conversions? I've done zero research. – yano Sep 03 '20 at 21:00
  • 1
    @yano is *must* be better than `double`, which is only useful for about 16 significant digits. If you would like to research, it is now [open source](https://github.com/Microsoft/calculator). – Weather Vane Sep 03 '20 at 21:28
  • Per C 2018 7.21.6.1 7, a conversion with `%f` produces six digits after the decimal point. If you are getting “3.3333333333333335”, then either you did not use `%f` or your C implementation is defective. – Eric Postpischil Sep 04 '20 at 02:46

1 Answers1

2

By default printf will only print a limited number of digits. On my system it's 6 digits. On your system it seems to be 16 digits.

To print more digits do:

    printf("%.60f \n%.60f \n" , var , var2 );

Output:

3.333333333333333481363069950020872056484222412109375000000000 
3.333333253860473632812500000000000000000000000000000000000000 

As you can see neither float nor double can print

3.333333333333333333333333333333333333333333333333333333333333

but - obviously - double is closer than float.

BTW: It's not void main() {. It shall be int main(void) {

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • _It shall be int main(void)_. The latest specs have loosened up on this quite a bit. – ryyker Sep 03 '20 at 20:37
  • AFAIK, when using `printf`, the float uses `%f`, but the double uses `%lf` (long float)... – Myst Sep 03 '20 at 20:41
  • 1
    @Myst Not when printing. – Support Ukraine Sep 03 '20 at 20:44
  • 2
    @Myst you are confusing with `scanf` which is different. A `float` passed to `printf` is promoted to `double`. In `printf` the format spec for `double` is `%f`. – Weather Vane Sep 03 '20 at 20:45
  • Thanks @WeatherVane , you're totally right, I forget that one – Myst Sep 03 '20 at 20:49
  • 2
    The C standard specifies that the precision of a floating point conversion in `printf` defaults to 6, not an implementation-defined value. To get 16 digits of precision, you'd need to specify an explicit precision. – rici Sep 03 '20 at 21:02