| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛

问题点数：5 回复次数：2
C语言检测计算产生subnormal值的函数是什么?

https://cloud.
，按照这篇文章测试。如果运算产生了上溢和下溢，并未匹配到对应的宏。

```#include <stdio.h>
#include <math.h>
#include <float.h>
#include <fenv.h>

#pragma STDC FENV_ACCESS ON
void show_fe_exceptions(void)
{
int result;
printf("exceptions raised:");
if (result = fetestexcept(FE_DIVBYZERO))
{
printf(" FE_DIVBYZERO\n");
printf("result = %d\n", result);
}
else if (result = fetestexcept(FE_INEXACT))
{
printf(" FE_INEXACT\n");
printf("result = %d\n", result);
}
else if (result = fetestexcept(FE_INVALID))
{
printf(" FE_INVALID\n");
printf("result = %d\n", result);
}
else if (result = fetestexcept(FE_OVERFLOW))
{
printf(" FE_OVERFLOW\n");
printf("result = %d\n", result);
}
else if (result = fetestexcept(FE_UNDERFLOW))
{
printf(" FE_UNDERFLOW\n");
printf("result = %d\n", result);
}
else
{
printf(" no above expections\n");
printf("result = %d\n", result);
}
feclearexcept(FE_ALL_EXCEPT);
printf("\n");
}

int main(void)
{
printf("MATH_ERREXCEPT is %s\n\n",
math_errhandling & MATH_ERREXCEPT ? "set" : "not set");

float toobig = 3.4E38 * 100.0f;
printf("toobig = %e\n", toobig);///< 未产生上述异常，输出inf
show_fe_exceptions();

float toosmall = 1.0 * 3.4E-65565;
printf("toosmall = %e\n", toosmall);///< 未产生上述异常，输出0.000000e+00
show_fe_exceptions();

return 0;
}
```

[此贴子已经被作者于2021-5-4 18:54编辑过]

```#include <stdio.h>
#include <math.h>
#include <float.h>
#include <fenv.h>

#pragma STDC FENV_ACCESS ON
void show_fe_exceptions(void)
{
int result;
printf("exceptions raised:");
if (result = fetestexcept(FE_DIVBYZERO))
{
printf(" FE_DIVBYZERO\n");
printf("result = %d\n", result);
}
else if (result = fetestexcept(FE_INEXACT))
{
printf(" FE_INEXACT\n");
printf("result = %d\n", result);
}
else if (result = fetestexcept(FE_INVALID))
{
printf(" FE_INVALID\n");
printf("result = %d\n", result);
}
else if (result = fetestexcept(FE_OVERFLOW))
{
printf(" FE_OVERFLOW\n");
printf("result = %d\n", result);
}
else if (result = fetestexcept(FE_UNDERFLOW))
{
printf(" FE_UNDERFLOW\n");
printf("result = %d\n", result);
}
else
{
printf(" no above expections\n");
printf("result = %d\n", result);
}
feclearexcept(FE_ALL_EXCEPT);
printf("\n");
}

int main(void)
{
printf("MATH_ERREXCEPT is %s\n\n",
math_errhandling & MATH_ERREXCEPT ? "set" : "not set");

/*printf("0.0/0.0 = %f\n", 0.0 / 0.0);///< 编译不能通过，错误：C2124，被零除或对零求模
show_fe_exceptions();

printf("1.0/0.0 = %f\n", 1.0 / 0.0);///< 编译不能通过，错误：C2124，被零除或对零求模
show_fe_exceptions();*/

printf("0.0/DBL_MIN = %f\n", 0.0 / DBL_MIN);///< 未产生上述异常，0.0/DBL_MIN = 0.000000
show_fe_exceptions();

printf("0.0/(DBL_MIN * -1) = %f\n", 0.0 / (DBL_MIN * -1));///< 未产生上述异常，0.0/(DBL_MIN * -1) = -0.000000
show_fe_exceptions();

return 0;
}
```

[此贴子已经被作者于2021-5-4 18:54编辑过]

https://en.

• 3
• 1/1页
• 1

Powered by Discuz, Processed in 0.095625 second(s), 9 queries.
Copyright©2004-2021, BCCN.NET, All Rights Reserved