Storage calculation of FLOAT data structure in C language

Source: Internet
Author: User

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
  1. void Main (int argc, char* argv[])
  2. {
  3. float a=-8.25;
  4. Char *p= (char*) &a;
  5. *p=0xff;
  6. * (p+1) =0xff;
  7. * (p+2) =0xff;
  8. * (p+3) =0x7f;
  9. printf ("\n&a=%x", &a);
  10. printf ("\na=%f", a);
  11. }
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
  1. #define FLT_DIG 6/* # of decimal digits of precision */
  2. #define Flt_epsilon 1.192092896e-07f/* Smallest such that 1.0+flt_epsilon! = 1.0 */
  3. #define Flt_guard 0
  4. #define FLT_MANT_DIG/* # of bits in Mantissa */
  5. #define FLT_MAX 3.402823466e+38f/* MAX value */
  6. #define FLT_MAX_10_EXP/* MAX decimal exponent */
  7. #define FLT_MAX_EXP/* MAX binary exponent */
  8. #define FLT_MIN 1.175494351e-38f/* MIN Positive value */
  9. #define FLT_MIN_10_EXP ( -37)/* MIN decimal exponent */
  10. #define FLT_MIN_EXP ( -125)/* MIN binary exponent */
  11. #define FLT_NORMALIZE 0
  12. #define FLT_RADIX 2/* exponent RADIX */
  13. #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
  1. void Main (int argc, char* argv[])
  2. {
  3. float a=-8.25;
  4. Char *p= (char*) &a;
  5. *p=0xff;
  6. * (p+1) =0xff;
  7. * (p+2) =0x7f;
  8. * (p+3) =0x7f;
  9. printf ("\n&a=%x", &a);
  10. printf ("\na=%f", a);
  11. }
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
    1. #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
  1. void Main (int argc, char* argv[])
  2. {
  3. float a=-8.25;
  4. Char *p= (char*) &a;
  5. *p=0x01;
  6. * (p+1) =0x00;
  7. * (p+2) =0x00;
  8. * (p+3) =0x00;
  9. printf ("\n&a=%x", &a);
  10. printf ("\na=%e", a);
  11. }

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
  1. void Main (int argc, char* argv[])
  2. {
  3. float a=-8.25;
  4. float b=0;
  5. Char *p= (char*) &a;
  6. *p=0x01;
  7. * (p+1) =0x00;
  8. * (p+2) =0x80;
  9. * (p+3) =0x00;
  10. printf ("\ n%d",sizeof (a));
  11. printf ("\n&a=%x", &a);
  12. printf ("\na=%e", a);
  13. }


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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.