0

Possible Duplicate:
Is there a C++ equivalent to Java's BigDecimal?

I recently posted about a C++ Pi approximating program I was making. You can find the code for that here: C++ Pi approximation program but the code itself isn't really relevant. Just know that I need to do arithmetic including powers with the numbers involved.

Now the program works fine, but I was wandering if it were possible to somehow (perhaps using a custom data type) increase the precision above the measly 15 decimal places offered by a double as the program can calculate Pi precise to 15 decimal places in less than one second.

Any help is appreciated.

Community
  • 1
  • 1
user1546083
  • 1,777
  • 3
  • 13
  • 12
  • In Java, there is a BigDecimal. Perhaps there is a C++ equivalent? Edit: http://stackoverflow.com/questions/4798777/is-there-a-c-equivalent-to-javas-bigdecimal – Ryan Amos Aug 20 '12 at 19:50
  • Not built-in, but there are many 3rd party C and or C++ BigNum libraries that can handle decimals. GNU MP being the biggest/best. – Mooing Duck Aug 20 '12 at 19:51
  • 1
    sounds like it would be fun to write one. It would easier if you constrain the precision to after the decimal just using `unsigned char[]` to store everything. I think you'd want to overload the `==`, `+`, `-`, `/`, `*` for example. – MartyE Aug 20 '12 at 19:54
  • Are you looking to implement this yourself? As others have mentioned, the [GNU Multiple Precision Arithmetic Library](http://gmplib.org/) is a free library for arbitrary precision. – Chris Dargis Aug 20 '12 at 19:57
  • BTW, where did you get 15 decimal places? *floating*-point-precision. – Chris Dargis Aug 20 '12 at 20:00
  • I read somewhere that doing this `long double pi=acos(-1.0L)` will not necessarily give you more precision because a long double is only guaranteed to at least the precision of a `double`, but will give you the most your processor is capable of. – ChiefTwoPencils Aug 20 '12 at 20:03
  • @DougRamsey: It's not "floating point precision", it's just "floating point". It *is* accurate to 15 decimal places, though the positional significance of the decimals in your number is variable. – Benjamin Lindley Aug 20 '12 at 20:03
  • @BenjaminLindley: Isn't it the case that the "representable" floating point values are densest in the real number line near zero? Meaning that the "representable" floating point values grow sparser (exponentially?) as the number line moves away from zero? Hence, less *precision* farther from zero? Is the accuracy considered 15 decimal places or more? – Chris Dargis Aug 20 '12 at 20:24
  • @DougRamsey: 1) Yes 2) Yes 3) No. Precision does not refer to the density of the numbers you can represent. With a double, you can distinguish between 1.23456789012345 and 1.23456789012346, that's 15 digits of precision. You can also distinguish between 12345678901234500000 and 123456789012345600000. But, you *cannot* distinguish between 1.2345678901234500 and 1.2345678901234501, nor can you distinguish between 123456789012345600000 and 123456789012345601000. See, in both cases, it's 15 (or 16) digits of precision. – Benjamin Lindley Aug 20 '12 at 20:34
  • @BenjaminLindley: *" Precision does not refer to the density of the numbers you can represent"*. With a greater amount of representable numbers, don't you get more precision? – Chris Dargis Aug 20 '12 at 20:42
  • @DougRamsey: If you increase the bits used for the mantissa, you get more precision. This string of comments is getting way too long, and I know the moderators do not like side discussions in the comments. I'd suggest you read the [wikipedia article on floating point](http://en.wikipedia.org/wiki/Floating_point). It's [very clearly laid out there](http://en.wikipedia.org/wiki/Floating_point#Internal_representation) that a double has about 15.9 decimal digits of precision. If you have further questions, make a post. – Benjamin Lindley Aug 20 '12 at 20:56
  • maybe you will get something interesting here- http://stackoverflow.com/questions/257353/whats-bigger-than-a-double – TheNewOne Aug 21 '12 at 08:55

0 Answers0