Data storage in memory (2)

Storage of floating point types in memory

We know from the previous chapter that computers store data in binary, so how does it store floating-point data?

Floating point number storage rules

According to the international standard IEEE (Institute of Electrical and Electronics Engineers) 754, any binary floating-point number V can be expressed in the following form:

(-1)^S * M * 2^E
(-1)^S represents the sign bit, when S=0, V is a positive number; when S=1, V is a negative number.
M represents a valid number, greater than or equal to 1 and less than 2.
2^E means exponent bits.

like:
decimalof5.5, written asbinarybecomes101.1, that is1.011*2^2.
According to the above rules, S=0;E=2;M=1.011 .
Then stipulate:
For 32-bit floating-point numbers, the highest bit is the sign bit S, the next 8 bits are the exponent E, and the remaining 23 bits It is a valid number M.
insert image description here
For a 64-bit floating-point number, the highest bit is the sign bit S, the next 11 bits are the exponent E, and the remaining 52 bits are the significand M.
insert image description here
IEEE 754 has some special regulations on the significant figure M and exponent E.
M:

As mentioned earlier, 1≤M<2, that is to say, M can be written in the form of 1.xxxxxx, where xxxxxx represents the decimal part.
IEEE 754 stipulates that when M is saved inside the computer, the first digit of this number is always 1 by default, so it can be discarded, and only the following xxxxxx part is saved. For example, when saving 1.01, only save 01, and then add the first 1 when reading. The purpose of doing this is to save 1 significant figure. Taking the 32-bit floating-point number as an example, there are only 23 bits left for M. After the first 1 is rounded off, 24 significant figures can be saved.

E: Here, E is specified as an unsigned integer; when Edepositin memory,

This means that if E is 8 bits, its value range is 0~255; if E is 11 bits, its value range is 0-2047. However, we know that E in scientific notation can have negative numbers, so IEEE 754 stipulates that an intermediate number must be added to the real value of E when stored in memory. For 8-digit E, the intermediate number is 127; For an 11-bit E, this intermediate number is 1023. For example, the E of 2^3 is 3, so when saving it as a 32-bit floating point number, it must be saved as 3+127=130, which is 10000010.

Index E from memorytake outIt can be further divided into three situations:

E is not all 0 or not all 1.
At this time, the floating point number is represented by the following rules, that is, the calculated value of the exponent E is subtracted by 127 (or 1023) to obtain the real value, and then the effective number M is added before the first digit. 1.
insert image description here
When E is all 0
, the exponent E of the floating-point number is equal to 1-127 (or 1-1023), which is the real value, and the
effective number M is no longer added to the first digit of 1, but is restored to a decimal of 0.xxxxxx. This is done to represent ±0, and
very small numbers close to 0.
E is all 1.
At this time, if the effective number M is all 0, it means ± infinity (positive or negative depends on the sign bit s).

This is the rule of floating-point data storage memory, let's look at an example.

example

int main()
{
    
    
 int n = 9;
 float *pFloat = (float *)&n;
 printf("n的值为:%d\n",n);
 printf("*pFloat的值为:%f\n",*pFloat);
 *pFloat = 9.0;
 printf("num的值为:%d\n",n);
 printf("*pFloat的值为:%f\n",*pFloat);
 return 0;
}

Result:
insert image description here
Explanation:

For the data printed on the first line, there is no doubt;
pFloat is a pointer to the n variable, and it is a floating-point pointer, and when it is dereferenced, it becomesfloating point typeThat's it, then we need to apply our above rules, whenThe representation it fetches from memory;
insert image description here
When *pFloat=9.0, this is a floating point type, so the answer in the fourth line is consistent with it; for the printing of the third line, it needs to be converted,Store in memory, convert to binary, and see the result in integer
insert image description here

Guess you like

Origin blog.csdn.net/m0_74068921/article/details/130989596