nextafter() – nextafterl() – nexttoward() – nexttowardl() — Calculate the Next Representable Floating-Point Value
Format
#include <math.h>
double nextafter(double x, double y);
long double nextafterl(long double x, long double y);
double nexttoward(double x, long double y);
long double nexttowardl(long double x, long double y);
Language Level
ANSI
Threadsafe
Yes
Description
The nextafter()
, nextafterl()
, nexttoward()
,
and nexttowardl()
functions
calculate the next representable value after x in the direction of
y.
Return Value
The nextafter()
, nextafterl()
, nexttoward()
,
and nexttowardl()
functions
return the next representable value after x in the direction of y.
If x is equal to y, they return y. If x or y is NaN (Not a Number),
NaN is returned and errno is set to EDOM. If x is the largest finite
value and the result is infinite or not representable, HUGE_VAL is
returned and errno is set to ERANGE.
Example
This example converts a floating-point
value to the next greater representable value and next smaller representable
value. It prints out the converted values.
#include <stdio.h>
#include <math.h>
int main(void)
{
double x, y;
long double ld;
x = nextafter(1.234567899, 10.0);
printf("nextafter 1.234567899 is %#19.17g\n" x);
ld = nextafterl(1.234567899, -10.0);
printf("nextafterl 1.234567899 is %#19.17g\n" ld);
x = nexttoward(1.234567899, 10.0);
printf("nexttoward 1.234567899 is %#19.17g\n" x);
ld = nexttowardl(1.234567899, -10.0);
printf("nexttowardl 1.234567899 is %#19.17g\n" ld);
}
/***************** Output should be similar to: *****************
nextafter 1.234567899 is 1.2345678990000002
nextafterl 1.234567899 is 1.2345678989999997
nexttoward 1.234567899 is 1.2345678990000002
nexttowardl 1.234567899 is 1.2345678989999997
*/