The difference between X and the next value of X changes in accordance with X
DBL_EPSILON is just the difference between 1 and the next value of 1 .
You can use std::nextafter to test two double with epsilon difference:
bool nearly_equal(double a, double b) { return std::nextafter(a, std::numeric_limits<double>::lowest()) <= b && std::nextafter(a, std::numeric_limits<double>::max()) >= b; }
If you want to test two double with a difference of * epsilon coefficient, you can use:
bool nearly_equal(double a, double b, int factor ) { double min_a = a - (a - std::nextafter(a, std::numeric_limits<double>::lowest())) * factor; double max_a = a + (std::nextafter(a, std::numeric_limits<double>::max()) - a) * factor; return min_a <= b && max_a >= b; }
source share