C++中long double的精度是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/476212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:32:08  来源:igfitidea点击:

What is the precision of long double in C++?

c++precisionlong-double

提问by Bill the Lizard

Does anyone know how to find out the precision of long doubleon 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 doubleis represented with 8 bytes on my platform, while long doubleis 12 bytes.

有谁知道如何找出long double特定平台上的精度?我似乎在 17 位十进制数字后失去精度,这与我只使用double. 我希望得到更多,因为double在我的平台上用 8 个字节表示,而用long double12 个字节表示。

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

在你问之前,这是针对欧拉计划的,所以是的,我确实需要超过 17 位数字。:)

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

编辑:感谢您的快速回复。我刚刚确认我只能long double在我的系统上使用 18 位十进制数字。

回答by Johannes Schaub - litb

You can find out with std::numeric_limits:

您可以通过以下方式了解std::numeric_limits

#include <iostream>     // std::cout
#include <limits>       // std::numeric_limits
int main(){
    std::cout << std::numeric_limits<long double>::digits10 << std::endl;
}

回答by user7116

You can use <cfloat>. Specifically:

您可以使用<cfloat>。具体来说:

LDBL_DIG