views:

4594

answers:

3

Does anyone know how to find out the precision of long double on a specific platform? I appear to be losing precision after 17 decimal digits, which is the same as when I just use double. I would expect to get more, since double is represented with 8 bytes on my platform, while long double is 12 bytes.

Before you ask, this is for Project Euler, so yes I do need more than 17 digits. :)

EDIT: Thanks for the quick replies. I just confirmed that I can only get 18 decimal digits by using long double on my system.

+5  A: 

You can find out with std::numeric_limits:

std::cout << std::numeric_limits<long double>::digits10 << std::endl;
Johannes Schaub - litb
will that tell you the precision, or just the limits?
Paul Tomblin
it tell you the precision. i.e how many digits you can have in the double. min() and max() tell you the limit
Johannes Schaub - litb
More specifically it'll tell you how many you can round into a long double and not lose the digit.
sixlettervariables
yeah, while min() and max() will tell you the limit you can have even if you loose precision. max is 1.18973e+4932 here, while digits10 is 18.
Johannes Schaub - litb
Thanks for the reply. I'm getting the same results, 18 decimal digits. Now I'm wondering why 4 extra bytes of storage are needed to get one more decimal digit?
Bill the Lizard
On x86 it tends to be the 80-bit extended format: 64bits of mantissa, 15bits of exponent, probably going to be around 18 round safe digits. Double is 53bit mantissa or 16 round safe digits.
sixlettervariables
A: 

You can use

sizeof(long double)

to find out how many bytes are used to store the long double.

Also this has nothing to do with platforms but with compilers.

Stefan
I already know how many bytes long double is stored as, but that doesn't tell me what those bytes are used for. Also, "platform" can mean a lot of things, but it typically means hardware + OS + compiler. http://en.wikipedia.org/wiki/Platform_(computing)
Bill the Lizard
You didn't mention that you already know how many bytes the long double is store with. And "platform" usually means OS for most people, so I wanted to clarify this.Nice downvote btw - seems you're angry...
Stefan
Actually, I did mention it at the end of the first paragraph in my question. As I said, "platform" can mean different things to different people, which is why I'm not the one who downvoted you. Why would I be angry?
Bill the Lizard
+1  A: 

You can use <cfloat>. Specifically:

LDBL_DIG
sixlettervariables
Thanks for the link. It's LDBL_DIG that gives the decimal digits. LDBL_MANT_DIG gave me the binary digits.
Bill the Lizard
Touche, thanks for the edit.
sixlettervariables