我在MacOS上,并按照Using the Intel® Math Library指南实现第一个示例:
// real_math.c
#include <stdio.h>
#include <mathimf.h>
int main() {
float fp32bits;
double fp64bits;
long double fp80bits;
long double pi_by_four = 3.141592653589793238/4.0;
// pi/4 radians is about 45 degrees
fp32bits = (float) pi_by_four; // float approximation to pi/4
fp64bits = (double) pi_by_four; // double approximation to pi/4
fp80bits = pi_by_four; // long double (extended) approximation to pi/4
// The sin(pi/4) is known to be 1/sqrt(2) or approximately .7071067
printf("When x = %8.8f, sinf(x) = %8.8f \n", fp32bits, sinf(fp32bits));
printf("When x = %16.16f, sin(x) = %16.16f \n", fp64bits, sin(fp64bits));
printf("When x = %20.20Lf, sinl(x) = %20.20Lf \n", fp80bits, sinl(fp80bits));
return 0;
}
按照指南中的指示,我编译为:icc real_math.c
执行时,我得到:When x = 0.78539819, sinf(x) = 0.70710677
When x = 0.7853981633974483, sin(x) = 0.7071067811865475
When x = 0.78539816339744830952, sinl(x) = 0.00000000000000000000
我已经进行了广泛的搜索,所有示例似乎都表明这应该是微不足道的。我想念什么? 我试图将
-long_double
传递给icc
,但是没有任何变化。
最佳答案
好的,我知道了。此问题是由于省略1个字符L
而导致的我的错误。具体而言,以某种方式在我自己的计算机上的代码中,我有:
printf("When x = %20.20Lf, sinl(x) = %20.20f \n", fp80bits, sinl(fp80bits));
正确的代码是这样(注意%20.20f
与%20.20Lf
): printf("When x = %20.20Lf, sinl(x) = %20.20Lf \n", fp80bits, sinl(fp80bits));
我什至不知道这怎么可能发生,但这是根本原因。谢谢大家的快速回答,并感谢@PeterCordes将我链接到 godbolt 。
关于c++ - 如何在Intel ICC中启用long double,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64333966/