C programming nexttoward , nexttowardf and nexttowardl math.h

1 float nexttoward(float x , long double y);
2 double nexttoward(double x , long double double y);
3 long double nexttoward(long double x , long double y);
4 float nexttowardf(float x ,long double y);
5 long double nexttowardl(long double x , long double y);

In C programming the <math.h> nextafter,nextafterf and nextafterl functions are same as the nextafter,nextafterf and nextafterl.

Link : C programming nextafter,nextafterf and nextafterl math.h

Parameters:
x -The first value.

y -The second argument

Return type
floating point -The next representable value after ‘x’ in the direction of ‘y’.

Some points to note:

i) Like the nextafter function nexttoward determine the next representable value, in the type of the function, after ‘x’ in the direction of ‘y’ ,where x and y are first converted to the type of the function.The only difference between nextafter and nexttoward is, nexttoward accept the second argument as long double type.

ii) The function return ‘y’ is ‘x’ equals ‘y’.

iii) If x is ±∞ ,a range error may occur or a largest representable value of the type may be returned.

iv) If x is NaN ,’nan’ is returned.

Code example

double dx=12.34 , x1=10.00 ;

float fx=12.34 ;

long double ldx=12.34 , ldy=14 ;

printf(“%lf”, nextafter( dx , ldy) ) ; //calls the 2nd version
printf(“\n%f”, nextafter( fx , ldy) ) ; //calls the 1st version
printf(“\n%Lf”, nextafter( ldx , ldy) ) ;//calls the 3rd version

printf(“\n%lf”, nextafter( x1 , ldy) ) ;
printf(“\n%lf”, nextafter( INFINITY , 90) ) ;

Output in Visual Studio ,

12.540000
12.540000
12.540000
10.000000
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000

The 5ht output is compiler dependent.





nexttowardf

The ‘nexttowardf‘ function is same as the 1st function.This means the parameter and return type of the function is float type.The ‘f‘ character appended to ‘nexttowardf‘ stands for float and it signify the parameter type and return type of the function.

Code example

float x=100.0 ;
long double y=100.0 ;

printf(“%f”, nexttoward( x , y) ); //Calls the 1st version
printf(“\n%f”, nexttowardf( 100.0 , 100.0) ); Calls the 4th version

Output in Visual Studio,

100.000000
100.000000


nexttowardl

The ‘nexttowardl‘ function is same as the 1st function.This means the parameter and return type of the function is long double type.The ‘l‘ character appended to ‘nexttowardl‘ stands for long double and it signify the parameter type and return type of the function.

Code example

long double x=99.7 , y=1000.0 ;

printf(“%Lf”, nexttoward( x , y) ); //Calls the 3rd version
printf(“\n%Lf”, nexttowardl( 99.7 , 1000.0) ); Calls the 5th version

Output in Visual Studio,

99.700000
99.700000


Related links

->C programming nearbyint,nearbyintf and nearbyintl math.h

->C programming ceil,ceilf and ceill math.h