在java中epsilon的值总是相同的吗?
在我的在线课堂上,它说
double x = 0.7, y = 1.0 - 0.1 - 0.1 - 0.1;
System.out.println("x is " + (x));
System.out.println("y is " + (y));
System.out.println("x == y is " + (x == y));
final double EPSILON = 1.0E-6;
System.out.println("Math.abs(x - y) < EPSILON is " + (Math.abs(x - y) < EPSILON));哪一项会产生
x is 0.7
y is 0.7000000000000001
x == y is false
Math.abs(x - y) < EPSILON is true所以我想知道epsilon总是1.0E-6,或者它可能是任何东西?
发布于 2020-12-06 12:07:43
你刚刚把它定义为百万分之一。它可以是(a)对您有用的任何东西,并且(b)可以用所选的格式表示。
你选择的epsilon的大小取决于你正在处理的值的大小;它代表了你对“足够接近”意味着什么的看法,对于接近1的数字来说,并不等同于对于接近1000000000000000或接近0.000000000000001的数字来说足够接近。
https://stackoverflow.com/questions/65164528
复制相似问题