1. Learn about the float storage structure float storage structure see another article http://blog.csdn.net/whzhaochao/article/details/128858752. Float maximum value float structure such as: To obtain a positive maximum value, that is, the sign bit is 0, the digits and the tail bit is all 1, then the maximum will be, the maximum value should be: 1.11111111111111111111111*2^128= (2-2^-23) *2^128= 6.805646932770577*10^38 the float type hexadecimal is represented as: 0x7FFF FFFF The following is the test code:
[CPP]View PlainCopy
- 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 as we thought, appeared 1. #QNAN0, this I do not know why, know the reason can reply!
If the last digit of the exponent is 0, it is the result we want, we know that float is the largest (2-2^-23) *2^127= 3.4028234663852886*10^38 See float.h
[CPP]View PlainCopy
- #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/* # of bits in Mantissa */
- #define FLT_MAX 3.402823466e+38f/* MAX value */
- #define FLT_MAX_10_EXP/* MAX decimal exponent */
- #define FLT_MAX_EXP/* 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 digits: 1111 1110 =254 The exponent is 254-127=127, the number is 1, the maximum number is 1.11111111111111111111111*2^127= (2-2^-23) *2^127= 3.4028234663852886*10^38 hexadecimal is represented as: 0x7f7f ffff3. Test code:
[CPP]View PlainCopy
- 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 float.h we see that the minimum value of float is 1.175494351e-38f
[CPP]View PlainCopy
- #define FLT_MIN 1.175494351e-38f/* MIN Positive value */
By understanding the structure of the float type we know how to get the positive minimum value, we just need to get the exponent position to the minimum and 0000 0000 The exponent is 0-127=-127, then the last position of the trailing digit is 1, the other 0 and hexadecimal is 0x0000 000015. Test code
[CPP]View PlainCopy
- 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);
- }
We got a result of 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 1, the test code is as follows:
[CPP]View PlainCopy
- 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 results for 1.0000 0000 0000 0000 0000 001*2^-126= 1.1754944909521339e-038, which is the result we want!
Storage calculation of FLOAT data structure in C language