i have a float value that is hundreds of digits long (like the first 100 digits of pi - 3) and need a way to operate on it. is there any way to store and operate on the float that has a large number of decimals and maintain much precision with built in libraries? is there anything like python's Decimal module in c++?
-
3With the C++ Standard library - no. – May 29 '11 at 18:04
-
@KevinDTimm - Unless you hand roll one yourself – Ed Heal Aug 09 '16 at 15:33
-
http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic – KevinDTimm May 29 '11 at 18:04
3 Answers
The other answers all point to high precision integer libraries. There are however a few floating point libraries around:
- The High Precision Arithmetic library
- The GNU Multiple Precision Arithmetic Library (GMP) "Arithmetic without limitations"
- The GNU multiple-precision floating-point computations with correct rounding (the GNU MPFR library). There's also a C++ wrapper.
- NTL: A Library for doing Number Theory. Together with NTL::RR you can use this even within boost.
- The LBNL double-double precision, quad-double precision and arbitrary precision software.
... and don't forget the possibility that you can always implement your own solution. (Might not be the most effective or fastest solution, but it's "the" solution if you want to learn something.
No built-in library, but you can do that using Bignum arithmetics :) http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic.
What a Bignum is: an array (vector) of digits. You can easily implement sum/difference....
I've actually asked something simillar here: STL big int class implementation
Unless it is some extra exotic platform, where a float is 100+ bytes long, you will find it hard to archive what you want without a library for big numbers.

- 62,405
- 41
- 173
- 273