Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

หาค่า sine ของมุมใน Python


ภาพรวม

ใช้ numpy.sin() หา sine ของมุม (เรเดียน) 2π = 360° คืนค่า scalar ถ้า input เป็น scalar

พารามิเตอร์

  • x: มุมในเรเดียน
  • out (optional): ndarray เก็บผลลัพธ์
  • where (optional): condition broadcast

ขั้นตอนการทำงาน

1. นำเข้า NumPy

import numpy as np

2. คำนวณ sine มุมต่างๆ (เรเดียน)

print("\nResult...", np.sin(np.pi/2.))   # 90°
print("\nResult...", np.sin(np.pi/3.))   # 60°
print("\nResult...", np.sin(np.pi/4.))   # 45°
print("\nResult...", np.sin(np.pi/6.))   # 30°
print("\nResult...", np.sin(0))          # 0°

ตัวอย่างโค้ดเต็ม

import numpy as np
print("Get the Trigonometric sine...")
print("\nResult...", np.sin(np.pi/2.))
print("\nResult...", np.sin(np.pi/3.))
print("\nResult...", np.sin(np.pi/4.))
print("\nResult...", np.sin(np.pi/6.))
print("\nResult...", np.sin(0))

ผลลัพธ์

Get the Trigonometric sine...
Result... 1.0
Result... 0.8660254037844386
Result... 0.7071067811865475
Result... 0.49999999999999994
Result... 0.0

คำอธิบาย: sin(90°)=1, sin(60°)=√3/2≈0.866, sin(45°)=√2/2≈0.707, sin(30°)=0.5, sin(0°)=0