If your compiler implements the mathematical functions of C99 / C ++ 11, you can use nextafter :
#include <cfloat> // DBL_MAX #include <cmath> // std::nextafter double x = 0.1; // next representable number after x in the direction of DBL_MAX double xPlusSmallest = std::nextafter(x, DBL_MAX);
Even if your compiler does not support it, it probably has an integral attribute for it. (For example, MSVC has _nextafter since 2005. GCC probably implements it as a standard.)
If your compiler does not support it, but Boost is available for you, you can do this:
#include <boost/math/special_functions/next.hpp> // boost::float_next double x = 0.1; // next representable number after x double xPlusSmallest = boost::math::float_next(x);
Which is equivalent to this (C99 emulation):
#include <boost/math/special_functions/next.hpp> // boost::nextafter #include <cfloat> // DBL_MAX double x = 0.1; // next representable number after x in the direction of DBL_MAX double xPlusSmallest = boost::math::nextafter(x, DBL_MAX);
And if none of them work for you, you just need to hack the Boost header and copy it.
GManNickG Apr 15 '12 at 7:13 2012-04-15 07:13
source share