...one of the most highly
regarded and expertly designed C++ library projects in the
world.

— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards

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

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

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 | |
---|---|

The C99 functions must use suffixes f and l to distinguish |

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 | |
---|---|

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

Tip | |
---|---|

Nearly always, you just want the next or prior representable value, so
instead use |

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