【发布时间】:2012-10-10 11:37:11
【问题描述】:
我在 C99 中使用 fetestexcept(),它有时会抱怨浮点数相乘会得到不精确的结果 (FE_INEXACT)。将浮点变量与浮点文字相乘时似乎会发生这种情况。我怎样才能修改这个以便 fetestexcept() 不会抱怨?
gcc -std=c99 -lm test.c
#include <stdio.h>
#include <math.h>
#include <fenv.h>
#pragma STDC FENV_ACCESS ON
int main(void)
{
float a = 1.1f;
float b = 1.2f;
float c = a * b * 1.3f;
int exception = fetestexcept(FE_ALL_EXCEPT);
if(exception != 0)
{
printf("Exception: 0x%x\n", exception); // 0x20 FE_INEXACT
}
return 0;
}
【问题讨论】: