简体   繁体   中英

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?

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, eg 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM