32

What is the easiest way to handle huge numbers in C? I need to store values in the Area 1000^900, or in more human readable form 10^2700.

Does anybody know of an easy way to do that? Any help would really be appreciated!

Simson
  • 3,373
  • 2
  • 24
  • 38
Chris
  • 9,209
  • 16
  • 58
  • 74

4 Answers4

38

Use libgmp:

GMP is a free library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating-point numbers. There is no practical limit to the precision except the ones implied by the available memory in the machine GMP runs on...

Since version 6, GMP is distributed under the dual licenses, GNU LGPL v3 and GNU GPL v2...

GMP's main target platforms are Unix-type systems, such as GNU/Linux, Solaris, HP-UX, Mac OS X/Darwin, BSD, AIX, etc. It also is known to work on Windows in both 32-bit and 64-bit mode...

gnat
  • 6,213
  • 108
  • 53
  • 73
kmkaplan
  • 18,655
  • 4
  • 51
  • 65
  • GMP can handle numbers as large as 1000^900? – jww Aug 29 '17 at 17:57
  • 5
    I have not tested but it reads "There is no practical limit to the precision except the ones implied by the available memory". A quick estimate is that 1000⁹⁰⁰<1024⁹⁰⁰ that is it fits in less than 9000 bits. I see no reason why it should break GMP. – kmkaplan Aug 31 '17 at 13:58
  • @jww It can handle it easily, without breaking a sweat. My own, crappy, homebrew multiprecision library (which is an embarrassment, at least by comparison with something properly sophisticated like GMP) computes 1000^900 without difficulty, in less time than I can even measure. It's an 8,970-bit number, which is hardly a memory-buster. (Also computing it isn't as hard as it sounds, because "[binary exponentiation](https://en.wikipedia.org/wiki/Exponentiation_by_squaring)" is super easy to implement, and blazingly efficient.) – Steve Summit Sep 16 '22 at 18:08
12

There are a few libraries to help you do this (arbitrary precision mathematics):

Assuming this isn't work related (ie you're doing it for fun or its a hobby or just an oportunity to learn something), coding up a library for arbitrary precision maths is a relatively interesting project. But if you need to absolutely rely on it and aren't interested in the nuts and bolts just use a library.

cletus
  • 616,129
  • 168
  • 910
  • 942
5

There are a number of libraries for handling huge numbers around. Do you need integer or floating point arithmetic?

You could look at the code built into Python for the task.

You could look at the extensions for Perl for the task.

You could look at the code in OpenSSL for the task.

You could look at the GNU MP (multi-precision) library - as mentioned by kmkaplan.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • What if I'm not supposed to use any external library and is only allowed to use GCC. Now, to actually store `BIGINTs` or perform any calculations on them what do I've to do? – phougatv Feb 21 '16 at 03:23
  • 2
    Read Knuth? I mean, TAOCP — The Art of Computer Programming ([Volume 2, Semi-Numerical Algorithms](http://www.amazon.com/Art-Computer-Programming-Volume-Seminumerical/dp/0201896842); section 4.3 Multiple-Precision Arithmetic applies). Or any similar book covering multi-precision arithmetic. – Jonathan Leffler Feb 21 '16 at 03:45