9

In my quest for looking for a BigInt library, I came across this post: C or C++ BigInt library on Microsoft Windows

The accepted answer mentions the GMP library, but one of the commenters claim that library does not error out very gracefully and would not be good for production code. Has anybody done any long term development with this library? Are there any good alternatives? Thanks in advance.

Community
  • 1
  • 1
Chad Harrison
  • 2,836
  • 4
  • 33
  • 50
  • 1
    There are quite a few more similar questions,such as: http://stackoverflow.com/questions/3191002/are-there-any-solid-large-integer-implementations-in-c/3191064#3191064. One of the answers there mentions a couple of alternatives... – Jerry Coffin Jun 10 '11 at 14:56

2 Answers2

5

The implementation of the Tcl language uses libtommath for its bigint library, and it does indeed appear to be suitable for production use. (It's a C library, not C++, but it's certainly suitable.)

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • 1
    @Vlad: No. Actually, C is _entirely_ suitable for production use. It certainly used to be the case that C was more suitable than C++ due to it being easier to control the dependencies on some platforms, but I don't know if that's still true. (Tcl's implementation has particularly strict requirements for linking as part of supporting some deployment models; most code isn't under such restrictions. It'd take rather more space than I've got to go into the details though, and it's off-topic for this question anyway.) – Donal Fellows Jun 10 '11 at 14:26
  • There is also `tomsfastmath` together with `tommath`. – puchu Feb 26 '21 at 09:51
4

Well, as a large project like GCC uses this for its printf and compile-time calculations (correct me if I'm wrong here), I think it's stable enough. I would think there are ways to limit the out-of-memory errors the commenter worries about, but I haven't used it personally, so I can't help you there. All I'm saying is that it is a proven stable library. It even has a C++ interface library.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • This could very well be a question on it's own, but will keep the question in context to the post. When you say "limit erros" I assume you mean through exception handeling, or would you go as far as modifying the source code (scary)? – Chad Harrison Jun 10 '11 at 14:27
  • No, I would imagine that GMP has functions/defines to limit memory. Editing GMP's source is ludacris. See [here](http://gmplib.org/manual/Memory-Management.html#Memory-Management) for all the info the commenter in linked question seems to have missed. – rubenvb Jun 10 '11 at 14:39
  • 1
    AFAIU GCC uses GMP/MPFR/MPC for constant folding, thus operating in fixed precision mode. I'd imagine that any possible memory issues might crop up if you want to do arbitrary precision arithmetic (e.g. exact rational arithmetic). – janneb Jun 10 '11 at 15:34