C++ Math nextafter
The nextafter function denotes the subsequent representable value in a particular direction.
Suppose we have two numerical values referred to as 'from' and 'to'. In this scenario, the nextafter method is utilized to determine the succeeding value after 'from' in the 'to' direction.
Syntax
float nextafter( float from, float to);
double nextafter( double from, double to);
long double nextafter( long double from, long double to);
promoted nextafter( arithmetic from, arithmetic to);
Note: If any argument is long double, then the return type is long double. If not, the return type is double.
Parameter
( from, to) : These represent decimal numbers.
Return value
- If 'from' equals to 'to', it returns the value of 'from'.
- If no error occurs, the next representable value of 'from' is returned.
Example 1
Let's examine a basic scenario where the values of 'from' and 'to' are the same.
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float from=6.7;
float to=6.7;
cout<<"Values of from and to are:"<<from<<", "<<to<<'\n';
cout<<nextafter(from,to);
return 0;
}
Output:
Values of from and to are:6.7, 6.7
6.7
When 'from' and 'to' have the same value in the given example, the function will output the 'from' value.
Example 2
Let's consider a basic scenario where both 'from' and 'to' variables are of identical data types.
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
double from=0.0;
double to=6.0;
cout<<"Values of from and to are:"<<from<<", "<<to<<'\n';
cout<<nextafter(from,to);
return 0;
}
Output:
Values of from and to are:0, 6
4.94066e-324
In the prior instance, 'from' and 'to' are of identical type but have different values. The nextafter method yields a result of approximately 4.94066e -324.