Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

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

Synopsis
#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.

[Warning] Warning

The template parameter FTP must be a floating-point type. An integer type, for example, will produce an unhelpful error message.

[Tip] Tip

Nearly always, you just want the next or prior representable value, so instead use float_next or float_prior below.

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

PrevUpHomeNext