3 minute read

When using floating point variables, there’s always the danger that a certain number cannot be represented exactly. Therefore, oftentimes epsilon values need to be used when checking or comparing results of floating point computations. But which epsilon to use? The answer is: It depends. More precisely, it depends on the range of numbers that you are using for your computations, which is where this small online tool comes into play: The Floating Point Epsilon Calculator will tell you the largest difference between two consecutive floating point values within a given range of numbers—use that as your epsilon! Furthermore, the precision across the given range of numbers is plotted.

Operating instructions: Select the data type, enter lower and upper bounds of the floating point range of interest, then hit the [Calculate] button and await the resulting epsilon and precision chart.

Alternatively, select one of the predefined ranges:
 

Calculate precision of
between:
and:
Result:
constexpr float epsilon = ?;

How It Works

The calculations are performed in C++ via the fantastic and invaluable “Compiler Explorer” at godbolt.org. The calculations use std::nextafter to get the smallest adjacent floating point value to a given reference value => this is the proposed epsilon. For a range of values, the proposed epsilon value can always w.l.o.g. be calculated by taking the maximum of the lower bound’s epsilon and the upper bound’s epsilon.

The following C++ code is used to calculate the floating point precision around a given reference value:

#include <limits>
#include <cmath>
#include <algorithm>
	
template <typename T>
T precision_for(T reference) {
    T more = std::nextafter(reference,  std::numeric_limits<T>::infinity());
    T less = std::nextafter(reference, -std::numeric_limits<T>::infinity());
    T precision = std::max(more - reference, reference - less);
    return precision;
}

Further Information

Other sources have done a great job of describing floating point numbers and their varying precision. Please refer to some of the following great resources:

Comments