# Math nexttoward() function in C++

In this tutorial, we will learn about how to use the nexttoward() function in C++. So continue through this article…

### nexttoward() function in C++

The nexttoward() function takes two arguments and returns the next value after the x in the direction of y. It works in a similar way to nextafter() function, but it returns the more accurate value of y.

The syntax of this function is:

`nexttoward(x, y);`

Where

x is the base value.
y is the approximated return value.

Return value: Based on the input data type, it returns the next value of x.

Header File used is:

```#include <cmath>
or
#include <math.h>```

This header file contains a large number of useful mathematical functions such as sqrt() which calculates the square root or exp() etc.

### Write our C++ code for understanding

Now, let us write the code to illustrate the same. Look at our program:

```#include <iostream>
#include <cmath>
using namespace std;

int main()
{
long double x=0.0;        //x= from
double y= -1.0;           //y= to
cout<<"Values of x (from) and y (to) are:"<<x<<", "<<y<<'\n';
cout<<nexttoward(x,y);
return 0;
}```

Now see our output result for the above code:

```Values of x (from) and y (to) are:0, -1
-3.6452e-4951```

Explanation:

In the above C++ program, first of all, we use the header file <cmath> which contains all the mathematical functions in it. Then, we declared the value of x and y with their respective data types. By using the nexttoward() function we find the next value after x in the direction of y and print the output value on the screen.

I hope that this tutorial will help you to solve your problem.