Python math.copysign() Method

Python math 模块Python math Module

Python math.copysign(x,y)The method returns a floating-point number based on the absolute value of x and the sign+/-of y.

Python Version: 2.6

Syntax

The syntax of the math.copysign() method is as follows:

math.copysign(x, y)

Parameter description:

  • x-- Required, a number.
  • y-- Required, a number. The returned value will have the sign of this parameter.+/-。

Return Value

Returns a floating-point value consisting of the value of the first parameter and the sign of the second parameter.

Example

The following example returns the value of the first parameter and the sign of the second parameter:

Example

# Import the math package
import math

# Return the value of the first parameter and the sign of the second parameter
print(math.copysign(4, -1))
print(math.copysign(-8, 97.21))
print(math.copysign(-43, -76))

Output result:

-4.0
8.0
-43.0

Python math 模块Python math Module

Other extensions