我尝试搜索有关 long double 的信息,到目前为止,我了解到编译器对它的实现有所不同。
在 Ubuntu (XUbuntu) Linux 12.10 上使用 GCC 时,我得到了这个:
double PId = acos(-1);
long double PIl = acos(-1);
std::cout.precision(100);
std::cout << "PId " << sizeof(double) << " : " << PId << std::endl;
std::cout << "PIl " << sizeof(long double) << " : " << PIl << std::endl;
输出:
PId 8 : 3.141592653589793115997963468544185161590576171875
PIl 16 : 3.141592653589793115997963468544185161590576171875
有人明白为什么他们输出(几乎)相同的东西吗?
最佳答案
根据reference of acos ,只有当你传递一个 long double
给它时,它才会返回一个 long double
。您还必须像狒狒建议的那样使用 std::acos
。这对我有用:
#include <cmath>
#include <iostream>
int main() {
double PId = acos((double)-1);
long double PIl = std::acos(-1.0l);
std::cout.precision(100);
std::cout << "PId " << sizeof(double) << " : " << PId << std::endl;
std::cout << "PIl " << sizeof(long double) << " : " << PIl << std::endl;
}
输出:
PId 8 : 3.141592653589793115997963468544185161590576171875
PIl 12 : 3.14159265358979323851280895940618620443274267017841339111328125
3.14159265358979323846264338327950288419716939937510582097494459
最后一行不是输出的一部分,但包含此精度的 pi 的正确数字。
关于c++ - 使用 GCC 和 C++11 实现类型 "long double",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13903785/