浮点数运算

不能做位运算和移位运算

浮点数运算误差

浮点数常常无法精确表示
浮点数0.1在计算机中就无法精确表示,因为十进制的0.1换算成二进制是一个无限循环小数,无论使用float还是double,都只能存储一个0.1的近似值,但是0.5这个浮点数又可以精确地表示
例如

public class Main {
    public static void main(String[] args) {
        double x = 1.0 / 10;
        double y = 1 - 9.0 / 10;
        System.out.println(x);//0.1 
        System.out.println(y);//0.09999999999999998
    }
}

由于浮点数存在运算误差,所以比较两个浮点数是否相等常常会出现错误的结果
正确的比较方法是判断两个浮点数之差的绝对值是否小于一个很小的数

// 比较x和y是否相等,先计算其差的绝对值:
double r = Math.abs(x - y);
// 再判断绝对值是否足够小:
if (r < 0.00001) {
    // 可以认为相等
} else {
    // 不相等
}

类型提升

如果参与运算的两个数其中一个是整型,那么整型可以自动提升到浮点型

public class Main {
    public static void main(String[] args) {
        int n = 5;
        double d = 1.2 + 24.0 / n; // 6.0
        System.out.println(d);
    }
}

复杂的四则运算中,两个整数的运算不会出现自动提升的情况

double d = 1.2 + 24 / 5; // 5.2

计算结果为5.2,原因是编译器计算24 / 5这个子表达式时,按两个整数进行运算,结果仍为整数4

溢出

整数运算在除数为0时会报错,而浮点数运算在除数为0时,不会报错,但会返回几个特殊值

  • NaN表示Not a Number
  • Infinity表示无穷大
  • -Infinity表示负无穷大
double d1 = 0.0 / 0; // NaN
double d2 = 1.0 / 0; // Infinity
double d3 = -1.0 / 0; // -Infinity

强制转型

可以将浮点数强制转型为整数
在转型时,浮点数的小数部分会被丢掉
如果转型后超过了整型能表示的最大范围,将返回整型的最大值

int n1 = (int) 12.3; // 12
int n2 = (int) 12.7; // 12
int n2 = (int) -12.7; // -12
int n3 = (int) (12.7 + 0.5); // 13
int n4 = (int) 1.2e20; // 2147483647

如果要进行四舍五入,可以对浮点数加上0.5再强制转型

public class Main {
    public static void main(String[] args) {
        double d = 2.6;
        int n = (int) (d + 0.5);
        System.out.println(n);//3
    }
}
THE END
最后修改:2020 年 03 月 04 日 19 : 15
本文链接:https://www.j000e.com/%E5%9F%BA%E7%A1%80/floatingpointoperations.html
版权声明:本文『[浮点数运算][误差][判断相等][强制转型][四舍五入]』为『Joe』原创。著作权归作者所有。
转载说明:[浮点数运算][误差][判断相等][强制转型][四舍五入] || Joe's Blog』转载许可类型见文末右下角标识。允许规范转载时,转载文章需注明原文出处及地址。
Last modification:March 4, 2020