C programming nextafter , nextafterf and nextafterl math.h


1 float copysign(float x , float y);
2 double copysign(double x , double y);
3 long double copysign(long double x , long double y);
4 float copysignf(float x , float y);
5 long double copysignl(long double x , long double y);

In C programming the <math.h> nextafter,nextafterf and nextafterl function return 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.

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) The function return ‘y’ if ‘x’ equals ‘y’.

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

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

Code example

double dx=12.34 , dy=14 , x1=10.00 , x2=11.00 ;

float fx=12.34 , dy=14 ;

long double ldx=12.34 , ldy=14 ;

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

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

Output in Visual Studio ,

12.340000
12.340000
12.340000
10.000000
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000

The value returned when ‘x’ is infinity-the 5th output- is a very large number.Note this output is compiler dependent.


 


nextafterf

The ‘nextafterf‘ 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 ‘nextafterf‘ stands for float and it signify the parameter type and return type of the function.

Code example

float x=100.0 , y=100.0 ;

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

Output in Visual Studio,

100.000000
100.000000


nextafterl

The ‘nextafterl‘ 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 ‘nextafterl‘ 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”, nextafter( x , y) ); //Calls the 3rd version
printf(“\n%Lf”, nextafterl( 99.7 , 1000.0) ); Calls the 5th version

Output in Visual Studio,

99.700000
99.700000


Related links

->C programming round,roundf and roundl math.h

->C programming trunc,truncf,truncl-math-h/