Byron Whitlock
Byron Whitlock

Reputation: 53921

How do I represent huge floating point numbers in Visual C++

double ld = 0.299999999999999990009;

Gets truncated to 0.29999999999999999

How do I get more precision out of the float type?

Is there a class that wraps a larger value?

Upvotes: 3

Views: 1333

Answers (4)

Stefan Dragnev
Stefan Dragnev

Reputation: 14493

The Gnu MP library apparently has support of arbitrary size floating point arithmetic. I'd recommend trying it out as GMP is one of the fastest general purpose arbitrary size arithmetic libraries out there. It's very stable, has comprehensive C interfaces, as well as C++ wrappers and binaries compiled for many compilers.

The GNU MPFR library is based on GMP and adds a massive amount of special functions working with arbitrary precision.

Upvotes: 7

ardiyu07
ardiyu07

Reputation: 1800

double itself is actually big enough (1×10^−37 → 1×10^37) and there is also long double and long float

I think it gets truncated because you did not set the precision of std::cout

#include <iomanip>
float yourfloat;
cout << fixed << setprecision(lengthoftheprecision) << yourfloat;

Upvotes: 0

jdehaan
jdehaan

Reputation: 19938

If you really need more precision then you will need a multi-precision library. There are free libraries around like GMP (LGPL software)

Upvotes: 1

Yippie-Ki-Yay
Yippie-Ki-Yay

Reputation: 22854

There is long double, but in MSVS, for example, it's a synonym for double (platform-specific).

And you may also want to try some high precision math library, e.g. HPA.

Upvotes: 3

Related Questions