Two: The storage format of floating point numbers
2.1 IEEE Floating point standard
Above we say, the decimal point of floating-point number is not fixed, if everyone in accordance with their own hobby stored in the computer, that is not a mess? So how do you store this type of numbers on your computer? Such ancient problems as these predecessors have done for us the corresponding norms, no rules radius it. We usually say that the storage specification of floating-point numbers, is specified by the IEEE, the specific specification file is: IEEE Standard 754 for Binary floating-point arithmetic. You can easily download this document from the Web.
In C, the single-precision (float) data type is 32bits, as shown in the following illustration:
The whole 32bits is divided into three parts, namely
Sign: Sign bit, 1 bit,0 is positive, 1 is negative;
Exponent (Bias): Index part, 8 bits, storage format for the move code storage (also described later), offset is 127;
Mantissa (fraction): part of the tail.
The corresponding double precision (double) type has the following format:
Similarly, the 64-bit is also divided into three parts, the control of single precision, I do not have to say you can understand the meaning of the various parts of it?
is not a bit confused, do not be afraid, the theory of this thing most can be fooled people, looks very advanced, in fact, is a fart big thing, for example, it is easy to understand.
For example, if 3.24x103, the corresponding part is, sign is 0,3 as the exponent part (note that the computer store is not 3, this is only for illustration), 3.24 is the mantissa. We know that the computer "stupid" to die, only know 0 and 1, then exactly how a floating-point value in the computer storage media is how to store it?
For example, we want to peek at the value of the floating-point type 4.25 in the computer hard disk stored in the true, please follow me: first of all, to convert 4.25 to binary expression, that is 100.01, in detail, into 1.0001x22, well, the condemnation.
sign=0;
Exponent (bias) =2+127=129 (offset is 127, is directly plus a 127);
mantissa=1.0001-1.0=0001 (normalized, before the decimal point is always an integer 1, the World people know that the front is 1 is not 0, so omitted not to write, that is, the end of the part does not include the integer parts; when people ask you, why the number of bits in the end can represent 24-digit precision, Know how to answer it. Rely on, what, did not read, and then carefully read good will know.
I'm sure you've seen the picture in comparison with the above diagram. Believe in your IQ. To deepen our understanding, one more. If you give a binary number string, 01000000100010000000000000000000, and tell you that this is a float type value, let you say who it is, know how to forget it? If you do not know, look at the picture below, I will not explain the nonsense.
2. 2 deep understanding of floating-point storage formats
For a deeper understanding of the format of floating-point numbers. We use the C language to do something. In the C language world, mandatory type conversion, everyone should be very familiar with. For example:
Copy Code code as follows:
...
float f=4.6;
int i;
...
i = (int) (f+0.5); I=5
..
Below we do not use the coercion type transformation, we calculate the F transformation to the shape by oneself to be equal to how many?
Post the main code as follows:
Copy Code code as follows:
The tail number part of the 23+1 bit
int ival= ((* (int *) (&fval)) & 0X07FFFFF) | 0x800000
Extract Index section
int exponent =-((* (int *) (&fval)) >>) & 0xff);
if (Exponent < 0)
Ival = (ival<<-exponent);
Else
Ival = (ival >> exponent);
If less than 0, the result is reversed
if ((* (int *) &fval) & 0x80000000)
Ival =-ival;
Think carefully about it, see understand, it means that you basically understand the storage format of floating point numbers, if you do not see, and then look, know understand so far.
The above is the entire content of this article, I hope to give you a reference, but also hope that we support the cloud habitat community.