c++ numeric_limits<double>::epsilon() for values other than 1

449 Views Asked by At

numeric_limits<double>::epsilon() is defined to be the difference between 1.0 and the next value representable by double, but I want to find/calculate the numeric_limits<double>::epsilon() of a value other than 1.0? Is there any way to do this?

1

There are 1 best solutions below

0
bolov On BEST ANSWER

As pointed in the comments, you can use std::nextafter:

float       nextafter ( float from, float to );

Returns the next representable value of from in the direction of to.

float f = /* ... */
float next_after_f = std::nextafter(f, std::numeric_limits<float>::infinity());