Is there a native c++ variable type that's "bigger" than a double?
float is 7
double is 15 (of course depending on the compiler)
Is there anything bigger that's native, or even non-native?
views:
2307answers:
8C++ has long double
, but there is no guarantee that it's any more precise than a plain double
. On an x86 platform, usually double
is 64 bits, and long double
is either 64 or 80 bits (which gives you 19 significant figures, if I remember right).
Your mileage may vary, especially if you're not on x86.
There are also some various bigfloat/bigint libraries around for C++ that allow arbitrary precision math. There's this library on Microsoft Codeplex, but Googling will find you plenty of others.
You can use GNU MP. Its floating-point functions have unlimited mantissa and 32-bit or 64-bit (depending on the native word size) exponent. It also comes with a C++ wrapper.
C++ has long double, but it's still quite limited. For good time try GNU's gmp library. You can set up numbers as big as you like, and it's quite fun and hackishly when you use gmp_add instead of a normal +. I'm sure there's a C++ wrapper somewhere.
A long double typically only uses 10 bytes, but due to alignment may actually take up 12 or 16 (depending on the compiler and options) bytes in a structure.
The 10 byte long double provides a 64-bit mantissa; this is very convenient for when you want to store 64 bit integers in floating point without loss of precision.
long long double only some cpus will allow you to use it though...