Interview essential: floating point decryption in Java

There is such an interview question, I believe many friends will be confused

package com.newer;

public class HelloWorld {

	public static void main(String[] args) {
		
//		float单精度保留8位有效数字
//		double双精度,保留16位有效数字
		
		float f1=20.5f;
		float f2=20.3f;
		
		double d1=20.5;
		double d2=20.3;
		
//		所有的运算在计算机内部都是按照二进制来计算的
		System.out.println(f1==d1);
		System.out.println(f2==d2);
		
		
		
		
		
		
	}
}

Console output: 


 Why is f1 == d1, and f2 and d2 are not equal , here is the answer for partners.

First we need to know the relationship between binary and decimal

Binary and decimal


    In real life, the
    binary counting
    principle is generally used in a computer with a decimal system : the computer has
    only two circuits: the circuit and the circuit have only two states: power on and power off, the number 1 means power on, and 0 means power off .
    The software level is called a bit, or a binary bit.


Conversion relationship between binary and decimal

For example, int t = 59;
    59% 2 = 1 59/2 = 29
    29% 2 = 1 29/2 = 14
    14% 2 = 0 14/2 = 7
    7% 2 = 1 7/2 = 3
    3% 2 = 1 3/2 = 1
    1% 2 = 1 1/2 = 0

 The resulting remainder is concatenated 111011 in reverse order  


Integer binary storage

Integer binary and integer type related

   1.byte              8bit
    2.short         16bit
    3.int             32bit
    4.long          64bit

    short a=59--->00000000 00111011


After understanding these, we can start our focus.

Regarding the first float f1 = 20.5f, and double d1 = 20.5 , why are these two equal?

Binary storage of floating point numbers ( 1 + 8 + 23 )

    float single precision 32bit
    double double precision 64bit
    
    such as: float a = 20.5f;
    20.5 = 2.05 * 10 = 205 * 10 (-1)

    32-bit float:
    1 + 8 + 23 : The first bit is the sign bit , indicating positive and negative numbers . 0 represents a positive number and 1 represents a negative number.
                     The eight middle bits are exponential bits , which indicate how many powers .
                     The last 23 bits are the mantissa , which represents the precision part.
    
   64-bit double:
    1 + 11 + 52 : The first bit is the sign bit, indicating positive and negative numbers. 0 represents a positive number and 1 represents a negative number.
                    The middle 11 digits are exponential digits , which indicate how many powers.
                    The next 52 bits are the mantissa , indicating the precision part.
 


How to convert floating point to binary 

1. First replace the integer part into binary (take 20.5 as an example)

Divide by 2 to take the remainder until the quotient is 0
    20% 2 = 0 20/2 = 10
    10% 2 = 0 10/2 = 5;
    5% 2 = 1 5/2 = 2
    2% 2 = 0 2/2 = 1
    1% 2 = 1 1/2 = 0

   Binary of 20: 10100

2. Replace the decimal part with binary

Conversion method: multiply by 2 to take the integer part until the product is 0
    0.5 * 2 = 1.0 1
    0 * 2 = 0 0
    0.5 binary: 10

    Such as 0.3
    0.3 * 2 = 0.6 0
    0.6 * 2 = 1.2 1
    0.2 * 2 = 0.4 0
    0.4 * 2 = 0.8 0
    0.8 * 2 = 1.6 1
    0.6 * 2 = 1.2 1
    ...

3. Splice the binary part of the integer part and the binary part of the fractional part

The binary of 20.5: 10100.10

4. Represent the obtained binary number with scientific notation

      10100.10 = 1.010010E4
    converts the exponent part in binary representation
    4 to binary: 100
    10100.10 = 1.010010E4 = 1.010010E100 

5. Storage

     Store sign bit: positive number 0 negative number 1


    Store exponent bit: Float type should be based on the original exponent value +127 (01111111): identify positive and negative numbers.
    100 + 01111111 =
    00000100
    01111111
    ---------------------
    10000011

 

    Mantissa part: The integer part of 1.010010E100 is always 1, so this 1 does not need to be stored .
    010010 00000000000000000

    Float
    sign bit 1 bit       exponent bit 8 bit        mantissa bit 23 bit
    0 10000011 010010 00000000000000000

 

6. Value 

   1. Obtain various parts:
    sign bit: 0
    exponent bit: 10000011, need to subtract 127 (01111111), the result is 100, converted to decimal is 4
    mantissa digits: 010010 00000000000000000
    
    2.
    Spliced into scientific notation 1.010010 00000000000000000E100
    
    3. Do not use science Counting method
    10100 .10 00000000000000000
    
    4. Get the integer part 10100 = 20
    
    5. Get the decimal part: 0.1 = 0.5
    
    6. Splice 20.5

And double and float are similar

double:

    Sign bit: positive number 0. negative number 1


    Index position: +1023 (11111111 11111111 11)
        100 + 11111111 11111111 11 = 10000000011 on the basis of the original index


    Sign bit 1 bit       exponent bit 11        mantissa bit 52 bit
    0 10000000011 010010 ( there are 46 0s )


Binary addition and subtraction

    0+0=0
    0+1=1
    1+1=10


Solved float f1 = 20.5f, and double d1 = 20.5, these two are equal, but why is 20.3 not equal? ? ?

We also use the above method to infer:

     Integer part: 20 = 10100


    0.3 to binary: 0 1001 1001 ...
    0.3 * 2 = 0.6 0
    0.6 * 2 = 1.2 1
    0.2 * 2 = 0.4 0
    0.4 * 2-0.8 0
    0.8 * 2 = 1.6 1
    0.6 * 2 = 1.2 1
    0.2 * 2 = 0.4 0
    0.4 * 2 = 0.8 0
    0.8 * 2 = 1.6 1
    ---------------------------
    10100.010011001100 ... .
    
    a float
    sign bit: 0
    exponent: 100 + 01111111 = 10000011
    mantissa bits: 0100010011001100110 .. ( maximum of memory 23 )
        
    Double
    sign bit: 0
    exponent: + 100 = 1111111111 10000000011
    mantissa bits: 0100010011001100110 .... (total 52 )


 I believe that through these, friends should have a certain understanding of floating point numbers in java. For the interview, can we tell at a glance? ? ? Next is the focus:

Under what circumstances, float and double are not equal

    1. Is the binary part of the decimal part converted to an infinite loop ?
    2. The mantissa part exceeds 23 bits, and the float will be lost at this time


This is the end of the disclosure of floating-point numbers in java. Friends with questions, please leave a message! ! !

Published 136 original articles · praised 366 · 30,000+ views

Guess you like

Origin blog.csdn.net/weixin_44364444/article/details/105606265