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 (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:
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
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours