问题描述
我在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",sinl(fp80bits));
我什至不确定这是怎么发生的,但这是根本原因。
感谢大家的快速解答,并感谢@PeterCordes将我链接到 godbolt 。