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

Math Nexttoward Function

BLUF: Mastering Math Nexttoward 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 Nexttoward Function

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

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

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

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.

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 previous instance, the 'start' and 'end' points belong to distinct categories. The nexttoward method yields the result which is -3.6452e-4951.

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