How to store A/C + + floating point number in memory

Source: Internet
Author: User

Transferred from: https://www.cnblogs.com/dolphin0520/archive/2011/10/02/2198280.html

Any data in memory is stored in binary form, such as a short data 1156, and a binary representation of 00000100 10000100. In the Intel CPU architecture of the system, the storage method is 10000100 (low address unit) 00000100 (High address unit), because the Intel CPU architecture is a small terminal mode. But how are floating-point numbers stored in memory? All of the C + + compilers currently use the standard floating-point format developed by IEEE, the binary scientific notation.

In binary scientific notation, S=M*2^N consists of three main parts: the sign bit + order code (N) + mantissa (M). For the float type data, the second binary has 32 bits, wherein the sign bit 1 bits, the order 8 bits, the mantissa 23 bits; for double type data, the second binary is 64 bits, the sign bit 1 bits, the order code 11 bits, the Mantissa 52 bits.

31 30-23 22-0

Float sign bit order mantissa

63 62-52 51-0

Double sign bit order Mantissa

Sign bit: 0 means positive, 1 means negative

Order code: Here the order code to use the shift code, for the float type data, its specified offset is 127, the order has a positive negative, for 8-bit binary, then its representation range is -128-127,double type 1023, which represents a range of-1024-1023. For example, for float type data, if the real value of the order code is 2, then add 127 after 129, and its order representation is 10000001

Mantissa: A valid digit bit, that is, part bits (bits after the decimal point), because the integer portion of the specified m is constant 1, so this 1 is not stored.

The following examples illustrate:

Float data 125.5 converted to standard floating-point format

The 1252 binary representation is 1111101, the fractional part is represented as binary 1, the 125.5 binary is represented as 1111101.1, because the integer portion of the mantissa is constant 1, it is 1.1111011*2^6, the order is 6, plus 127 is 133, is represented as 10000101, and for the mantissa, the integer Part 1 is removed, 1111011, after which 0 of the number of digits to 23 bits, then 11110110000000000000000

Then the binary representation is

0 10000101 11110110000000000000000, the memory is stored in the following way:

00000000 Low Address

00000000

11111011

01000010 High Address

Conversely, to calculate a floating-point number in binary form, such as 0 10000101 11110110000000000000000

Because the symbol is 0, it is a positive number. The order code is 133-127=6 and the mantissa is 11110110000000000000000, then its true mantissa is 1.1111011. So its size is

1.1111011*2^6, move the decimal point to the right 6 bits, get 1111101.1, and 1111101 decimal is 125,0.1 decimal for 1*2^ (-1) = 0.5, so its size is 125.5.

Similarly, if you convert the float type data 0.5 to a binary form

0.5 binary form is 0.1, because the specified positive part must be 1, the decimal point to the right 1 bits, then the 1.0*2^ (-1), the order is -1+127=126, expressed as 01111110, and the Mantissa 1.0 minus the integer part is divided into 0, 0 to 23 bit 00000000000000000000000, then the second binary representation is

0 01111110 00000000000000000000000

From the above analysis, the maximum representation range of float type data is 1.11111111111111111111111*2^127=3.4*10^38

Similar to double data, except that the order code is 11 bits, the offset is 1023, and the mantissa is 52 bits.

Test procedure:

/*test how floating-point data is stored in memory 2011.10.2*/#include<iostream>using namespacestd;intMainintargcChar*argv[]) {    floatA=125.5; Char*p= (Char*) &A; printf ("%d\n",*p); printf ("%d\n", * (p+1)); printf ("%d\n", * (p+2)); printf ("%d\n", * (p+3)); return 0;}

The output is:

0

0

-5

66

It is already known that the float type 125.5 is stored in memory in the following way:

00000000 Low Address

00000000

11111011

01000010 High Address

Therefore, for P and p+1 points, the decimal integer represented by the stored binary number is 0;

And for p+2 points, because the char pointer is a signed data type, so 11111011, the sign bit is 1, is negative, because in memory the binary is in the complement of the store, so its truth value is-5.

For a cell that p+3 points to, 01000010, is a positive number, then its size is 66. The output of the above program verifies its correctness.

How to store A/C + + floating point number in memory

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.