C programming copysign , copysignf and copysignl 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);

The C++ <cmath> copysign,copysignf and copysignl return the magnitude of ‘x’ but with the sign of ‘y’.

Parameters:
x -The first value.

y -The second argument

Return type
floating point -The magnitude of ‘x’ with the sign of ‘y’.

Some points to note:

i) If ‘x’ is NaN ,’nan’ is returned but with the sign of y.

ii) If x is ±∞ ,∞ is returned with the sign of ‘y’.

Code example

double dx=89.56 , dy=-5 , x1=-4.35 , x2=9 ;

float fx=89.56 , dy=-5 ;

long double ldx=89.56 , ldy=-5 ;

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

printf(“\n%lf”, copysign( x1 , x2) ) ;
printf(“\n%lf”, copysign( INFINITY , -0.00) ) ;

Output in Visual Studio ,

-89.560000
-89.559998
-89.560000
4.350000
-inf


 


copysignf

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

Code example

float x=90.23 , y=-3 ;

printf(“%f”, copysign( x , y) ); //Calls the 1st version
printf(“\n%f”, copysignf( 90.23 , -3) ); Calls the 4th version

Output in Visual Studio,

-90.230000
-90.230000


copysignl

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

Code example

long double x=90.23 , y=-3 ;

printf(“%Lf”, copysign( x , y) ); //Calls the 3rd version
printf(“\n%Lf”, copysignl( 90.23 , -3) ); Calls the 5th version

Output in Visual Studio,

-90.230000
-90.230000