C++ Math nexttoward
The nexttoword function denotes the subsequent representable value in a particular direction. Both nextafter and nexttoward functions operate similarly and yield identical results. The discrepancy lies solely in their syntax.
Syntax
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
The values inside the parentheses represent floating-point numbers.
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.
#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:
Values of from and to are:0, -1
-3.6452e-4951
In the previous instance, the 'start' and 'end' points belong to distinct categories. The nexttoward method yields the result which is -3.6452e-4951.