Thread: digits of precision in floats

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    1

    Unhappy digits of precision in floats

    I read somewhere that "Float values have between 6 and 9 digits of precision" and I guess it's because of the 23 bits available in the mantissa part in IEEE754 format. 2^23-1 = 8,388,607 which means the largest number of correct 9 digits after radix point in a number will be 999,999 which is 6 digits long. My guess might be wrong(I've seen some articles on the internet and they are too complicated), but I'm asking this because when I run this code:

    Code:
    printf("%.20f",3.12345678901234567890);


    I get 16 of the digits right, and also when I run this one:

    Code:
    printf("%.*f",n,atan(1)*4);


    I get 10 of the digits right. Why?

    (I know how the IEEE754 formula (-1)^S * 2^E-127 * 1.M works so if it's needed in your answer you don't need to explain it. Thank you.)

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    The "precision" of floating point types is given by the number of significant bits, not the decimal fractional part of the number.
    When it is said 'single precision' has 6 or 7 digits of precision, this is an approximation: Since single precision has 24 bits of precision, in decimal, we get p=log10(2^24) -> p = 24*log10(2) = 7.22... (7 decimal digits). But, again, this is an approximation. As an example, the value: 9.403954806578300063749892297777965422549324454176 7001720700136502273380756378173828125*10^-38 (2^-123) can be stored EXACTLY in single precision, because it supports values with 24 bits and expoents from -126 to 127, so 2^-123 is 1.000...*2^-123, which is an exact value in single precision:

    Code:
    $ bc
    scale=130
    2^-123
    .0000000000000000000000000000000000000940395480657830006374989229777\
    796542254932445417670017207001365022733807563781738281250000000
    There is a different interpretation of "precision", used by printf(): There the "precision" is the number of digits after the decimal point, like:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main( void )
    {
      float f = powf( 2.0f, -123.0f );
    
      printf( "%.85e\n", f );
    }
    Here printf() will print the value of f with 85 decimal digits after the decimal point... This is different from "floating point" precision (which is always measured in bits).

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Another thing... when you use literals as 3.12345678901234567890 or functions as atan(), you are dealing with double precision (double).

    Literal constants to single precision have 'f' suffix (like 3.1415926f), and there are 'float' versions of math functions to float, like 'atanf()'.

    printf() will always "convert" single precision (float) to 'double' (except when you use strings like "%Lf" to use 'long double's).

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Another word of caution: It is said floats has 6 decimal digits (not "fractional digits", but "overall digits") of precision, it doen't mean all decimal fracional values can be store exactly in floating point... 0.1 (decimal), for example, cannot be exactly stored in ANY floating point precision, since, in binary, it has always in infinite "binary fractional digits" (0.1 (decimal) = 0.000110011001100... (binary) - this pattern 1100 repeats forever).

    So, the majority of "decimal" values are stored as approximations... Here's an example:

    Code:
    #include <stdio.h>
    
    int main( void )
    {
      double d = 0.1;
    
      printf( "%.60f\n", d );
    }
    Compiing and running:
    Code:
    $ cc -o test test.c
    $ ./test
    0.100000000000000005551115123125782702118158340454101562500000
    This is also true for 0.2, 0.3, 0.4, 0.6, 0.7, 0.8 and 0.9 (restriction to 1 "decimal place" here, but true for other values as well).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic c++ variable types, digits of precision etc
    By jackson6612 in forum C++ Programming
    Replies: 3
    Last Post: 04-17-2011, 12:50 AM
  2. Replies: 10
    Last Post: 02-19-2010, 07:50 PM
  3. A doubt about precision in Floats
    By avi2886 in forum C Programming
    Replies: 3
    Last Post: 11-27-2009, 03:05 PM
  4. Precision with floats
    By ramparts in forum C Programming
    Replies: 4
    Last Post: 11-04-2006, 03:31 PM
  5. Set Precision Help !
    By Halo in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2002, 10:24 AM

Tags for this Thread