C++ Math nexttoward
The nexttoword function represents the next representable value in a specific direction. The nextafter and nexttoward both the functions work similar and return the same value. The only difference occurs in their syntax.
Syntax
Example
float nexttoward(float from, long double to);
double nexttoward(double from, long double to);
long double nexttoward(long double from, long double to);
double nexttoward(integral from, long double to);
Parameter
( from , to ) : These are the floating point values.
Return value
- If 'from' equals to 'to', then it returns the value of 'from'.
- If no error occurs, then the next representable value of 'from' is returned.
Example
Let's see a simple example.
Example
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
long double from=0.0;
double to= -1.0;
cout<<"Values of from and to are:"<<from<<", "<<to<<'\n';
cout<<nexttoward(from,to);
return 0;
}
Output:
Output
Values of from and to are:0, -1
-3.6452e-4951
In the above example, the 'from' and 'to' are of different type. The nexttoward function returns the value i.e -3.6452e-4951.