A
A
Alexander Rak2014-05-26 01:48:42
Programming
Alexander Rak, 2014-05-26 01:48:42

How to calculate the error at which two floating point numbers can be considered equal?

When implementing computational geometry algorithms, using Okulov's book "Programming in Algorithms", I ran into the problem of using a "magic" number, or rather, the implementation of the algorithm in the book implied an error of 10 ^ (-3) and I was interested in where this number was taken from.
Equality check algorithm :
IF | a - b | <= eps THEN the numbers are equal
ELSE the numbers are not equal,
where eps is the same error.

Answer the question

In order to leave comments, you need to log in

8 answer(s)
R
Rsa97, 2014-05-26
@Rsa97

If we are talking about abstract mathematics, then it is necessary to consider the representation of a floating type (float, double, long double) in a specific architecture. In most cases, IEEE 754-1985 is used . Let's say for float32 in normalized form, the precision - that is, the unit of the least significant digit of the mantissa - depending on the exponent can be from 1.175494351×10 −38 to 3.402823466×10 38 .
If we are talking about applied problems, then the accuracy is selected based on the process model. For example, when searching for the shortest travel route, an accuracy of ten meters is sufficient, and when parking, at least centimeters are needed.

A
Andrew, 2014-05-26
@OLS

One of the approaches to determining the accuracy is to conduct "double" calculations:
the usual calculation is carried out + in each formula, the error of the final result is calculated (based on the input errors of the operands and the type of operations).
By the time you come to the comparison of two numbers a and b, it will be enough for you to make sure that their intervals, taking into account the errors, do not intersect.
Calculating the number of eps in advance is possible only in very simple cases.
Moreover, to specify it as fixed in the general case is completely wrong.
But what if you calculate a triangle with sides in millimeters?
But what if you have an angle at the top of only half a degree?

M
mrgloom, 2014-07-15
@mrgloom

en.wikipedia.org/wiki/Machine_epsilon
What every scientist should know about floating-po...
www.parashift.com/c++-faq/floating-point-arith.html
probably should use std::numeric_limits::epsilon here

#include <cmath>  /* for std::abs(double) */

inline bool isEqual(double x, double y)
{
  const double epsilon = /* some small number such as 1e-5 */;
  return std::abs(x - y) <= epsilon * std::abs(x);
  // see Knuth section 4.2.2 pages 217-218
}

D
DancingOnWater, 2014-05-26
@DancingOnWater

The error is related to the measurement procedure.
In relation to numerical calculations, this means the following:
1) If two different implementations of the same are similar (there are large pieces of the same code), then the accuracy is determined by the standard of numbers in which the calculations are performed (Rsa97 spoke about this)
2) Otherwise accuracy is evaluated within the framework of the task being solved. I will give an example using the example of solving problems of celestial mechanics.
a) A scientific problem is being solved to simulate the movement of celestial bodies. Then the resulting error = model errors.
b) An applied problem is solved to calculate the position of the satellite. Then the error = the requirements.

T
tsarevfs, 2014-05-26
@tsarevfs

There are techniques to accurately calculate eps. However, it is somewhat more difficult to accurately check a number for equality to 0. Let res be the result of the expression, err be the calculation error. Then |res| + |err| should be < eps. In order to ensure this, one has to use long arithmetic and count tens of thousands of characters in a number. However, in practical problems, the mathematical accuracy of fp-calculations is not often important.

K
KOLANICH, 2014-05-26
@KOLANICH

in all normal programming languages ​​there is a constant, usually called EPS or EPSILON
But in principle, you can calculate
Floating point numbers - this is the mantissa and the exponent,
you need the smallest exponent and the smallest mantissa - this will be the epsilon,
but for this you need to know how they are stored in memory
it is easier to use a constant, especially since it is the same for all PLs that use the "iron" implementation of floating point numbers

A
afiskon, 2014-05-30
@afiskon

It depends on the task and business requirements. For example, you work with money. Floats are not used there, but as a result of taking commissions, multiplying by cross-rates, and so on, a similar problem occurs - there is a number with a large number of decimal places and you need to understand whether they are equal (for example, comparison with zero) or not. Here you can take epsilon 0.005, since for a person if two sums differ by less than half a cent, then they can be considered equal. In some physical measurements, one can start from the error of instruments. Well, and so on.

R
rafuck, 2014-06-29
@rafuck

It is usually more correct to calculate the relative error:
|ab|/max{|a|, |b|} < eps
but you need to separately handle the divide-by-zero case, something like this:
(a == b || fabs(a-b)/max(fabs(a), fabs(b)))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question