Python math.copysign() method is used to return a float consisting of the value of the first parameter and the sign of the second parameter.
Visual Representation
Syntax
math.copysign(x, y)
Parameters
x(required): The value whose magnitude(absolute value) is used in the result
y(required): The value whose sign is used in the result.
Return Value
Returns a float value.
It returns a TypeError, if either x or y is not a number.
Example: How to Use math.copysign() Method
import math
print(math.copysign(8, 4))
print(math.copysign(8, -4))
print(math.copysign(-8, 4))
print(math.copysign(-8, -4))
print(math.copysign(10, -2.8))
Output
8.0
-8.0
8.0
-8.0
-10.0
That’s it.
Related posts
Ankit Lathiya is a Master of Computer Application by education and Android and Laravel Developer by profession and one of the authors of this blog. He is also expert in JavaScript and Python development.