Storage Methods of float and double in memory,
This article reproduced in: http://wenku.baidu.com/link? Url = Response
In C, float and double types are used for data of the floating point type. float data occupies 32 bits and double data.
64bit: How do we allocate memory when declaring a variable float f = 2.25f? If it is distributed randomly, isn't the world a mess?
Float and double are both subject to IEEE specifications in terms of storage. float complies with IEEE R32.24, while double follows R64.53.
Both single precision and double precision are divided into three parts in storage:
1. Sign (Sign): 0 indicates positive, and 1 indicates negative.
2. Exponent: used to store index data in scientific notation and stored in shift mode.
3. Mantissa: Mantissa
Shows the storage method of float:
The dual-precision storage method is:
R32.24 and R64.53 both use scientific notation to store data. For example, if 8.25 is expressed in decimal notation, It is 8.25*10 ^ 0, and 120.5.
It can be expressed as 1.205*10 ^ 2. You don't need to talk about the knowledge of these primary schools. While our silly computer doesn't know the decimal data at all, he only knows 0, 1, so
In computer storage, first we need to change the above number to a binary scientific notation, and 8.25 can be expressed as 1000.01 in binary notation.
Won't it be converted? Then I guess it's okay. 120.5 in binary format: 1110110.1 in binary scientific notation 1000.01 can be expressed as 1.0001*2 ^ 3,
1110110.1 can be expressed as 1.1101101*2 ^ 6. The scientific notation of any number is 1. xxx * 2 ^ n. The ending part can be expressed as xxxx. The first part is 1,
Why? You can omit 1 before the decimal point. Therefore, the accuracy of the 23bit ending part is changed to 24bit. For the exponent part
Positive and negative, the index range of the 8-bit index value should be-127-128. Therefore, the storage of the index part adopts shift storage, and the stored data is metadata + 127,
Next let's take a look at the real storage methods of 8.5 and 120.5 in the memory.
First, let's take a look at 8.5, which is represented by the binary scientific Notation: 1.0001*2 ^ 3.
According to the above storage method, the symbol bit is: 0, indicating positive, the index bit is: 3 + 127 = 130, and the number of digits is, so the storage method of 8.5 is shown in:
The Storage Method of Single-precision floating point number 120.5 is shown in:
The following describes the value range and accuracy:
The value range depends on the index:
Float is a signed type, where the 8-bit index bit is 2 ^ 8 = (-128-127), so the actual range is-2 ^ 127-2 ^, -3.4E38-3.4E38
Similarly, the double value range is-1.7E308-1.7E308,
The precision is to check the ending number:
Float tail number 23 bits, 2 ^ 23 = 8.3E6, 7 bits, so different compiler rules are different, some are 7 bits, some are 8 bits
The ending number of double is 52 bits, 2 ^ 52 = 4.5E15, 15 bits, so the valid number of bits of double is 15 bits.