在Python中,數學模塊包含許多數學運算,可以使用該模塊輕鬆執行。math.copysign(a, b)
函數返回一個浮點數,其浮點值為a,但符號為b。
用法: math.copysign(a, b) 參數: a, b: numeric values. 返回: 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
相關用法
- Python now()用法及代碼示例
- Python cmp()用法及代碼示例
- Python map()用法及代碼示例
- Python ord()用法及代碼示例
- Python int()用法及代碼示例
- Python dir()用法及代碼示例
- Python hex()用法及代碼示例
- Python sum()用法及代碼示例
- Python tell()用法及代碼示例
- Python id()用法及代碼示例
- Python oct()用法及代碼示例
注:本文由純淨天空篩選整理自Shivam_k大神的英文原創作品 Python | math.copysign() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。