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

﹥>﹥吖頭↗ 提交于 2019-11-26 22:13:53

问题


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.


回答1:


You can find out with std::numeric_limits:

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



回答2:


You can use <cfloat>. Specifically:

LDBL_DIG


来源:https://stackoverflow.com/questions/476212/what-is-the-precision-of-long-double-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!