C Math copysign() Function
Example
Give a number the sign of another number:
printf("%f\n", copysign(5.0, -2.0));
printf("%f", copysign(-3.5, 1.0));
Definition and Usage
The copysign() function returns a number with the absolute value of x (the value without its sign) and the sign (plus or minus) of y.
In the example, copysign(5.0, -2.0) returns -5, because y is negative.
The copysign() function is defined in the <math.h> header file.
Syntax
double copysign(double x, double y);
There are also versions for float and long double values: copysignf() and copysignl().
Parameter Values
| Parameter | Description |
|---|---|
| x | Required. The number that gives the value of the result. |
| y | Required. The number that gives the sign of the result. |
Technical Details
| Returns: | A double value with the absolute value of x and the sign of y. |
|---|
Related Pages
Read more about math in our C Math Tutorial.