Double with maximum precision of 40 digits [duplicate]

1

I need my program to return a high value of digits in the house with a maximum of 40 digits.

I've seen that the type DOUBLE has a range from 1.7 x 10 ^ -308 to 1.7X10 ^ 308 , so I should think that it caters to my needs too much.

But when this simple program is started:

int main()
{
  double p = 1234567890123456789012345678901234567890.0;

  printf("%.0lf", p);

  return 0;
}

See also Ideone

The return you get will be: 1234567890123456780000000000000000

How do you get around the problem in question?

    
asked by anonymous 28.10.2014 / 17:47

1 answer

4

Can not get this precision with double .

Numbers in flow point notation such as double are excellent for representing very small numbers or very large numbers, but not a mixture of the two. For very large numbers, smaller values of magnitude influence very little on the results of operations, being discarded without major losses.

If you want to learn more, check out the IEEE 754 standard, which is the standard that x86 processors implement (I believe that ARM uses the same pattern).

For arbitrary precisions it is necessary to use a library such as GMP (GNU Multi-Precision Library). It is able to manipulate numbers as large as their memory, whether these integers are fractional or floating-point numbers.

The lib site:

The GNU Multiple Precision Arithmetic Library

It supports the major platforms (Windows, Mac, Linux, FreeBSD).

    
28.10.2014 / 17:55