1

What i need is a C++ library (preferably C++, C will do as well) that implements any sort of decimal number - be they floating-point or not.

My requirements in terms of precision are rather humble - max of 10 digits after the point, and at least 20 digits before (i.e., {20digits}.{10digits}. The bottleneck in my case is space and efficiency - there will be an enormous amount of decimals (probably in the count of hundreds of millions) that i need to store and do operations on. There's also a need for storing them in files as well, so the ABI should be stable as well; or at least there should be an ability to extract the underlying parts (e.g., mantissa+exponent in case of floating-point).

I guess that implementation using int128 and fixed point at the 10th digit would suit me, though i would rather not implement it myself. However, if i need to, i don't mind doing so, in which case any related libraries that would help me implement arithmetic (and in the future, probably more advanced) operations would also be welcome.

Ive already looked into boost::multiprecision and libmpdec but they both allocate memory. libmpdec can be configured to only use statically allocated decimals, but there is still a problem of mpd_t structure being huge (48 bytes, i think).

1
  • 1
    You could program in COBOL and define PICTURE IS 9(20)V9(10) to pack two decimal digits into each byte. And buy yourself an IBM mainframe to run it on, as it has packed decimal hardware to do the operations in one machine instruction. Some things were actually a lot easier in the good old days. I still miss the power of ADD CORRESPONDING. :-) Nov 9, 2022 at 2:43

1 Answer 1

1

The GNU Multiple Precision Arithmetic Library (GMP) could be worth a shot. Especially since the homepage mentions

There are several categories of functions in GMP:

  1. High-level signed integer arithmetic functions (mpz). There are about 150 arithmetic and logic functions in this category.
  2. High-level rational arithmetic functions (mpq). This category consists of about 35 functions, but all mpz functions can be used too, by applying them to the numerator and denominator separately.
  3. High-level floating-point arithmetic functions (mpf). This is the GMP function category to use if the C type `double' doesn't give enough precision for an application. There are about 70 functions in this category. New projects should strongly consider using the much more complete GMP extension library mpfr instead of mpf.
  4. C++ class based interface to all of the above. (The C functions and types can of course be used directly from C++ too.)
  5. Low-level positive-integer, hard-to-use, very low overhead functions are found in the mpn category. No memory management is performed; the caller must ensure enough space is available for the results. The set of functions is not always regular, nor is the calling interface. These functions accept input arguments in the form of pairs consisting of a pointer to the least significant word, and an integral size telling how many limbs (= words) there are in that argument. The functions in the other categories call mpn for almost all their calculations. Of these functions about 60 are public.
1
  • GMP isn't suitable for small values
    – phuclv
    Jan 26 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.