Is the maximum supported digits for a double type in c is 15 digits or more

Debojit Acharjee 455 Reputation points
2023-05-29T04:59:37.8566667+00:00

I came to know from the Microsoft's site that a double variable in C can store a floating=point number up to 15 digits only, but in the following program I am able to store 17 digits. Why is that?

#include <stdio.h>

int main()
{
    double a;
    
    a = 12345678901234567;

    printf("%lf", a);

    return 0;
}

Output:

12345678901234568.000000

Ref: https://learn.microsoft.com/en-us/cpp/cpp/data-type-ranges?view=msvc-170

Developer technologies C++
Developer technologies C#
{count} votes

2 answers

Sort by: Most helpful
  1. YujianYao-MSFT 4,296 Reputation points Microsoft External Staff
    2023-05-29T08:12:39.75+00:00

    Hi Debojit Acharjee,

    The significand of the double type is approximately 15 to 17 decimal digits for most platforms. In most cases, a variable of type double can accurately represent 15 to 17 decimal digits. Numbers outside this range may lose precision or be rounded.

    When I defined a 18 digits number, the result lost precision.

    User's image

    When using floating-point numbers, you should choose the appropriate data type according to your specific needs and precision requirements, and avoid using values beyond its representation range for calculations.

    Regarding the double type, this documentation states:

    Microsoft Specific The double type contains 64 bits: 1 for sign, 11 for the exponent, and 52 for the mantissa. Its range is +/-1.7E308 with at least 15 digits of precision.

    You could also refer to this document for the float type.

    Best regards,

    Elya Yao


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-06-02T15:24:08.96+00:00

    A double is stored in base 2 not decimal. It’s stored in 64 bits. The mantissa is 52 bits, or max 179769313486232 in decimal. The exponent is 11 bits or max of 2047 in decimal. The final bit is the sign bit.

    See:

    https://en.wikipedia.org/wiki/Computer_number_format#:~:text=an%2011%2Dbit%20binary%20exponent,gives%20the%20actual%20signed%20value


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.