mathfloating-pointprecisionepsilon

How to choose epsilon value for floating point?


Since we know that 0.1 + 0.2 != 0.3 due to limited number representation, we need to instead check hat abs(0.1+0.2 - 0.3) < ε. The question is, what ε value should we generally choose for different types? Is it possible to estimate it depending on the number of bits and the number and types of operations that are likely to be performed?


Solution

  • You can estimate the machine epsilon using the algorithm below. You need to multiply this epsilon with the integer value of 1+(log(number)/log(2)). After you have determined this value for all numbers in your equation, you can use error analysis to estimate the epsilon value for a specific calculation.

    epsilon=1.0
    
    while (1.0 + (epsilon/2.0) > 1.0) {
      epsilon = epsilon /2.0     
    }
    //Calculate error using error analysis for a + b
    epsilon_equation=Math.sqrt(2*epsilon*epsilon)
    
    document.write('Epsilon: ' + epsilon_equation+'<br>')
    document.write('Floating point error: ' + Math.abs(0.2 + 0.4 -0.6)+'<br>')
    document.write('Comparison using epsilon: ')
    document.write(Math.abs(0.2 + 0.4 -0.6)<epsilon_equation)

    Following your comment, I have tried the same approach in C# and it seems to work:

    using System;
    
    namespace ConsoleApplication
    {
       
        public class Program
        {
            public static void Main(string[] args)
            {
                double epsilon = 1.0;
    
                while (1.0 + (epsilon/2.0) > 1.0)
                {
                    epsilon = epsilon/2.0;
                }
    
                double epsilon_equation = Math.Sqrt(2*epsilon*epsilon);
    
                Console.WriteLine(Math.Abs(1.0 + 2.0 - 3.0) < Math.Sqrt(3.0 * epsilon_equation * epsilon_equation));
            }
        }
    }