Python3 math.copysign() Method
Description
The math.copysign() function returns a float consisting of the magnitude (absolute value) of the first parameter and the sign of the second parameter.
Syntax
math.copysign(x, y)
Parameters
- x: A numeric value representing the magnitude. This parameter is required.
- y: A numeric value representing the sign. This parameter is required.
Return Value
Returns a float value with the magnitude of x and the sign of y.
Example
The following example demonstrates the usage of the copysign() function:
#!/usr/bin/python3
import math
print(math.copysign(4, -1)) # Output: -4.0
print(math.copysign(4, 1)) # Output: 4.0
print(math.copysign(-3, 1)) # Output: 3.0
print(math.copysign(-3, -1)) # Output: -3.0
Output:
-4.0
4.0
3.0
-3.0
YouTip
Comments