Java FAQ _06 Data Structure (025) _ binary how to represent floating-point values

Source: Internet
Author: User

Click to enter _ more _java thousand ask

1. How to represent floating-point values with binary

Before we know how to express a floating-point value in binary, we need to know how to represent integer values in binary notation: How to represent integer values with binary
Since the computer only knows 0, 12, and so the same as representing integers, floating-point values will eventually be interpreted as binary machine code, and unlike integer, all floating-point types stored by the computer are converted to decimal by operation, so they are highly approximate and cannot be 100% accurate. The specific rules are as follows:

    1. Follow the Ieee754 standard (IEEE binary floating-point arithmetic standard)

    2. The first is the sign bit, 1 for negative, 0 for positive.

3. To remove the first, the binary used to represent the floating-point type needs to be divided into digits and trailing digits (also known as decimal digits).

    1. The digits and the trailing bits of different floating-point types occupy the same length.

    2. The exponential deviation of the binary conversion decimal is: 2^ (refers to the digit length-1)-1.

In this case, the number of digits, the number of digits is the key to the decimal binary operation, with 32-bit floating point as an example, it consists of 1-bit sign bit, 8-bit exponent bit and 23-bit tail digits, the following 32-bit floating-point number:

0100 0010 1101 0110 0101 0001 1100 1111

Where: digits: 100 0010 1; number of digits: 101 0110 0101 0001 1100 1111.

From the above conclusions:

    1. The sign bit is: 0, which is a positive value.

    2. 32-bit floating point, so exponential deviation value: 2^ (8-1)-1 = 127.

    3. The digits are: 100 0010 1, which is the decimal 133.

    4. The trailing digits are: 101 0110 0101 0001 1100 1111.

Below we calculate the decimal value according to the above conclusion, the steps are as follows:

    1. Calculates the exponent, which is the number minus the exponent deviation value, i.e. 133-127=6

    2. To calculate decimals, first add a decimal point to the front of the trailing digit and the hidden bit 1:1.101 0110 0101 0001 1100 1111, and then right shift exponent 6:1101 011.0 0101 0001 1100 1111

    3. Bitwise operation, the exponentiation of 2 is obtained by bit:
      1* (2^6) +1* (2^5) +0* (2^4) +1* (2^3) +0* (2^2) +1* (2^1) +1* (2^0) + decimal +0* (2^-1) +0* (2^-2) +1* (2^-3) +0* (2^-4) +1* (2^-5) +0* (2^ -6) +0* (2^-7) +0* (2^-8) +1* (2^-9) +1* (2^-10) +1* (2^-11) +0* (2^-12) +0* (2^-13) +1* (2^-14) +1* (2^-15) +1* (2^-16) +1* (2^-17)
      =107.1597824

    4. To add a symbol bit: +107.1597824

It can be concluded that the floating-point type conversion requires a certain amount of CPU operation, and is not accurate, if you want to be as accurate as possible, you need to increase the number of floating-point type, such as 64 bits. In addition, different decimal floating-point numbers may be converted to the same binary floating-point number, for example:

        float423.15243f;        float423.15244f;        System.out.println(f3 == f4);

The result is:

True

In other words, we can only judge the accuracy of two floating-point numbers, generally use F4-F3 < 0.0001 to determine whether two floating-point numbers are equal (approximately equal).

2. How floating-point in Java is represented by binary

In the Java language, floating-point values are divided into 2 types: float, double, are signed integers.

See the basic data types here: What are the 8 basic data types in Java
These types in addition to the length of the inconsistency, the other rules are in accordance with the above rules, specifically as follows:

float
Memory occupies 8 bytes, 32bit. Where the sign bit 1 bits, refers to the digit 8 bits, the tail digit 23 bits. Exponential deviation value: 2^ (8-1)-1 = 127

For example:

 Public classTest { Public Static void Main(string[] args) throws Unsupportedencodingexception {floatF1 =423.1594FfloatF2 =-423.1594FintFloatToIntBits1 = float.floattointbits (F1);///According to the IEEE754 rule, a floating-point representation value is obtained.         intFloatToIntBits2 = Float.floattointbits (F2); System. out. println ("Positive float==="+ integer.tobinarystring (floatToIntBits1));//Turn binarySystem. out. println ("Negative float==="+ integer.tobinarystring (floatToIntBits2)); System. out. println ("Positive float==="+ integer.tohexstring (floatToIntBits1));//Turn hexSystem. out. println ("Negative float==="+ integer.tohexstring (floatToIntBits2));}}

The results are as follows:
Zheng float===1000011110100111001010001100111
Negative float===11000011110100111001010001100111
Zheng float===43d39467
Negative float===c3d39467

Double
Memory occupies 16 bytes, 64bit. Where the sign bit 1 bits, refers to the digit 11 bits, the tail digit 52 bits. Exponential deviation value: 2^ (8-1)-1 = 1023

For example:

 Public classTest { Public Static void Main(string[] args) throws Unsupportedencodingexception {DoubleD1 =423453.1597824345;DoubleD2 =-423453.1597824345;LongDoubleToLongBits1 = double.doubletolongbits (D1);///According to the IEEE754 rule, a floating-point representation value is obtained.         LongDoubleToLongBits2 = Double.doubletolongbits (D2); System. out. println ("Positive double==="+ long.tobinarystring (doubleToLongBits1));//Turn binarySystem. out. println ("Negative double==="+ long.tobinarystring (doubleToLongBits2)); System. out. println ("Positive double==="+ long.tohexstring (doubleToLongBits1));//Turn hexSystem. out. println ("Negative double==="+ long.tohexstring (doubleToLongBits2));}}

The results are as follows:
Zheng double===100000100011001110110000111010010100011100111100000000110101011
Negative double===1100000100011001110110000111010010100011100111100000000110101011
Zheng Double===4119d874a39e01ab
Negative DOUBLE===C119D874A39E01AB

Java FAQ _06 Data Structure (025) _ binary how to represent floating-point values

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.