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 wr开发者_JS百科aps a larger value?
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.
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.
If you really need more precision then you will need a multi-precision library. There are free libraries around like GMP (LGPL software)
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;
精彩评论