np.copysign
Python math.copysign()方法 (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.
math.copysign()方法是数学模块的库方法,用于获取带有另一个数字的符号的数字,它接受两个数字(整数或浮点数)并返回带有符号的第一个数字的浮点值第二个数字。
Note: If anything is passed except the number, the method returns a type error, "TypeError: a float is required".
注意:如果传递了除数字以外的任何内容,则该方法将返回类型错误“ TypeError:需要浮点数”。
Syntax of math.copysign() method:
math.copysign()方法的语法:
math.copysign(x, y)
Parameter(s): x, y – two numbers, x to be converted and y whose sign is required.
参数: x,y –两个数字, x是要转换的, y是需要符号的。
Return value: float – it returns a float value, that is x with the sign of y.
返回值: float-它返回一个浮点值,即x和y的符号。
Example:
例:
Input:
a = 10
b = -2
# function call
print(math.copysign(a, b))
Output:
-10.0
Python代码演示math.copysign()方法的示例 (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
翻译自: https://www.includehelp.com/python/math-copysign-method-with-example.aspx
np.copysign