在Python中,數學模塊包含許多數學運算,可以使用該模塊輕鬆執行。math.sin()
函數返回作為參數傳遞的值的正弦值。在此函數中傳遞的值應以弧度為單位。
用法: math.sin(x)
參數:
x:要傳遞給sin()的值
返回:返回作為參數傳遞的值的正弦值
代碼1:
# Python code to demonstrate the working of sin()
# importing "math" for mathematical operations
import math
a = math.pi / 6
# returning the value of sine of pi / 6
print ("The value of sine of pi / 6 is:", end ="")
print (math.sin(a))
輸出:
The value of sine of pi/6 is:0.49999999999999994
代碼2:
# Python program showing
# Graphical representation of
# sin() function
import math
import matplotlib.pyplot as plt
in_array = [-3.14159265, -2.57039399, -0.28559933,
0.28559933, 2.57039399, 3.14159265]
out_array = []
for i in range(len(in_array)):
out_array.append(math.sin(in_array[i]))
i += 1
print("in_array:", in_array)
print("\nout_array:", out_array)
# red for numpy.sin()
plt.plot(in_array, out_array, color = 'red', marker = "o")
plt.title("math.sin()")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()
輸出:
in_array: [-3.14159265, -2.57039399, -0.28559933, 0.28559933, 2.57039399, 3.14159265]
out_array: [-3.5897930298416118e-09, -0.5406408168673427, -0.2817325547837714, 0.2817325547837714, 0.5406408168673427, 3.5897930298416118e-09]
相關用法
- 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.sin() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。