โมดูล math ให้ Mathematical Functions สำหรับ Integer/Float (ไม่รองรับ Complex -> ใช้ cmath)
import math
Constants
| Constant | ค่า | คำอธิบาย |
math.pi | 3.141592653589793 | π |
math.e | 2.718281828459045 | Natural Base |
math.tau | 6.283185307179586 | 2π (Python 3.6+) |
math.inf | inf | Positive Infinity |
math.nan | nan | Not a Number |
Number Theory & Representation
| Function | คำอธิบาย |
ceil(x) | ปัดขึ้น (Smallest Integer ≥ x) |
floor(x) | ปัดลง (Largest Integer ≤ x) |
trunc(x) | ตัดทศนิยม (Toward Zero) |
fabs(x) | Absolute Value (Float) |
factorial(x) | Factorial (x ≥ 0 Integer) |
gcd(a, b) | Greatest Common Divisor |
lcm(a, b) | Least Common Multiple (3.9+) |
fsum(iterable) | Sum แบบแม่นยำ (Precision) |
prod(iterable) | Product (3.8+) |
isqrt(n) | Integer Square Root (3.8+) |
copysign(x, y) | x ด้วย Sign ของ y |
remainder(x, y) | IEEE 754 Remainder |
Special Value Checks
isfinite(x) | ไม่ใช่ Inf/NaN |
isinf(x) | เป็น Infinity |
isnan(x) | เป็น NaN |
isclose(a, b, rel_tol=1e-9, abs_tol=0) | เปรียบเทียบ Float ใกล้เคียง |
Power & Logarithmic
| Function | คำอธิบาย |
pow(x, y) | x^y (Float) |
sqrt(x) | √x |
exp(x) | e^x |
expm1(x) | e^x - 1 (แม่นยำสำหรับ x ใกล้ 0) |
log(x[, base]) | Log Base e (Default) หรือ Base ที่กำหนด |
log2(x) | Log Base 2 |
log10(x) | Log Base 10 |
log1p(x) | ln(1+x) (แม่นยำสำหรับ x ใกล้ 0) |
Trigonometric & Angular
| Function | คำอธิบาย |
sin(x), cos(x), tan(x) | Input เป็น Radian |
asin(x), acos(x), atan(x) | Inverse (Output Radian) |
atan2(y, x) | Angle จาก (x,y) Quadrant ถูกต้อง |
hypot(x, y) | √(x²+y²) - ระยะทาง Euclidean |
degrees(rad) | Radian -> Degree |
radians(deg) | Degree -> Radian |
Hyperbolic
sinh/cosh/tanh | Hyperbolic Functions |
asinh/acosh/atanh | Inverse Hyperbolic |
โค้ดตัวอย่าง
import math
# Basic
print(f"π = {math.pi}, e = {math.e}, τ = {math.tau}")
print(f"ceil(23.56) = {math.ceil(23.56)}, floor(23.56) = {math.floor(23.56)}")
print(f"copysign(10, -15) = {math.copysign(10, -15)}")
print(f"fabs(-96) = {math.fabs(-96)}")
print(f"factorial(5) = {math.factorial(5)}")
print(f"gcd(24, 56) = {math.gcd(24, 56)}")
print(f"fsum([12, 4.25, 89, 3.02, -65.23]) = {math.fsum([12, 4.25, 89, 3.02, -65.23])}")
# Power/Log
print(f"5^8 = {math.pow(5, 8)}")
print(f"√400 = {math.sqrt(400)}")
print(f"e^5 = {math.exp(5)}")
print(f"log(625, 5) = {math.log(625, 5)}")
print(f"log2(1024) = {math.log2(1024)}")
print(f"log10(1024) = {math.log10(1024)}")
# Trigonometric
print(f"sin(60°) = {math.sin(math.radians(60))}")
print(f"cos(π) = {math.cos(math.pi)}")
print(f"atan2(1, 1) = {math.atan2(1, 1)} rad = {math.degrees(math.atan2(1, 1))}°")
print(f"hypot(3, 4) = {math.hypot(3, 4)}")
# Special Values
print(f"isnan(nan) = {math.isnan(float('nan'))}")
print(f"isinf(inf) = {math.isinf(float('inf'))}")
print(f"isfinite(45) = {math.isfinite(45)}")
print(f"isclose(0.1+0.2, 0.3) = {math.isclose(0.1+0.2, 0.3)}")
New in Python 3.11+
math.exp2(x) - 2^x
math.log2p(x) - log2(1+x)
math.cbrt(x) - Cube Root
math.sumprod(iter1, iter2) - Sum of Products