function
<cmath> <ctgmath>

nexttoward

     double nexttoward  (double x     , long double y);      float nexttowardf (float x      , long double y);long double nexttowardl (long double x, long double y);
     double nexttoward (double x     , long double y);      float nexttoward (float x      , long double y);long double nexttoward (long double x, long double y);     double nexttoward (T x          , long double y);  // additional overloads
Next representable value toward precise value
Returns the next representable value after x in the direction of y.

This function behaves as nextafter, but with a potentially more precise y.

Header <tgmath.h> provides a type-generic macro version of this function.
Additional overloads are provided in this header (<cmath>) for the integral types: These overloads effectively cast x to a double before calculations (defined for T being any integral type).

Parameters

x
Base value.
y
Value toward which the return value is approximated.
If both parameters compare equal, the function returns y (converted to the return type).

Return Value

The next representable value after x in the direction of y.

If x is the largest finite value representable in the type, and the result is infinite or not representable, an overflow range error occurs.

If an overflow range error occurs:
- And math_errhandling has MATH_ERRNO set: the global variable errno is set to ERANGE.
- And math_errhandling has MATH_ERREXCEPT set: FE_OVERFLOW is raised.

Example

1
2
3
4
5
6
7
8
9
10
/* nexttoward example */
#include <stdio.h>      /* printf */
#include <math.h>       /* nexttoward */

int main ()
{
  printf ("first representable value greater than zero: %e\n", nexttoward(0.0,1.0L));
  printf ("first representable value less than zero: %e\n", nexttoward(0.0,-1.0L));
  return 0;
}

Possible output:

first representable value greater than zero: 4.940656e-324
first representable value less than zero: -4.940656e-324


See also