0

I wish to declare a floating point variable that can store more significant digits than the more common doubles and long doubles, preferably something like an octuple (256 bits), that (I believe) might give about 70 significant digits.

How do I declare such a variable? And will cross-platform compability be an issue (as opposed to fixed-width integers)?

Any help is much appreciated.

Jonasbue
  • 3
  • 4
  • 3
    Yes it will be a problem because no such data-type is specified in the C++ standard. There are "bignum" libraries you can find if you search a little, and which can handle it in a portable way (at least for source, and if serializing to text). – Some programmer dude Sep 04 '17 at 13:51
  • It is extremely unlikely that your compiler offers octuple-precision. It may offer quad-precision with an inconvenient syntax and some of the features missing (emulated in software, https://stackoverflow.com/questions/5451447/quadruple-precision-in-c-gcc ), but it is also possible that it doesn't. If you are willing to use an external library, MPFR is a good choice: http://www.mpfr.org – Pascal Cuoq Sep 04 '17 at 13:51
  • It’s worth reconsidering whether you actually need that much precision: Virtually all computational tasks can be done with *vastly* less precision. Some tasks where you run into problems with arithmetic stability of very small numbers can be better solved by working in log space. And lastly, it might be better to scale your computation to work on integers instead. – Konrad Rudolph Sep 04 '17 at 14:26
  • 1
    You can use the "Quad-Double Arithmetic" method, similarly to "Double-Double Arithmetic" it uses multiple doubles and combines their accuracy to get a higher precision answer. – Kaaf Jun 03 '21 at 01:23

2 Answers2

5

The C++ standard mandates precision up to and including double; and the finer details of that floating point scheme are left to the implementation.

An IEEE754 quadruple precision long double will only give you 36 significant figures. I've never come across a system, at the time of writing, that implements octuple precision.

Your best bet is to use something like the GNU Multiple Precision Arithmetic Library, or, if you really want binary floating point, The GNU Multiple Precision Floating Point Reliable Library.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

While I don't know of any C++ libraries that fully implement a proper IEEE754 octuple precision, I've found a library by the name ttmath which implements a multi-word system, allowing it to deal with much larger numbers.