0
#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.

1 Answers1

0

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.

  • Could you please explain me about Extended precision in detail ? – kingmohan45 Oct 24 '18 at 15:52
  • Computers generally store floating point numbers in three parts - a sign (1 bit), an exponent, and a fraction. The size of the floating point storage determines how many bits the exponent and fraction have. The key thing to know is that there is a limit to how many digits can be accurately stored in the fraction part. It depends on whether you use a float, a double, a double double, etc. Each occupies a different number of bytes and each has its own limit of precision. Your number has 17 digits, which is too long for any of those types. – Steve Boyko Oct 24 '18 at 16:53
  • You said there will be limit that no. of bits for exponent , fraction. Could you please tell me those limits for float,double and long double ? – kingmohan45 Oct 25 '18 at 02:07
  • float: about 7 significant digits, double: about 16 significant digits, double double: about 18 significant digits. – Steve Boyko Oct 26 '18 at 18:56