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

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


ภาพรวม

Tangent เทียบเท่ากับ sin(x)/cos(x) แบบ element-wise ใช้ numpy.tan() หา tangent ของมุม (radian) 2π = 360°

พารามิเตอร์

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

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

1. นำเข้า NumPy

import numpy as np

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

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

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

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

ผลลัพธ์

Get the Trigonometric tangent...
Result... 1.633123935319537e+16
Result... 1.7320508075688767
Result... 0.9999999999999999
Result... 0.5773502691896257
Result... 0.0
Result... -1.2246467991473532e-16
Result... 1.2246467991473532e-16

คำอธิบาย:

  • tan(90°) ใหญ่มาก (~1.6e16) เพราะ cos(π/2) ≈ 0
  • tan(60°) = √3 ≈ 1.732
  • tan(45°) = 1
  • tan(30°) = 1/√3 ≈ 0.577
  • tan(0°) = 0
  • tan(180°) และ tan(-180°) ≈ 0 (ค่าเล็กมากจาก floating point)