1

With Java's BigDecimal, it's possible to have unlimited precision and specified rounding & scale with a string constructor.

I've seen GNU's GMP, boost's multiprecision, and MPFR, but none seem to have theses characteristics.

Can any of them do this? If so, how? If not, how can this be done?

  • GMP has `explicit mpf_class::mpf_class (const string& s)`, which is defined in gmpxx.h – segfault Mar 06 '14 at 22:14
  • @segfault Thank-you segfault! I am very unsure of how to translate Java's implementation to GMP. Would you mind showing a quick example of how the equivalent Java and C++ code? Thank you so very much in advance! –  Mar 07 '14 at 02:24

1 Answers1

1

GMP is designed to have arbitrary precision with support for C++ using the gmpxx.h header, and corresponding library. If you're building GMP from scratch, use --enable-cxx flag during configure.

To construct an object from std::string, simply use the constructor, for example,

#include <gmpxx.h>

const std::string longNumber = "12345678901234567890";

mpz_class n(longNumber);

More details are available here

segfault
  • 5,759
  • 9
  • 45
  • 66
  • Thank you segfault! Would you mind showing me how to specify rounding and scale as Java defines them? Thank you so very much in advance! –  Mar 07 '14 at 14:53
  • 1
    GMP has no support for rounding and scale. You'll have to implement them yourself. – segfault Mar 07 '14 at 15:03