Java float data storage, loss of precision problems

Source: Internet
Author: User

The float type in Java is 4 bytes 32 bits, and in-memory storage follows the IEEE-754 format standard:

A floating-point number consists of 2 parts: base m and index E
Base m part: uses a binary number to represent the actual value of this floating point.
Index e part: A binary number that occupies 8bit (1 bytes), which can represent a range of values 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 portion is actually a value of 24bit (3 bytes), but the highest level is always 1, so the top bit is omitted from storage, which accounts for 23bit in storage.

So 32 bits of a floating-point number are made up of three parts:

1. The first bit is the sign bit, indicating whether the floating point number is a positive or negative, 0 is positive, and 1 is negative;

2. The second to the Nineth place a total of 8 to denote the exponent E, turns into the binary need +127 (conversely minus 127);

3. The last 23 bits are the base, but the storage automatically omits the highest 1, so the storage is 23 bits actually 24 bits. (Decimal position is 2^ (23) =8388608 7 digits calculated as 6-digit precision)

For a chestnut: 3.2f

Convert to binary storage: integral part 32 binary 11

Fractional part 0.2 is converted to binary (by 2 to the integer portion until the fractional part is divided into 0)----> (decimal by 2) 0.4 0.8 1.6 1.2 0.4 0.8 1.6 1.2 0.4 ... (Infinite Loop)

The result is: 11.00 1100 11001100 11001100 11001100 (because of the limited number of bits, and the fractional part is infinitely bad, this has lost precision)

Move right until you have only one left: 1.100 1100 11001100 11001100 11001100 (one bit to the right, the exponential portion is 1+127=128 1000 0000)

Finally get binary storage: 0 100 0000 0100 1100 11001100 11001100 (loss of precision)

Inverse conversion to floating point decimal: Sign bit 0: positive

Index 100 0000 0 is 128 minus 127 index is 1

Number of parts: the highest bit 1 is omitted by default plus the decimal point is 1.100 1100 11001100 11001100

Positive index right Shift one 11.0011 00110011 0011001100

Positive number part---->3 Base part 2^ (-3) +2^ (-4) +2^ (-7) +2^ (-8) +2^ (-11) +2^ (-12) +2^ (-15) +2^ (-16) +2^ (-19) +2^ (-20)

Java float data storage, loss of precision problems

Related Article

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.