C++ Standard Library C++ STL Library

C++ <cmath> - nextafter() Function



The C++ <cmath> nextafter() function returns the next representable value after x in the direction of y. The nexttoward() function is similar to this function, except it takes a long double as second argument.

Syntax

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

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 nextafter() function.

#include <iostream>
#include <cmath>

using namespace std;

int main (){

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

The output of the above code will be:

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

❮ C++ <cmath> Library