c++numeric-limits

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


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?


Solution

  • 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());