Python Math copysign() Function: Complete Guide

The math.copysign() is a function in the Standard math Library. Python math module contains several mathematical operations, which can be easily performed using the module. For example, the math function returns the float with the magnitude (absolute value) of a but the sign of b.

Python Math copysign()

Python copysign() is a built-in function of the math library used to get the float number with the sign of another number. The sign can be positive or negative. The copysign() function returns a float value of magnitude from parameter x and the sign (+ve or -ve) from parameter y.


math.copysign(x, y)


Here x is the number to be converted to float, and y is the number whose sign will be copied to x.

Note: To call this function, we first have to import the math library.

For example:

copysign(10,-6): Here answer will be -10.0; it takes the sign from 6 and converts 10 to float.

Return Value

The function takes two numbers, the first number can be integer or float, and the second number is the number is whose sign is to be copied, finally. It returns a float value by taking the sign from another number.


See the following code example.


import math
# Taking two number from user
x = int(input("Enter first number: "))
y = int(input("Enter second number: "))

# Now we will call copysign() function
ans = math.copysign(x, y)

# Printing the answer
print("New value of x is: ", ans)


Enter first number: 15
Enter second number: -10
New value of x is:  -15.0

We have taken two integers in the above program, x, and y. Then we have called copysign() function, which converts x to float and copies the sign of y into x, and finally, we printed it.

See another code example.


import math

def funCopy():
    x = 11
    y = -21

    # implementation of copysign
    z = math.copysign(x, y)

    return z




The copySign() Function Compatibility

Python 2.x – Yes
Python 3.x – Yes


Python math.copysign() method is a library method of the math module. It is used to get a number with the sign of another number; it accepts two numbers (either integers or floats) and returns a float value of the first number with the sign of the second number.

See also

Python math functions

Python math.sqrt()

Python math.floor()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.