【发布时间】:2024-01-09 12:41:01
【问题描述】:
当我将 long double 类型更改为 double 时,它运行良好。问题是我需要更高的精度,但我不知道为什么使用 long double 我有 -1 作为解决方案。
typedef long double big_num_t;
typedef unsigned int un_i;
long double suite(big_num_t x, big_num_t a2n_1, un_i n) {
return ((-1) * ((a2n_1 * (2 * n - 1)) / (n * (2 * n + 1))) * (x * x));
}
int main() {
big_num_t summ = 3;
big_num_t temp = 3;
for (un_i n = 1; n < 100; ++n) {
temp = suite(3, temp, n);
summ += temp;
}
summ *= (M_2_SQRTPI);
printf("%Lf", summ);
return 0;
}
谢谢 ;)
【问题讨论】:
-
在我的系统上使用 clang 和 gcc 编译你的示例都产生
0.999978而不是-1。 -
还要注意
long double可能与double具有相同的精度。这适用于 Visual Studio,例如:msdn.microsoft.com/en-us/library/s3f49ktz.aspx -
我的系统上也有 0.999978。这是打印问题吗?或许您可以添加:
if (summ < 0) printf("less zero") else printf("not less zero");看看这是否与打印的值一致。 -
好吧,我在带有 MinGW 的 Windows 上使用 Clion。我现在切换 tu linux 看看它是否更适合 gcc。