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()
#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
Parameter | Description |
---|---|
x | Base value from which to find the next representable value. |
y | The 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
hasMATH_ERRNO
set, the global variableerrno
is set toERANGE
. Alternatively, ifMATH_ERREXCEPT
is set, theFE_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
#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:
- The program includes the standard input/output and math libraries.
- A base value of
1.0
is defined. - The
nextafter()
function is called to obtain the next representable value after1.0
in the direction of2.0
. - 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
#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:
- The standard libraries are included to use I/O and math functions.
- A base value of
1.0
is set. - The function is called to find the next representable value moving from
1.0
toward0.0
. - 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
#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:
- The program includes the necessary headers for I/O and mathematical operations.
- A negative base value of
-1.0
is defined. - The
nextafter()
function is used to get the next representable value toward-2.0
. - 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
#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:
- The necessary headers are included.
- A floating-point value
2.0
is defined for both the base and the target. - The function is called, and since both values are identical, it returns the target value.
- 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