C Standard Library

C <math.h> - copysign() Function



The C <math.h> copysign() function returns a number with magnitude of first argument and sign of second argument.

Syntax

double copysign (double x, double y);
float copysignf (float x, float y);
long double copysignl (long double x, long double y);

Parameters

x Specify a value providing the magnitude of the result.
y Specify a value providing the sign of the result.

Return Value

Returns a number with magnitude of first argument and sign of second argument.

Example:

In the example below, copysign() function returns a number with magnitude of first argument and sign of second argument.

#include <stdio.h>
#include <math.h>
 
int main (){
  printf("%.2lf\n", copysign(-324.1, 4));
  printf("%.2lf\n", copysign(500, -21));
  printf("%.2lf\n", copysign(-40.2, -15));
  return 0;
}

The output of the above code will be:

324.10
-500.00
-40.20

❮ C <math.h> Library