Accuracy and value range of double and float

(1) Value range

The range of float and double is determined by the number of digits in the exponent.

The exponent bit of float has 8 bits, and the exponent bit of double has 11 bits, which are distributed as follows:

float:

 

1bit (sign bit)

8bits (exponential bit)

23bits (mantissa bits)

double:

 

1bit (sign bit)

11bits (exponential bit)

52bits (mantissa bits)

Therefore, the exponent range of float is -127~+128, and the exponent range of double is -1023~+1024, and the exponent bits are divided in the form of complement. The negative exponent determines what the floating-point number can

The non-zero number with the smallest absolute value expressed; and the positive exponent determines the number with the largest absolute value that the floating-point number can express, which also determines the value range of the floating-point number. The range of float is -2^128 ~ +2^128,

That is -3.40E+38 ~ +3.40E+38; the range of double is -2^1024 ~ +2^1024, which is -1.79E+308 ~ +1.79E+308.

(2) Accuracy

The accuracy of float and double is based on the overall number of digits, not just the decimal part.

The precision of float is 7 to 8 significant digits , 7 digits can be guaranteed, and 8-digit values ​​also exist.

The precision of double is 16~17 significant digits

Look at a piece of code

public class Main {

    public static void main(String[] args) {
        float f6 = 1.000003f;//6位小数位,总共7位
        float f7 = 1.0000003f;//7位小数位,总共8位
        float f8 = 1.00000003f;//8位小数位,总共9位
        float f_8 = 10.000003f;//6位小数位,总共8位
        float f_9 = 10.0000003f;//7位小数位,总共9位
        float f_10 = 10.00000003f;//8位小数位,总共10位

        double d15 = 1.000000000000003;//15位小数位,总共16位
        double d16 = 1.0000000000000003;//16位小数位,总共17位
        double d17 = 1.00000000000000003;//17位小数位,总共18位
        double d_17 = 10.000000000000003;//15位小数位,总共17位
        double d_18 = 10.0000000000000003;//16位小数位,总共18位
        double d_19 = 10.00000000000000003;//17位小数位,总共19位

        System.out.println("结果为false证明 == 校验到了小数点位, 精确度可信");
        System.out.println("float(7位有效,6位小数)    1.000003f  == 1           的结果是:" + (f6==1));
        System.out.println("float(8位有效,7位小数)    1.0000003f == 1           的结果是:" + (f7==1));
        System.out.println("float(9位有效,8位小数)    1.00000003f  == 1         的结果是:" + (f8==1));
        System.out.println("float(8位有效,6位小数)    10.000003f  == 10         的结果是:" + (f_8==10));
        System.out.println("float(9位有效,7位小数)    10.0000003f  == 10        的结果是:" + (f_9==10));
        System.out.println("float(10位有效,8位小数)   10.00000003f  == 10       的结果是:" + (f_10==10));
        System.out.println("------------------");

        System.out.println("double(16位有效,15位小数) 1.000000000000003 == 1    的结果是:" + (d15==1));
        System.out.println("double(17位有效,16位小数) 1.0000000000000003 == 1   的结果是:" + (d16==1));
        System.out.println("double(18位有效,17位小数) 1.00000000000000003 == 1  的结果是:" + (d17==1));
        System.out.println("double(17位有效,15位小数) 10.000000000000003 == 10  的结果是:" + (d_17==10));
        System.out.println("double(18位有效,16位小数) 10.0000000000000003 == 10 的结果是:" + (d_18==10));
        System.out.println("double(19位有效,17位小数) 10.00000000000000003 == 10的结果是:" + (d_19==10));
    }
}

If the result is false, then the decimal point is meaningful, that is, the exact place

From the results, float can be accurate to 8 significant digits, and double to 17 digits.

 

The reason why f1==f2 cannot be used to judge that two numbers are equal is because although f1 and f2 may be two different numbers, they may be wrongly judged that the two numbers are equal due to the limitation of the precision of floating-point numbers!

 

Guess you like

Origin blog.csdn.net/liyang_nash/article/details/104527239