javaepsilon

What is the value of epsilon?


Is the value of epsilon in java always the same?

In my lesson in my online class it says

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));

which yields

x is 0.7
y is 0.7000000000000001
x == y is false
Math.abs(x - y) < EPSILON is true

So I was just wondering is epsilon always 1.0E-6 or can it be anything?


Solution

  • You just defined it as one millionth. It could be anything that (a) is useful to you, and (b) is representable in the chosen format.

    The magnitude of your chosen epsilon depends on the magnitude of the values you're working with; it represents your view of what 'close enough' means, and close enough for numbers near 1 is not the same as close enough for numbers near 1000000000000000 nor near 0.000000000000001.