How the float data is stored in memory

Source: Internet
Author: User

The float type number is stored in a computer with 4 bytes. Follow the IEEE-754 format standard:
A floating-point number consists of 2 parts: base m and index E

The base section uses a binary number to represent the actual value of this floating-point
The exponent portion occupies a binary number of 8bit and can represent a range of 0-255

However, the index can be negative, so the IEEE stipulates that the sub-square must be subtracted from 127 to be the true exponent.
Therefore, the float type index can be from 126 to 128

The base part is actually a value of 24bit, but the highest level is always 1, so the top bit is omitted from storage, and the storage occupies 23bit
Scientific counting method.

Format:
Seee eeee emmm MMMM MMMM MMMM MMMM MMMM
s indicates floating-point number plus or minus
e-index plus 127 worth of binary data
M base

Example:
17.625 in-memory storage

The first thing to do is convert 17.625 into binary: 10001.101

The integer part, divided by 2, until the quotient is 0, and the remainder is reversed.
The decimal part is multiplied by 2 until the multiply bit 0 and the rounding order is taken.

Move 10001.101 to the right until the decimal point is only 1 digits away:
1.0001101 * 2^4 because right moved four bit

This time, our base and index are out.
Base: Because it must be 1 before the decimal point, the IEEE stipulates that only the decimal point will be recorded. So, the base here is: 0001101
Index: Actual 4, must be added 127 (when the turn out, minus 127), so 131. Which means 10000011.
The symbol part is an integer, so it's 0.
In summary, the 17.625 storage format in memory is:
01000001 10001101 00000000 00000000

How the float data is stored in memory

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.