จากการดำเนินการทางคณิตศาสตร์อย่างง่ายไปจนถึงการคำนวณที่ซับซ้อน (เช่น การดำเนินการเกี่ยวกับตรีโกณมิติ ลอการิทึม ฯลฯ) ใน python เราอาจต้องใช้โมดูล math()
โมดูลคณิตศาสตร์หลามใช้เพื่อเข้าถึงฟังก์ชันทางคณิตศาสตร์ ฟังก์ชัน math() ทุกวิธีใช้สำหรับวัตถุประเภทจำนวนเต็มหรือจำนวนจริง แต่ไม่ใช่สำหรับจำนวนเชิงซ้อน
ในการใช้ฟังก์ชันนี้ เราต้องนำเข้ามาในโค้ดของเรา
import math
ค่าคงที่
เราใช้ค่าคงที่เหล่านี้ในการคำนวณในหลาม -
| ค่าคงที่ | คำอธิบาย |
|---|---|
| พี่ | คืนค่าของ pi:3.141592 |
| เอ๋อ | คืนค่าของฐานธรรมชาติ e. e คือ 0.718282 |
| เทา | คืนค่า tau เอกภาพ =6.283185 |
| inf | คืนค่าอนันต์ |
| น่าน | ไม่ใช่ประเภทตัวเลข |
ตัวเลขและการแสดงตัวเลข
Python มีฟังก์ชันต่างๆ ที่ใช้แทนตัวเลขในรูปแบบต่างๆ เช่น -
| ฟังก์ชัน | คำอธิบาย |
|---|---|
| ฝ้า(x) | ส่งคืนค่าเพดานซึ่งเป็นค่าที่น้อยที่สุด มากกว่าหรือเท่ากับจำนวน x |
| copysign(x, y) | คืนค่าจำนวน x และคัดลอกเครื่องหมายของ y เป็น x |
| แฟบส์(x) | คืนค่าสัมบูรณ์ของ x |
| แฟคทอเรียล(x) | คืนค่าแฟกทอเรียลของ x โดยที่ x>=0 |
| ชั้น(x) | คืนค่าพื้นซึ่งเป็นจำนวนเต็มที่มากที่สุด น้อยกว่าหรือเท่ากับจำนวน x |
| fsum(ทำซ้ำได้) | ส่งคืนผลรวมขององค์ประกอบในวัตถุที่ทำซ้ำได้ |
| gcd(x,y) | ส่งคืนตัวหารร่วมมากของ x และ y |
| isfinite(x) | ตรวจสอบว่า x ไม่ใช่ทั้งอินฟินิตี้หรือน่านหรือไม่ |
| isinf(x) | ตรวจสอบว่า x เป็นอนันต์หรือไม่ |
| isnan(s) | ตรวจสอบว่า s ไม่ใช่ตัวเลขหรือไม่ |
| ส่วนที่เหลือ(x,y) | หาเศษหลังจากหาร x ด้วย y |
มาเขียนโปรแกรมสาธิตการใช้ฟังก์ชันทางคณิตศาสตร์ข้างต้นกัน -
#Import math library
import math
#Floor and Ceiling
print('The Floor and Ceiling value of 9.45 are:
' + str(math.ceil(9.45)) + ', ' + str(math.floor(9.45)))
#Copysign
x = 94
y = -27
print('The value of x after copying the sign from y is: ' + str(math.copysign(x, y)))
#Absolute
print('Absolute value of -94 and 54 are: ' + str(math.fabs(-94)) + ', ' + str(math.fabs(54)))
#Fsum & gcd
my_list = [12, 9.25, 89, 3.02, -75.23, -7.2, 6.3]
print('Sum of the elements of the list: ' + str(math.fsum(my_list)))
print('The GCD of 24 and 56 : ' + str(math.gcd(24, 48)))
#isnan
x = float('nan')
if math.isnan(x):
print('It is not a number')
x = float('inf')
#isinf
y = 54
if math.isinf(x):
print('It is Infinity')
#x is not a finite number
print(math.isfinite(x))
#y is a finite number
print(math.isfinite(y))
ผลลัพธ์
The Floor and Ceiling value of 9.45 are: 10, 9 The value of x after copying the sign from y is: -94.0 Absolute value of -94 and 54 are: 94.0, 54.0 Sum of the elements of the list: 37.13999999999999 The GCD of 24 and 56 : 24 It is not a number It is Infinity False True
ฟังก์ชันกำลังและลอการิทึม
ฟังก์ชันเหล่านี้ใช้ในการคำนวณกำลังงานและงานที่เกี่ยวข้องกับลอการิทึมต่างๆ ใน python
| ฟังก์ชัน | คำอธิบาย |
|---|---|
| pow(x,y) | ส่งกลับ- x เป็นค่ากำลัง y |
| sqrt(x) | หารากที่สองของ x |
| ประสบการณ์(x) | หา xe โดยที่ e =2.718281 |
| บันทึก(x[,ฐาน]) | ส่งคืนล็อกของ x โดยที่ฐานได้รับ ฐานเริ่มต้นคือ e |
| log2(x) | คืนค่าล็อกของ x โดยที่ฐานคือ 2 |
| log10(x) | คืนค่าล็อกของ x โดยที่ฐานคือ 10 |
ตัวอย่างโปรแกรมสาธิตการใช้งานฟังก์ชันด้านบน
import math
print("The value of 2^5: " + str(math.pow(2, 5)))
print("Square root of 625: " + str(math.sqrt(625)))
print("The value of 5^e: " + str(math.exp(5)))
print("The value of log(625), base 5: " + str(math.log(625, 5)))
print("The value of log(1024), base 10: " + str(math.log10(1024)))
print("The value of log(1024), base 2: " + str(math.log2(1024))) ผลลัพธ์
The value of 2^5: 32.0 Square root of 625: 25.0 The value of 5^e: 148.4131591025766 The value of log(625), base 5: 4.0 The value of log(1024), base 10: 3.010299956639812 The value of log(1024), base 2: 10.0
ฟังก์ชันการแปลงตรีโกณมิติและเชิงมุม
ฟังก์ชันเหล่านี้ใช้ในการคำนวณการดำเนินการเกี่ยวกับตรีโกณมิติต่างๆ -
| ฟังก์ชัน | คำอธิบาย |
|---|---|
| บาป(x) | คืนค่าไซน์ของ x เป็นเรเดียน |
| cos(x) | ส่งกลับค่าโคไซน์ของ x เป็นเรเดียน |
| แทน(x) | ส่งกลับแทนเจนต์ของ x เป็นเรเดียน |
| อาซิน(x) | ส่งกลับค่าผกผันของไซน์ เช่นเดียวกับที่เรามี acos, atan ด้วย |
| องศา(x) | มันแปลงมุม x จากเรเดียนเป็นองศา |
| เรเดียน(x) | มันแปลงมุม x จากองศาเป็นเรเดียน |
ตัวอย่างโปรแกรมสาธิตการใช้งานฟังก์ชันด้านบน
import math
print("The value of sin(45 degree): " + str(math.sin(math.radians(45))))
print('The value of cos(pi): ' + str(math.cos(math.pi)))
print("The value of tan(45 degree): " + str(math.tan(math.pi/2)))
print("the angle of sin(0.95504050560):" + str(math.degrees(math.sin(0.95504050560)))) ผลลัพธ์
The value of sin(45 degree): 0.7071067811865475 The value of cos(pi): -1.0 The value of tan(45 degree): 1.633123935319537e+16 the angle of sin(0.95504050560):46.77267256206895