nextafter() Function

The nextafter() function computes the next representable floating-point value following a given value in the direction of another specified value. It is useful for iterating through floating-point numbers or for precision-critical numerical computations. Unlike many functions, it does not require explicit mention of its parameter names when describing its behavior.


Syntax of nextafter()

</>
Copy
#include <math.h>

double nextafter(double x, double y);
float nextafterf(float x, float y);
long double nextafterl(long double x, long double y);

Parameters

ParameterDescription
xBase value from which to find the next representable value.
yThe target direction; the function returns the next value from x toward this value.

Return Value

The function returns the next representable value after the base value in the direction of the target value. If both input values compare equal, the target value is returned.

Additional points to note about the function’s behavior:

  • If the base value is the largest finite number representable, and the result would be infinite or non-representable, an overflow range error occurs.
  • If an overflow range error occurs and math_errhandling has MATH_ERRNO set, the global variable errno is set to ERANGE. Alternatively, if MATH_ERREXCEPT is set, the FE_OVERFLOW floating-point exception is raised.

Examples for nextafter()

Example 1: Moving from a Positive Value Toward a Larger Value

This example demonstrates obtaining the next representable value when moving upward from a positive number.

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    double base = 1.0;
    double nextVal = nextafter(base, 2.0);
    printf("Next value after 1.0 toward 2.0: %.17g\n", nextVal);
    return 0;
}

Explanation:

  1. The program includes the standard input/output and math libraries.
  2. A base value of 1.0 is defined.
  3. The nextafter() function is called to obtain the next representable value after 1.0 in the direction of 2.0.
  4. The resulting value is printed with high precision.

Program Output:

Next value after 1.0 toward 2.0: 1.0000000000000002

Example 2: Approaching a Smaller Value from a Positive Number

This example shows how the function returns the next representable value when moving downward from a positive number.

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    double base = 1.0;
    double nextVal = nextafter(base, 0.0);
    printf("Next value after 1.0 toward 0.0: %.17g\n", nextVal);
    return 0;
}

Explanation:

  1. The standard libraries are included to use I/O and math functions.
  2. A base value of 1.0 is set.
  3. The function is called to find the next representable value moving from 1.0 toward 0.0.
  4. The result is printed with sufficient precision to show the difference.

Program Output:

Next value after 1.0 toward 0.0: 0.99999999999999989

Example 3: Working with Negative Values

This example illustrates how nextafter() functions when dealing with negative numbers, moving further into the negative direction.

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    double base = -1.0;
    double nextVal = nextafter(base, -2.0);
    printf("Next value after -1.0 toward -2.0: %.17g\n", nextVal);
    return 0;
}

Explanation:

  1. The program includes the necessary headers for I/O and mathematical operations.
  2. A negative base value of -1.0 is defined.
  3. The nextafter() function is used to get the next representable value toward -2.0.
  4. The output is printed, showing the immediate next negative value.

Program Output:

Next value after -1.0 toward -2.0: -1.0000000000000002

Example 4: Identical Values Return the Target Value

This example confirms that when both arguments are equal, the function returns the target value without modification.

Program

</>
Copy
#include <stdio.h>
#include <math.h>

int main() {
    double value = 2.0;
    double result = nextafter(value, value);
    printf("Result when both values are equal: %.17g\n", result);
    return 0;
}

Explanation:

  1. The necessary headers are included.
  2. A floating-point value 2.0 is defined for both the base and the target.
  3. The function is called, and since both values are identical, it returns the target value.
  4. The result is printed, verifying that the function behaves as expected when no transition is needed.

Program Output:

Result when both values are equal: 2