JavaScript
Number Methods
Math.pow / Math.sqrt / Constants
เรียนรู้ Math.pow(), Math.sqrt() และค่าคงที่ Math.PI, Math.E
Math.pow / Math.sqrt / Constants คืออะไร
บนออบเจกต์ `Math` มี method และ constant สำหรับการคำนวณระดับสูง 3 ตัวที่ใช้บ่อย: - **Math.pow()** — ยกกำลัง (power) - **Math.sqrt()** — ถอดรากที่สอง (square root) - **Math.PI, Math.E** — ค่าคงที่ทางคณิตศาสตร์ ทั้งหมดเป็น static members บน `Math` เรียกใช้ได้ทันทีโดยไม่ต้อง `new`
| Method/Constant | หน้าที่ | ตัวอย่าง | ผลลัพธ์ |
|---|---|---|---|
| Math.pow(base, exp) | ยกกำลัง | Math.pow(2, 3) | 8 |
| Math.sqrt(x) | ถอดรากที่สอง | Math.sqrt(16) | 4 |
| Math.PI | ค่า π ≈ 3.14159... | Math.PI | 3.14159... |
| Math.E | ค่า e ≈ 2.71828... | Math.E | 2.71828... |
console.log(Math.pow(2, 10)); // 1024 — 2 ยกกำลัง 10
console.log(Math.sqrt(144)); // 12 — รากที่สองของ 144
console.log(Math.PI); // 3.141592653589793
console.log(Math.E); // 2.718281828459045- ทั้งหมดเป็น static members บน `Math` เรียกตรงได้เลย
- `pow` และ `sqrt` รับ argument เป็น number คืน number
- Constants เป็นค่าคงที่ readonly — ไม่สามารถเปลี่ยนแปลงได้
- ทั้งหมดไม่เปลี่ยนแปลงค่าเดิม (number เป็น primitive — immutable)
Math.pow() — ยกกำลัง
`Math.pow(base, exponent)` คำนวณ `base` ยกกำลัง `exponent` คือ `base` คูณตัวเอง `exponent` ครั้ง เทียบเท่า `base ** exponent` ใน JavaScript สมัยใหม่ แต่ `pow` อ่านง่ายกว่าเมื่อ exponent เป็นตัวแปร
// ยกกำลังจำนวนเต็ม
console.log(Math.pow(2, 3)); // 8 — 2 × 2 × 2
console.log(Math.pow(5, 2)); // 25 — 5²
console.log(Math.pow(10, 0)); // 1 — อะไรยกกำลัง 0 เท่ากับ 1 เสมอ
// เลขลบ
console.log(Math.pow(-2, 3)); // -8 — ลบ×ลบ×ลบ
console.log(Math.pow(-2, 2)); // 4 — ลบ×ลบ
// เศษส่วนเป็น exponent = ถอดราก
console.log(Math.pow(9, 0.5)); // 3 — เท่ากับ √9
console.log(Math.pow(8, 1/3)); // 2 — รากที่สามของ 8
// exponent ติดลบ
console.log(Math.pow(2, -1)); // 0.5 — เท่ากับ 1/2
console.log(Math.pow(10, -2)); // 0.01
// edge cases
console.log(Math.pow(0, 0)); // 1 — กฎพิเศษ
console.log(Math.pow(0, 5)); // 0
console.log(Math.pow("2", "3")); // 8 — string ถูก coerce
console.log(Math.pow("abc", 2)); // NaNใช้ ** operator แทนได้
เช่น `2 ** 10` แทน `Math.pow(2, 10)` — ผลลัพธ์เหมือนกัน แต่ `**` กระชับกว่า ใช้ `Math.pow()` เมื่อ exponent เป็นตัวแปรหรือ expression ซับซ้อน เพราะอ่านง่ายกว่า
const base = 2;
const exp = 10;
// ทั้ง 2 แบบให้ผลเหมือนกัน
console.log(Math.pow(base, exp)); // 1024
console.log(base ** exp); // 1024
// แต่ ** อ่านง่ายกว่าเมื่อค่าคงที่
console.log(2 ** 10); // 1024 — กระชับ- `Math.pow(base, exp)` = ยกกำลัง — `base` ยกกำลัง `exp`
- ใช้เศษส่วนเป็น exponent เพื่อถอดราก: `Math.pow(9, 0.5)` = √9
- สามารถใช้ `**` operator แทนได้ — `2 ** 3` เท่ากับ `Math.pow(2, 3)`
- `Math.pow(0, 0)` ได้ `1` — กฎพิเศษทางคณิตศาสตร์
Math.sqrt() — ถอดรากที่สอง
`Math.sqrt(x)` คืนรากที่สองของ `x` — คือค่าที่เมื่อคูณกับตัวเองแล้วได้ `x` เช่น √9 = 3 เพราะ 3 × 3 = 9 เทียบเท่า `Math.pow(x, 0.5)` แต่ `sqrt` อ่านง่ายกว่า
console.log(Math.sqrt(9)); // 3
console.log(Math.sqrt(16)); // 4
console.log(Math.sqrt(2)); // 1.4142135623730951
console.log(Math.sqrt(0.25)); // 0.5
// edge cases
console.log(Math.sqrt(0)); // 0
console.log(Math.sqrt(1)); // 1
console.log(Math.sqrt(-1)); // NaN ← รากที่สองของเลขลบไม่มีจริง
console.log(Math.sqrt(Infinity));// Infinity
console.log(Math.sqrt("abc")); // NaN
console.log(Math.sqrt(null)); // 0 ← null ถูก coerce เป็น 0การใช้จริง: สูตรระยะทาง Euclidean distance
ระยะทางระหว่างจุด (x₁, y₁) ถึง (x₂, y₂): `Math.sqrt((x2-x1)**2 + (y2-y1)**2)` ตัวอย่าง: (0, 0) ถึง (3, 4): `Math.sqrt(3**2 + 4**2)` = `Math.sqrt(25)` = **5**
// ระยะทางระหว่าง 2 จุด
const x1 = 0, y1 = 0;
const x2 = 3, y2 = 4;
const dx = x2 - x1;
const dy = y2 - y1;
const distance = Math.sqrt(dx ** 2 + dy ** 2);
console.log(distance); // 5
// เปรียบเทียบ: sqrt เทียบ pow
console.log(Math.sqrt(25)); // 5
console.log(Math.pow(25, 0.5)); // 5 — ผลเหมือนกัน- `Math.sqrt(x)` ถอดรากที่สอง — เทียบเท่า `Math.pow(x, 0.5)`
- เลขลบ → `NaN` เพราะรากที่สองของเลขลบไม่มีในจำนวนจริง
- ใช้บ่อยกับสูตรระยะทาง (Euclidean distance), สถิติ (standard deviation)
- `Math.sqrt(0)` ได้ `0`, `Math.sqrt(1)` ได้ `1`
Math Constants — ค่าคงที่ทางคณิตศาสตร์
ออบเจกต์ `Math` เก็บค่าคงที่สำคัญทางคณิตศาสตร์เป็น static properties ค่าที่ใช้บ่อยที่สุดคือ `Math.PI` และ `Math.E` ไม่ต้องจำค่าทศนิยมเอง — ใช้ constant เหล่านี้แทนเพื่อความแม่นยำ
| Constant | ค่า (ประมาณ) | ความหมาย |
|---|---|---|
| Math.PI | 3.14159... | อัตราส่วนระหว่างเส้นรอบวงกับเส้นผ่านศูนย์กลาง |
| Math.E | 2.71828... | ฐานของลอการิทึมธรรมชาติ |
| Math.SQRT2 | 1.41421... | √2 |
| Math.SQRT1_2 | 0.70710... | √(1/2) = 1/√2 |
| Math.LN2 | 0.69314... | ln(2) |
| Math.LN10 | 2.30258... | ln(10) |
| Math.LOG2E | 1.44269... | log₂(e) |
| Math.LOG10E | 0.43429... | log₁₀(e) |
// พื้นที่วงกลม = πr²
const radius = 5;
const area = Math.PI * radius ** 2;
console.log(area); // 78.53981633974483
// เส้นรอบวง = 2πr
const circumference = 2 * Math.PI * radius;
console.log(circumference); // 31.41592653589793
// แปลงองศาเป็นเรเดียน
const degrees = 180;
const radians = degrees * (Math.PI / 180);
console.log(radians); // 3.141592653589793 ≈ π
// Math.E กับการเติบโตแบบ exponential
const growthRate = Math.pow(Math.E, 2); // e²
console.log(growthRate); // 7.38905609893065ค่าคงที่เป็น readonly
ค่าเหล่านี้เป็น properties แบบอ่านอย่างเดียวบน `Math` ไม่ควร (และใน strict mode ไม่ได้) เขียนทับ `Math.PI = 3` จะไม่ทำงาน — ค่าจะไม่เปลี่ยน
- `Math.PI` ใช้บ่อยที่สุด — คำนวณวงกลม, มุม, trigonometry
- `Math.E` ใช้กับการคำนวณ exponential, logarithm
- อย่าจำค่าทศนิยมเอง — ใช้ `Math.PI`, `Math.E` แทนเพื่อความแม่นยำ
- ค่าเหล่านี้เป็น constant ไม่สามารถเปลี่ยนแปลงได้
เปรียบเทียบและเลือกใช้ให้ถูกต้อง
ทั้ง pow, sqrt และ constants ทำงานเกี่ยวกับการคำนวณทางคณิตศาสตร์ แต่ใช้คนละสถานการณ์ เลือกใช้ให้ตรงกับสิ่งที่ต้องการ
| สถานการณ์ | ใช้อะไร | ตัวอย่าง |
|---|---|---|
| ยกกำลัง n | Math.pow() หรือ ** | Math.pow(2, 10) |
| ถอดรากที่สอง | Math.sqrt() | Math.sqrt(144) |
| ถอดรากที่ n | Math.pow(x, 1/n) | Math.pow(27, 1/3) |
| ค่า π | Math.PI | Math.PI * r ** 2 |
| ค่า e | Math.E | Math.pow(Math.E, x) |
- ยกกำลังทั่วไป → **`**` operator** หรือ `Math.pow()`
- ถอดรากที่สอง → **`Math.sqrt()`** — อ่านง่ายกว่า `pow(x, 0.5)`
- ถอดรากที่ n → **`Math.pow(x, 1/n)`** — ใช้เศษส่วนเป็น exponent
- ค่าคงที่ → **`Math.PI`, `Math.E`** — อย่าพิมพ์ค่าเอง