c++ - 使用 GCC 和 C++11 实现类型 "long double"

标签 c++ double

我尝试搜索有关 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/

相关文章:

c++ - 在 C++ 中使用 wait() 和 fork() 时出错

C++ 如何退出 while 循环 recvfrom()

c++ - 将 QuaZip 添加到项目中

c# - 获取下一个最小的 Double 数

java - 使用 double 类型时在 Java 中转换为整数

c++ - `auto pp` 和 `auto *ppp` 有什么区别?

c++ - Gettext 错误的默认语言

c - 使用 C 在单个 printf 语句中打印多个不同的内容

c# - 在 C# 中将未知的 long\int\short 转换为 double

java - Double 中的 BigDecimal 值不正确?