The nexttowardf()
calculates the next value after a number x
in the direction of another number y
in a precise manner. nexttowardf()
is similar to the nextafter()
function.
nexttowardf()
is defined in the <math.h>
library, so you must include the <math.h>
library at the beginning of your program as shown below:
#include<math.h>
The nexttowardf()
function is defined in the <math.h>
header files as shown below:
float nexttowardf( float x, long double y );
The nexttowardf()
function takes two parameters, x
and y
, of type float
and long double
respectively.
x
is defined as ‘from’ and y
as ‘to’ because the function returns the next floating-point number from x
and in the direction of y
.
Moreover, the return values vary for some distinct values of x
and y
:
nexttowardf()
function returns .x
or y
are Not a Number (NaN)
, then the function returns NaN
.The return value never depends on the current rounding mode in any case.
The following errors can be returned if the nexttowardf()
function is not executed successfully:
HUGE_VALF
macro is returned.x
value, the FE_INEXACT
and FE_OVERFLOW
errors are returned due to overflow.FE_INEXACT
and FE_UNDERFLOW
flags are raised.Below is an example of how you can use the nexttowardf()
function. It calculates the next floating-point number after in the direction of (positive numbers).
#include<stdio.h>#include<math.h>int main() {float from = 0;long double to = 1;float result = nexttowardf(from, to);printf("the next number from 0 in the direction of 1 is: %f\n", result);return 0;}
Free Resources