开发者

Actual long double precision does not agree with std::numeric_limits

开发者 https://www.devze.com 2022-12-26 00:39 出处:网络
Working on Mac OS X 10.6.2, Intel, with i686-apple-darwin10-g++-4.2.1, 开发者_如何学Pythonand compiling with the -arch x86_64 flag, I just noticed that while...

Working on Mac OS X 10.6.2, Intel, with i686-apple-darwin10-g++-4.2.1, 开发者_如何学Pythonand 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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消