How to find the nearest next / previous double value (numeric_limits :: epsilon for a given number)

The name is pretty clear, the input is given a double value, and I want to add / subtract the smallest possible amount.

+13
c ++ double floating-point
Apr 15 2018-12-12T00:
source share
4 answers

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.

+19
Apr 15 '12 at 7:13
source share

Here's a very dirty trick that is actually not legal and only works if your platform uses IEEE754 floats: the binary representation of the float is ordered in the same way as the float value, so you can increase the binary representation:

 double x = 1.25; uint64_t * const p = reinterpret_cast<uint64_t*>(&x); ++*p; // undefined behaviour! but it gets the next value // now x has the next value 

You can achieve the same effect completely legally by running a regular binary copy of gymnastics to get the correct uint64_t value. Remember to also check for zero, infinity, and NaN.

+7
Apr 15 2018-12-12T00:
source share

What about:

 x += fabs(x) * std::numeric_limits<double>::epsilon(); 
-2
Apr 15 2018-12-12T00:
source share
 #define FLT_MIN 1.175494351e-38F /* min positive value */ #define FLT_MAX 3.402823466e+38F /* max value */ #define DBL_MIN 2.2250738585072014e-308 /* min positive value */ #define DBL_MAX 1.7976931348623158e+308 /* max value */ 

http://xona.com/2006/07/26.html

-four
Apr 15 2018-12-12T00:
source share



All Articles