Python| math.copysign()函数
在Python中,数学模块包含许多数学运算,可以使用该模块轻松执行。 math.copysign(a, b)
函数返回一个浮点数,其大小(绝对值)为 a,但符号为 b。
Syntax: math.copysign(a, b)
Parameter:
a, b: numeric values.
Returns: Return absolute value of a but the sign of b.
代码#1:
# Python code to demonstrate the working of copysign()
# importing "math" for mathematical operations
import math
a = 5.2
b = -25
# returning the copysign
print ("The copysign of 5.2 and -25 is : ", end ="")
print (math.copysign(a, b))
输出:
The copysign of 5.2 and -25 is : -5.2
代码#2:
# Python code to demonstrate the working of factorial()
# importing "math" for mathematical operations
import math
# returning the copysign
print (math.copysign(-13, 25.5))
print (math.copysign(2.87, -15))
输出:
13.0
-2.87