C Language Advanced Chapter (3) The Secret of Floating Point Numbers

1. Floating point numbers in memory

    Floating point numbers are stored in memory as sign bit, exponent, and mantissa

   

    The representation of float and double data in the computer is the same, but due to the difference in the memory space occupied, the numerical range and precision that they can represent respectively are different.

Second, the conversion of floating point numbers

  1.     Convert floating point number to binary
  2.     Represent binary floating point numbers in scientific notation
  3.     Calculates the exponentially shifted value

    Note: The offset needs to be added when calculating the exponent, and the value of the offset is related to the type.

    Example: For exponent 6, the offset value is as follows:

    float:127+6 ->133

    double:1023 ->1029

3. Examples

    In-memory float representation of the real number 8.25

        The binary representation of 8.25: 1000.01 -> 1.00001 x (2^3)

            Sign bit: 0

            The index is: 3+127 -> 130 -> 10000010

            Decimal places: 00001

        The float representation of 8.25 in memory

            0 10000010 00001 000000000000000000 -> 0x41040000

    

    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324730321&siteId=291194637