Вопрос

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 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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top