How to choose epsilon value for floating point?

Since we know that 0.1 + 0.2 != 0.3due to the limited number of views, we need to check the hat instead abs(0.1+0.2 - 0.3) < ε. The question is & epsilon; the value that we usually choose for different types? Is it possible to evaluate it depending on the number of bits and the number and types of operations that can be performed?

+4
source share
3 answers

You rate a car using the algorithm below. You need to multiply this epsilon by the integer value 1+ (log (number) / log (2)). Once 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)
Run codeHide result

Following your comment, I 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));
        }
    }
}
+4
source

The base value for epsilon is the difference between 1.0and the next highest represented value. In C ++, this value is available as std::numeric_limits<T>::epsilon().

, , , , . , , , :

double epsilon = std::numeric_limits<double>::epsilon();

// C++ literals and math functions are double by default
bool is_near = abs(0.1+0.2 - 0.3) < 0.3 * (2*epsilon);

:

bool is_approximately_equal(double a, double b) {
  double scale = max(abs(a), abs(b));
  return abs(a - b) < scale * (2*epsilon);
}

, , , , . ( ), . - characteristic_value * epsilon.

+4

: , , . p(x) == 0 p(x) < 0 .. p(x) , . .

. , .

, , googling " " .

, - .

+1
source

Source: https://habr.com/ru/post/1627051/


All Articles