为什么isnormal()说的值是正常的,当它是不是? [英] Why is isnormal() saying a value is normal when it isn't?
本文介绍了为什么isnormal()说的值是正常的,当它是不是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <stdlib.h>
#include <stdio.h>
#include <float.h>
#include <math.h>
void PrintBytes( const float value )
{
const char* const byte = ( const char* )&value ;
for( size_t i = 0 ; i < sizeof( value ) ; i++ )
{
printf( "%02hhx" , byte[i] );
}
}
int main(void)
{
float value = FLT_MIN;
while( 1 )
{
printf( "%e %d " , value , isnormal( value ) );
PrintBytes( value );
puts( "" );
if( !isnormal( value ) )
{
break;
}
value /= 2.0F;
}
return 0;
}
输出是:
1.175494e-038 1 00008000
5.877472e-039 1 00004000
2.938736e-039 1 00002000
1.469368e-039 1 00001000
7.346840e-040 1 00000800
3.673420e-040 1 00000400
1.836710e-040 1 00000200
9.183550e-041 1 00000100
4.591775e-041 1 00800000
2.295887e-041 1 00400000
1.147944e-041 1 00200000
5.739719e-042 1 00100000
2.869859e-042 1 00080000
1.434930e-042 1 00040000
7.174648e-043 1 00020000
3.587324e-043 1 00010000
1.793662e-043 1 80000000
8.968310e-044 1 40000000
4.484155e-044 1 20000000
2.242078e-044 1 10000000
1.121039e-044 1 08000000
5.605194e-045 1 04000000
2.802597e-045 1 02000000
1.401298e-045 1 01000000
0.000000e+000 0 00000000
显然,第二个值 5.877472e-039
是低于正常,因为它的指数为0, 00004000
。
Clearly the second value 5.877472e-039
is subnormal, since its exponent becomes 0, 00004000
.
Ideone 产生正确的结果:
1.175494e-38 1 00008000
5.877472e-39 0 00004000
我编译在Windows上使用gcc我的code(MinGW的-W64)。
I'm compiling my code using gcc (MinGW-w64) on Windows.
推荐答案
这适用于其他平台如预期(在这里,在 ideone 为例),所以它可能是你正在使用的GCC /标准库版本的问题。
That works as expected on other platforms (here, on ideone, for example) so it is probably an issue with the gcc/standard library version you are using.
最可能的原因是,参数 isnormal
被转换为double。
The most probable cause is that the argument to isnormal
is being converted to a double.
这篇关于为什么isnormal()说的值是正常的,当它是不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文