math.copysign()
sturrdlefish2 total contributions
Published Aug 17, 2024
Contribute to Docs
The math.copysign()
function in Python takes two numeric values and returns a float
with the magnitude (absolute value) of the first value and the sign of the second value.
Syntax
math.copysign(x, y)
x
: A numeric value (int
orfloat
) whose magnitude is used in the result.y
: A numeric value (int
orfloat
) whose sign is used in the result.
Example
In the example below, the math.copysign()
function is used to return 10
with the same sign as -5
:
import mathprint(math.copysign(10, -5))
The above code gives the following output:
-10.0
Codebyte Example
The following codebyte example demonstrates how the math.copysign()
function works:
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.