C++ next float with numeric_limits / epsilon?
问题 Consider a "normal" real number TREAL x in C++ (not subnormal and not NaN/Infinite) ( TREAL = float , double , long double ) Is the following the good solution to find the previous and next x from a floating-point point of view ? TREAL xprev = (((TREAL)(1.)) - std::numeric_limits<TREAL>::epsilon()) * x; TREAL xnext = (((TREAL)(1.)) + std::numeric_limits<TREAL>::epsilon()) * x; Thank you very much. 回答1: C99 and C++11 have nextafter, nextafterl and nextafterf functions in <math.h> and <cmath> .