Double with a maximum accuracy of 40 digits

Asked

Viewed 331 times

1

I need my program to return a high number of digits in the house up to 40 digits.

I saw that guy DOUBLE has a range of 1.7 X 10 -308 to 1.7X10 308, so I must think that it meets, even too, my needs.

But when this simple program is started:

int main()
{
  double p = 1234567890123456789012345678901234567890.0;

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

  return 0;
}

See also on Ideone

The return obtained will be: 1234567890123456780000000000000000

How to get around the problem in question?

  • 1

    You have already asked this question and it has already been answered: http://answall.com/questions/38138/divis%C3%A3o-matem%C3%A1tica-precisa Or not?

1 answer

4


This accuracy cannot be achieved with double.

Numbers in a floating 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 order of magnitude values influence very little in the result of operations, being discarded without major losses.

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

For arbitrary accuracies it is necessary to use a library such as the GMP (GNU Multi-precision Library). It is capable of manipulating numbers as large as its memory, be they integer, fractional or floating-point numbers.

The website of lib:

The GNU Multiple Precision Arithmetic Library

It supports major platforms (Windows, Mac, Linux, Freebsd).

  • 1

    You’re answering the same question for the same AP for the second time :-)

  • have the following function: code("house") = c26 3 + a26 2 + s26 1 + a26 0 = 35620.

  • for large strings, it is an error. I need these numbers to use in a hash function: x mod tam

  • @Caffé for you to see how I can’t even remember what I had for lunch!

  • 1

    @pcccj I’m afraid to ask, but why do you need to do it? What does this "hash function" do? (i.e. what does it do?) In general, when we want to work with data of arbitrary size, we do not convert this data into number first and do numerical operations on it, this your requirement seems to me a little unusual... Could you please put it in a better context?

  • I am implementing a Hash Table. The code() function will encode the strings. Sorry for the confusion.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.