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

poindexter picture poindexter · Jan 25, 2011 · Viewed 22.8k times · Source

I'm looking for a C++ class that can do decimal floating point arithmetic. Looking through http://speleotrove.com/decimal/ there are links to all sorts of classes that people have written and not maintained. Digging through the decNumber++ stuff led me to some emails showing that GCC will eventually support this functionality. (Formally known as ISO/IEC TR 24733)

I'm looking for something I can use as a drop-in replacement for float or double, something that other people are using in their own projects. Hopefully open source.

Thanks!

EDIT: I should point out that I'm trying to use this to represent prices. So I need EXACT decimals, not HUGE decimals.

Answer

Konrad Rudolph picture Konrad Rudolph · Jan 25, 2011

There exists a huge library called GMP (GNU multiple precision library) which supports this and also has C++ bindings, though to be honest the C++ interface is a bit wonky and outdated.

An example from the documentation, the following creates a float called f with at least 500 bits of precision:

mpf_class f(1.5, 500);