go to previous page   go to home page   go to next page

Answer:

No. Even with 64-bit double precision, very small values cannot be represented. To be safe, let us use 1.0E-14.


nearlyEquals

Here is a method that tests if two double precision values are nearly equal:


  public static boolean nearlyEquals( double x, double y )
  {
    final double smallValue = 1.0E-14 ;
    return Math.abs( x - y ) < smallValue ;
  }

The expression

Math.abs( x - y ) < smallValue

evaluates to true or false, so there is no need for an if statement.


QUESTION 20:

Does it matter which parameter is bigger, x or y?


go to previous page   go to home page   go to next page