Actual long double precision does not agree with std::numeric_limits
Asked Answered
W

1

1

Working on Mac OS X 10.6.2, Intel, with i686-apple-darwin10-g++-4.2.1, and compiling with the -arch x86_64 flag, I just noticed that while...

std::numeric_limits<long double>::max_exponent10 = 4932

...as is expected, when a long double is actually set to a value with exponent greater than 308, it becomes inf--ie in reality it only has 64bit precision instead of 80bit.

Also, sizeof() is showing long doubles to be 16 bytes, which they should be.

Finally, using <limits.h> gives the same results as <limits>.

Does anyone know where the discrepancy might be?

long double x = 1e308, y = 1e309;  
cout << std::numeric_limits<long double>::max_exponent10 << endl;  
cout << x << '\t' << y << endl;  
cout << sizeof(x) << endl;

gives

4932
1e+308 inf
16

Willowwillowy answered 2/4, 2010 at 7:24 Comment(0)
D
5

It's because 1e309 is a literal that gives a double. You need to use a long-double literal 1e309L.

Debouch answered 2/4, 2010 at 7:27 Comment(7)
Awesome, thank you. Unfortunately, I was only using the literals as a test case, after I noticed this happening with the return values of the pow() function, even with very careful typing. Thank god it's not a compiler issue though!Willowwillowy
Yeah, sadly, pow( (long double)2, 1020 ) = 1.12356e+307 and pow( (long double)2, 1030 ) = inf ...any ideas?Willowwillowy
@dmb: There are 3 variants of pow. pow operates on double. You need powl.Debouch
Yes! Thanks again, I really appreciate it. Those trailing L's do everything... why are they not on cplusplus.com??Willowwillowy
@dmb: cplusplus.com isn't very good. But it's not very bad either, and at least it's easy to search.Custard
@dmb: In C++, std::pow is overloaded, you don't need the separate names such as powf and powl. But you do need the header <cmath> that provides the overloads, as <math.h> may not.Prosper
@Willowwillowy cplusplus.com has a lot of incorrect entries. Better use en.cppreference.com/w/c/numeric/math/pow which is maintained by people who understands the standard moreMannos

© 2022 - 2024 — McMap. All rights reserved.