Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of boost. Click here for the latest Boost documentation.

Finding the Next Representable Value in a Specific Direction (nextafter)


#include <boost/math/special_functions/next.hpp>

namespace boost{ namespace math{
template <class FPT>
FPT nextafter(FPT val, FPT direction);
}} // namespaces
Description - nextafter

This is an implementation of the nextafter function included in the C99 standard. (It is also effectively an implementation of the C99 'nexttoward' legacy function which differs only having a long double direction, and can generally serve in its place if required).

[Note] Note

The C99 functions must use suffixes f and l to distinguish float and long double versions. C++ uses the template mechanism instead.

Returns the next representable value after x in the direction of y. If x == y then returns x. If x is non-finite then returns the result of a domain_error. If there is no such value in the direction of y then returns an overflow_error.

Examples - nextafter

The two representations using a 32-bit float either side of unity are:

The nearest (exact) representation of 1.F is      1.00000000
nextafter(1.F, 999) is                            1.00000012
nextafter(1/f, -999) is                           0.99999994

The nearest (not exact) representation of 0.1F is 0.100000001
nextafter(0.1F, 10) is                            0.100000009
nextafter(0.1F, 10) is                            0.099999994