Home » Python

math.copysign() method with example in Python

Python math.copysign() method: Here, we are going to learn about the math.copysign() method with example in Python.
Submitted by IncludeHelp, on April 17, 2019

Python math.copysign() method

math.copysign() method is a library method of 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.

Note: If anything is passed except the number, the method returns a type error, "TypeError: a float is required".

Syntax of math.copysign() method:

    math.copysign(x, y)

Parameter(s): x, y – two numbers, x to be converted and y whose sign is required.

Return value: float – it returns a float value, that is x with the sign of y.

Example:

    Input:
    a = 10
    b = -2

    # function call
    print(math.copysign(a, b))

    Output:
    -10.0

Python code to demonstrate example of math.copysign() method

# Python code demonstrate example of 
# math.copysign() method
import math

# numbers
a = 10
b = -2
print("copysign(a,b): ", math.copysign(a,b))

a = -10
b = -2
print("copysign(a,b): ", math.copysign(a,b))

a = 10.23
b = -2
print("copysign(a,b): ", math.copysign(a,b))

a = -10.23
b = -2.34
print("copysign(a,b): ", math.copysign(a,b))

a = -10
b = 2
print("copysign(a,b): ", math.copysign(a,b))

Output

copysign(a,b):  -10.0
copysign(a,b):  -10.0
copysign(a,b):  -10.23
copysign(a,b):  -10.23
copysign(a,b):  10.0


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.