C++ Math nextafter()

Last Updated : 21 May 2026

Math nextafter() Function

In C++, the nextafter() function is defined in the <cmath> or <math.h> header file and is used to return the next representable floating-point value of a number in the direction of another number. It is useful for working with floating-point precision and low-level numerical computations.

If the values of from and to are equal, the function simply returns the value of from. Otherwise, it returns the next representable floating-point value of from moving toward to.

Syntax

It has the following syntax.

Note: If any argument is long double, then the return type is long double. If not, the return type is double.

Parameters

( from, to): These are the floating point values.

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.

Examples of the List nextafter() Function

Here, we are going to discuss several examples to demonstrate the List nextafter() Function Function.

Example 1: Use nextafter() When Both Values are Equal

This example demonstrates how the nextafter() function behaves when the values of from and to are equal.

Output:

Values of from and to are:6.7, 6.7
6.7

Explanation:

In the above example, values of 'from' and 'to' are equal. Therefore, the function returns the value of 'from'.

Example 2: Use nextafter() When Both Values are Different

This example demonstrates how the nextafter() function returns the next representable floating-point value when from and to are different.

Output:

Values of from and to are:0, 6
4.94066e-324

Explanation:

In the above example, 'from' and 'to' are of same type but are unequal. The nextafter() function returns the value i.e 4.94066e-324

Example 3: Use nextafter() with Negative Values

This example demonstrates how the nextafter() function finds the next representable value when moving from one negative number toward another negative number.

Output:

Values of from and to are : -2, -5
-2