Search code examples
long-double

Giving input 22222222222222222 for double but the output is 22222222222222224.000000 Why?


#include <stdio.h>
int main()
{
    printf("Enter the number:");
    double num;
    scanf("%lf",&num);
    printf("%lf\n",num);
}

Input:22222222222222222
Output:22222222222222224.000000
Why it is giving output not like given.


Solution

  • A long double is typically implemented in the x86 extended precision format. The fraction part of that has 63 bits and has approximately 18 significant digits. Your number is 17 digits long, and what is happening is that the number is too large to precisely store in the 63 bits available.

    If it is always going to be an integer (no decimal portion), then consider using a "long long" to represent this number.