Today, I was looking through some C++ code (written by somebody else) and found this section:
double someValue = ...
if (someValue < std::numeric_limits&
The difference between X
and the next value of X
varies according to X
.
epsilon()
is only the difference between 1
and the next value of 1
.
The difference between 0
and the next value of 0
is not epsilon()
.
Instead you can use std::nextafter
to compare a double value with 0
as the following:
bool same(double a, double b)
{
return std::nextafter(a, std::numeric_limits::lowest()) <= b
&& std::nextafter(a, std::numeric_limits::max()) >= b;
}
double someValue = ...
if (same (someValue, 0.0)) {
someValue = 0.0;
}