作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
目前我正在学习浮点异常。我正在写一个带有函数的循环。在该函数中,计算出的值等于 0.5
.随着循环的进行,输入值除以 10
.
循环:
for(i = 0; i < e; i++)
{
xf /= 10.0; // force increasingly smaller values for x
float_testk (xf, i);
}
功能:
void float_testk(float x, int i)
{
float result;
feclearexcept(FE_ALL_EXCEPT); // clear all pending exceptions
result = (1 - cosf(x)) / (x * x);
if(fetestexcept(FE_UNDERFLOW) != 0)
fprintf(stderr,"Underflow occurred in double_testk!\n");
if(fetestexcept(FE_OVERFLOW) != 0)
fprintf(stderr,"Overflow occurred in double_testk!\n");
if(fetestexcept(FE_INVALID) != 0)
fprintf(stderr,"Invalid exception occurred in double_testk!\n");
printf("Iteration %3d, float result for x=%.8f : %f\n",i,x,result);
}
前几次迭代的输出大约是
0.5
后来它变成
0
由于CC。过了一会儿,这是程序的输出:
Iteration 18, float result for x=0.00000000 : 0.000000
Underflow occurred in double_testk!
Iteration 19, float result for x=0.00000000 : 0.000000
Underflow occurred in double_testk!
Iteration 20, float result for x=0.00000000 : 0.000000
Underflow occurred in double_testk!
Iteration 21, float result for x=0.00000000 : 0.000000
Underflow occurred in double_testk!
Invalid exception occurred in double_testk!
Iteration 22, float result for x=0.00000000 : -nan
Underflow occurred in double_testk!
Invalid exception occurred in double_testk!
我想知道从下溢过渡到
NaN
时会发生什么情况.因为下溢意味着数字太小,无法存储在内存中。
NaN
的目标是什么? ?
最佳答案
Because underflow means that the number is too small to be stored in the memory.
x
至少为 2−75,
x * x
产生非零结果。可能在浮点域的次正规部分,精度在下降,但实数结果
x
•
x
大到可以四舍五入到 2−149 或更大。那么,对于这些小
x
,
(1 - cosf(x)) / (x * x)
计算为零除以非零值,因此结果为零。
x
小于 2−75,则
x * x
产生零,因为
x
的实数结果•
x
太小了,在浮点运算中,它被四舍五入为零。然后
(1 - cosf(x)) / (x * x)
计算为零除以零,因此结果是 NaN。这就是您的迭代 22 中发生的情况。
float
。在 2−150 和 2−150 之间的实数结果将向上取整为 2−149。较低的结果将向下取整到 0。假设舍入模式是舍入到最近,并列到偶数。)
关于c - C中下溢和nan有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64032082/
我是一名优秀的程序员,十分优秀!