Understand floating-point numbers, use the IEEE 754 standard to represent floating-point numbers, and store floating-point numbers

Floating point numbers can usually be used to represent any real number.

The specific format is as follows:

Floating point number = mantissa * base order code

N = M * r E (where the mantissa is a normalized pure decimal)

Everyone should be familiar with this expression. Take a closer look. Isn't this scientific notation? The base of scientific notation is 10, which means that the base is 2.

for example:

Binary representation is a floating point number similar to scientific notation: 11100.101 = 1.1100101 * 24

Decimal representation in scientific notation format: 28.625 = 2.8625 * 101

Of course, the computer cannot be stored directly. The storage format in the computer is:

Number Order sign Order code mantissa

or

Order sign Order code Number mantissa

Among them; the number symbol is the symbol of the mantissa, and the order symbol is the symbol of the order code.

IEEE 754

In the IEEE 754 standard:

Insert picture description here

Normalized number: +/- 1.xxxxxxxx 2 * 2 E (in which the exponent bit is represented by a shift code, and the effective bit is represented by the original code)

The normalized mantissa always normalizes the highest bit to 1, without recording in the storage format, which implies

Single-precision floating-point number calculation formula: (-1)S * (1 + significant digit) * 2 (order code -127)
double-precision floating-point number calculation formula: (-1)S * (1 + significant digit) * 2 (order Code-1023)

Single-precision storage format:

Sign bit Order code Valid bit
1 bit 8 bit 23 bit

Occupies a total of 32 bits;

The offset value of the frame shift is 127;

The normalized order code range is: 0000 0001(1)~1111 1110(254), all 0s and all 1s are used to indicate special values!

Order code Valid bit Representation bit
0 0 +/- 0
0 Non-zero Invalid value
1-254 Effective value Effective value
255 0 +/- infinity
255 Zero Non-numerical

Double-precision storage format:

Sign bit Order code Valid bit
1 bit 11 bit 52 bit

It occupies a total of 64 bits, and the offset value of the shift code is 1023

for example:

Convert -12.75 to binary representation:

Don’t care about the sign bit, because IEEE 754 stores the sign specifically

The first step is the integer part 12=1100B, the
second step is the decimal part.75=.11B, the
third step is to normalize the number 1100.11=1.10011 * 2 3
Note that the 3 here is x-127 = 3,
so x = 130 = 127+ 3=128 + 2 =1000 0010B

The final storage is:

1 1000 0010 100 1100 0000 0000 0000 0000

Expressed in hexadecimal as: C14C0000H

Guess you like

Origin blog.csdn.net/weixin_44223946/article/details/110198977