C++ cmath copysign() Function
Example
Give a number the sign of another number:
cout << copysign(5.0, -2.0) << "\n";
cout << 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 <cmath> header file.
Syntax
copysign(double x, double y)
The function also works with float and long double values. Then it returns a value of the same type.
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 Math Tutorial.