Small mysteries of float

A. Float storage

    1.1. Float total of four bytes

    1.2. Float constitution

        1.2.1 whether single precision or double precision store is divided into three parts:

            <1> sign bit (Sign):. 0 represents a positive, 1 for negative

            <2> exponent (Exponent):. Exponential data is stored in scientific notation, and using the stored shifting (-127-128 range)

            <3> mantissa part (Mantissa):. Mantissa part

                . A way in which the float is stored as shown below:

                . B stores the double precision mode as follows:

 

Two floating-point number calculated

    2.1 such as 8.25 in decimal scientific notation it is: * 8.25 , while 120.5 can be expressed as: 1.205 * , knowledge of these school Needless to say it. 

        But the computer only recognize 0,1, so in the computer store, the first number you want to change the above binary scientific notation, can be expressed as 1000.01 8.25 expressed in binary, (I rely on, not even this will not be converted right? then I estimated to be no way out. left of the decimal representation , the right is ). 120.5 expressed in binary as: 1110110.1 binary scientific notation represented 1000.01 * can be expressed as 1.0001 , 1.1101101 * 1110110.1 can be expressed as any of a number are in scientific notation * are 1.xxx , can be represented as a mantissa part xxxx, the first bit is 1 Well, why should it represent? 1 may be omitted before the decimal point, so the mantissa part of 23bit, accuracy can be turned into a 24bit representation, the truth is here, that 24bit accurate to several decimal places of it, we know the binary representation of 9 is 1001, so 4bit accurately decimal a decimal, float 24bit can make accurate to six decimal places, while the index for the part, because the index may be positive or negative, 8-bit exponent represents the potential energy index range should be as follows: - 127-128, so that use shift storing portion stores index data stored as metadata +127, look at below 8.25 and 120.5 are stored in real memory.

    2.2 facie 8.25, expressed in binary scientific notation as follows: 1.0001 *//files.jb51.net/file_images/article/201606/2016061610214428.gif

        2.2.1 storage accordance with the above, the sign bit is: 0, denoted as positive, exponent bit is: 127 + 3 = 130 (see as to why back plus 127), the median part, so that the following storage 8.25 FIG.

 

        2.2.2 Single-precision floating point 120.5 storage as shown below:

 

III. Why plus 127

   3.1. 

   

   3.1. In the above example, we know that E represents the size of the power, and was stored in the computer's e E + 127, then the question is, why here to add this number 127 it?

        <1>. 

      <2> In fact, that is to say: computer representation of single precision floating point numbers are 8-bit exponent portion to store, in the above values, represents 0 to 255, but we also need to have a negative index, the number of bits negative exponent for equalization, each half is a special point of -127 ~ 128,0, special handling. Storage time will be added 127, so that 0 to 255 is just right, the storage can be very good, otherwise, to determine the sign bit need is determined negative value (-127 computer can take the index obtained negative )

 

Interview questions:

Please write float x compared with the "zero value" if statement

answer:

const float EPSINON = 0.00001;

if((x>=-EPSINON)&&(x<=EPSINON));

Guess you like

Origin www.cnblogs.com/linux-37ge/p/11241618.html