function
<cmath> <ctgmath>

nextafter

     double nextafter  (double x     , double y);
      float nextafterf (float x      , float y);
long double nextafterl (long double x, long double y);
     double nextafter (double x     , double y );
      float nextafter (float x      , float y );
long double nextafter (long double x, long double y );
     double nextafter (Type1 x      , Type2 y);        // additional overloads
Next representable value
Returns the next representable value after x in the direction of y.

The similar function, nexttoward has the same behavior, but it takes a long double as second argument.

Header <tgmath.h> provides a type-generic macro version of this function.
Additional overloads are provided in this header (<cmath>) for other combinations of arithmetic types (Type1 and Type2): These overloads effectively cast its arguments to double before calculations, except if at least one of the arguments is of type long double (in which case both are casted to long double instead).

Parameters

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

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
/* nextafter example */
#include <stdio.h>      /* printf */
#include <math.h>       /* nextafter */

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


Possible output:

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

See also