Python math.copysign()
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 (intorfloat) whose magnitude is used in the result.y: A numeric value (intorfloat) 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:
Contribute to Docs
- 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.
Learn Python on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours