Double.Epsilon for equality, greater than, less than, less than or equal to, greater than or equal to

I don’t know what they were smoking when they wrote that. Double.Epsilon is the smallest representable non-denormal floating point value that isn’t 0. All you know is that, if there’s a truncation error, it will always be larger than this value. Much larger.

The System.Double type can represent values accurate to up to 15 digits. So a simple first order estimate if a double value x is equal to some constant is to use an epsilon of constant * 1E-15

public static bool AboutEqual(double x, double y) {
    double epsilon = Math.Max(Math.Abs(x), Math.Abs(y)) * 1E-15;
    return Math.Abs(x - y) <= epsilon;
}

You have to watch out though, truncation errors can accumulate. If both x and y are computed values then you have to increase the epsilon.

Leave a Comment