Math Nextafter Function - C++ Programming Tutorial
C++ Course / Math Functions / Math Nextafter Function

Math Nextafter Function

BLUF: Mastering Math Nextafter Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Math Nextafter Function

C++ is renowned for its efficiency. Learn how Math Nextafter Function enables low-level control and high-performance computing in the tutorial below.

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

Example

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.

Example

#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:

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.

Example

#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:

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.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience