C Standard Library

C <math.h> - nexttoward() Function



The C <math.h> nexttoward() function returns the next representable value after x in the direction of y. The nextafter() function is similar to this function, but with a potentially more precise second argument.

Syntax

double nexttoward  (double x, long double y);
float nexttowardf (float x, long double y);
long double nexttowardl (long double x, long double y);                    

Parameters

x Specify the base value.
y Specify the value toward which the return value is approximated.

Return Value

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

Example:

The example below shows the usage of nexttoward() function.

#include <stdio.h>
#include <math.h>

int main (){

  printf("nexttoward(0.0, 1.0): %e\n", nexttoward(0.0, 1.0));
  printf("nexttoward(0.0, -1.0): %e\n", nexttoward(0.0, -1.0));
  return 0;
}

The output of the above code will be:

nexttoward(0.0, 1.0): 4.940656e-324
nexttoward(0.0, -1.0): -4.940656e-324

❮ C <math.h> Library