Floating-point memory representation---record a topic

Source: Internet
Author: User

First, the basis of the system

1. Decimal Turn binary method

When a decimal is converted to a binary number, the integer and fractional parts are converted and then merged, because the integer and decimal are converted differently. For example , convert a decimal integer to binary: Converts (173) 10 to a binary number.

For example, to convert decimal decimals to binary: Converts (0.8125) 10 to binary decimals.

Decimal decimals are converted into binary decimals using the "Multiply 2 rounding, order" method. The specific method is: with 2 times decimal decimals, you can get the product, the integral part of the integer, and then 2 by the remainder of the fractional part, and then get a product, and then the integral part of the integer is taken out, and so on, until the integer part of the product is zero , or the whole number is divided into 1, when 0 or 1 or to achieve the required accuracy .

Binary storage mode and conversion of floating-point number

Both single-precision and double-precision are divided into three parts in-memory storage:

1. Sign bit: 0 means positive, 1 means negative;

2. Digital digits: Used to store exponential data in scientific notation, and to use shift storage;

3. Part of the tail: because the first digit in the scientific notation is always 1, it can be shed. For example 1. 01*2^2 (Red 1 is the default bit).

Float is stored in memory in the following ways:

The double-precision memory is stored in the following ways:

Steps to convert the float type to memory storage format are as follows:

1. Convert the real number to a binary representation first.

2. Move the decimal point of the binary format real number to the left or right by n bits, so that the decimal point moves to the right of the first valid digit.

3. If the real number is positive then 31 bits are put into 0, if the negative 31 bits are put into 1.

4. The exponent in the scientific notation can be negative, so it is stated that the true value of the exponent must be added to an intermediate number, and for a 8-bit exponent, the median is 127; For a 11-bit exponent, the median is 1023. Fill in 22-30 bits with the added binary number.

5. Finally, the first digit to the right of the decimal point starts with a 23-digit number into 0-22 digits.

Three, give two examples

Example 1:float floating-point 125.5 converted to 32-bit binary floating-point number

125.5 binary code is 1111101.1, written as binary scientific count: 1.111101*2^6 (because the scientific notation "integer" part is greater than 1, in the binary, the "integer" part can only be 1) that is shifted to the left 6 bits, then e=6, then e=e+127= 133, while the binary code for E is 10000101.

So 125.5 of the 32-bit binary floating-point numbers are

0 10000101 11110100000000000000000

Example 2:float floating-point 0.5 converted to 32-bit binary floating-point number

The binary code of 0.5 is 0.1, and the scientific count written as binary is: 1.0*2^ (-1) Shifts 1 bits to the right, then e=-1, then e=e+127=126, and E's binary code is 01111110.

So 0.5 of the 32-bit binary floating-point numbers are

0 01111110 00000000000000000000000

It's too late. Write the memory representation of 0.0f, and how the program verifies that these representations are correct.

Floating-point memory representation---record a topic

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.