float type maximum and minimum values

1. Understand the float storage structure

For float storage structure, please see another article http://blog.csdn.net/whzhaochao/article/details/12885875

2. The maximum value of float

The float structure is as follows:

To obtain a positive maximum value, set the sign bit to 0, and the exponent and mantissa bits to be all 1, then it should be the maximum value, and the maximum value should be:
1.11111111111111111111111*2^128=(2-2^-23)*2^128= 6.805646932770577*10^38
Then the float type hexadecimal representation is: 0x7fff ffff 
 Here is the test code:
void main(int argc, char* argv[])
{
	float a=-8.25;
	char *p=(char*)&a;

	*p=0xff;
	*(p+1)=0xff;
	*(p+2)=0xff;
	*(p+3)=0x7f;

	
	printf("\n&a=%x",&a);
	printf("\na=%f",a);

}
The result is not what we thought, 1.#QNAN0 appeared, I don’t know why, if you know the reason, please reply!



If the last digit of the index is 0, it is the result we want,
We know that the maximum value of float is (2-2^-23)*2^127= 3.4028234663852886*10^38
see float.h
#define FLT_DIG         6                       /* # of decimal digits of precision */
#define FLT_EPSILON     1.192092896e-07F        /* smallest such that 1.0+FLT_EPSILON != 1.0 */
#define FLT_GUARD       0
#define FLT_MANT_DIG    24                      /* # of bits in mantissa */
#define FLT_MAX         3.402823466e+38F        /* max value */
#define FLT_MAX_10_EXP  38                      /* max decimal exponent */
#define FLT_MAX_EXP     128                     /* max binary exponent */
#define FLT_MIN         1.175494351e-38F        /* min positive value */
#define FLT_MIN_10_EXP  (-37)                   /* min decimal exponent */
#define FLT_MIN_EXP     (-125)                  /* min binary exponent */
#define FLT_NORMALIZE   0
#define FLT_RADIX       2                       /* exponent radix */
#define FLT_ROUNDS      1                       /* addition rounding: near */


When we make the index position: 1111 1110 =254, the index is 254-127=127
If the mantissa is all 1, the maximum number is 1.111111111111111111111*2^127=(2-2^-23)*2^127=3.4028234663852886*10^38
Then the hexadecimal representation is: 0x7f7f ffff

3. Test code:

void main(int argc, char* argv[])
{
	float a=-8.25;
	char *p=(char*)&a;

	*p=0xff;
	*(p+1)=0xff;
	*(p+2)=0x7f;
	*(p+3)=0x7f;

	
	printf("\n&a=%x",&a);
	printf("\na=%f",a);

}

We can see from the results
&a=12ff44
a=340282346638528860000000000000000000000.000000
This is the maximum value of float


4.float positive minimum value

In float.h we see that the minimum value of float is  1.175494351e-38F
#define FLT_MIN         1.175494351e-38F        /* min positive value */
By understanding the structure of the float type, we know how to obtain a positive minimum value. To obtain a positive minimum value, we only need to set the index position to the minimum and 0000 0000, then the index is 0-127=-127, and then place the mantissa at the last position 1, others set to 0
and hex is 0x0000 00001

5. Test code

void main(int argc, char* argv[])
{
	float a=-8.25;
	char *p=(char*)&a;

	*p=0x01;
	*(p+1)=0x00;
	*(p+2)=0x00;
	*(p+3)=0x00;


	printf("\n&a=%x",&a);
	printf("\na=%e",a);


}

The result we got is 1.00000000 00000000 0000 01*2^-127= 5.877472454760670*10^-039, but the result is not what we predicted! do not know why


If the index position is set to 1, the test code is as follows:
void main(int argc, char* argv[])
{
	float a=-8.25;
	float b=0;
	char *p=(char*)&a;

	*p=0x01;
	*(p+1)=0x00;
	*(p+2)=0x80;
	*(p+3)=0x00;
	
	printf("\n %d ",sizeof(a));
	printf("\n&a=%x",&a);
	printf("\na=%e",a);


}



We see that the result is 1.0000 0000 0000 0000 0000 001*2^-126= 1.1754944909521339e-038, which is the result we want!

Guess you like

Origin blog.csdn.net/whzhaochao/article/details/12887779