C++ Standard Library C++ STL Library

C++ <cmath> - nexttoward() Function



The C++ <cmath> 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 nexttoward (float x, long double y);
long double nexttoward (long double x, long double y);
double nexttoward (T 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 <iostream>
#include <cmath>

using namespace std;

int main (){

  cout<<"nexttoward(0.0, 1.0): "<<nexttoward(0.0, 1.0)<<"\n";
  cout<<"nexttoward(0.0, -1.0): "<<nexttoward(0.0, -1.0)<<"\n";
  return 0;
}

The output of the above code will be:

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

❮ C++ <cmath> Library