What is math.copysign() in Python?

Python is a high-level programming language that provides functionalities for several operations.

The math module comes with mathematical methods and constraints, which make for straightforward calculations.

One of these is math.copysign(), which takes in two parameters and returns the first parameter’s magnitude with the second parameter’s sign.

Syntax


math.copysign(x, y)

Parameters

  • x: An integer value that is to be converted. This parameter is required.

  • y: An integer value whose sign will be needed for x. This parameter is required.

Return value

This function returns a float value whose magnitude is that of x and whose sign is that of y.

Code

#import library
import math
r1 = math.copysign(15,-6)
print("15,-6: ", r1)
r2 = math.copysign(0,-4)
print("0,-4: ", r1)
r1 = math.copysign(-4,-13)
print("-4,-13: ", r1)
r1 = math.copysign(-10,6)
print("-10,6: ", r1)
r1 = math.copysign(-6,0)
print("-6, 0: ", r1)
r1 = math.copysign(6,0)
print("6, 0: ", r1)