为什么最后一个号码错了?

Alv*_*var 9 java math double

为什么输出此代码时只有最后一个数字错误:

public class Test {
    public static void main(String[] args) {
        System.out.println("Hello world");
        System.out.println("I wounder is the sqaure root of (2*3) the 
                           same as the sqaure root of 2 and 3 multiplied.");
        double squareroot0 = Math.pow(3*2, 0.5);
        double squarroot1 = (Math.pow(3, 0.5) * Math.pow(2, 0.5)); 
        System.out.println("So is it the same?");

        System.out.println("is " + squareroot0 + " the equvielant of " + 
                                               squarroot1 + "?");
        if(squareroot0 == squarroot1) {
            System.out.println("Yes number one and number two are 
                                               the same, Congrats!");
        }else {
            System.out.println("No they are not! ");
        }

        System.out.println(squareroot0);
        System.out.println(squarroot1);


    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

Hello world
I wonder is the sqaure root of (2*3) the same as the sqaure 
root of 2 and 3 multiplied.
So is it the same?
is 2.449489742783178 the equvielant of 2.4494897427831783?
No they are not! 
2.449489742783178
2.4494897427831783
Run Code Online (Sandbox Code Playgroud)

在数学上它们是等价的,所以发生了什么?

Math.sqrt()并且Math.pow(,0.5)同样准确.

Sve*_*ach 16

您无法在有限的计算机中表示具有无限精度的数字,因此您需要进行舍入.你看到的是四舍五入的效果.这是浮点数的所有使用所固有的.

强制性链接:每个计算机科学家应该知道的关于浮点运算的内容