pow() หรือฟังก์ชันยกกำลัง ใช้คำนวณกำลังของตัวเลข ทั่วไปใช้กับจำนวนจริง แต่ใน C++ รองรับจำนวนเชิงซ้อนด้วย
จำนวนเชิงซ้อน (Complex Numbers)
จำนวนเชิงซ้อนมีรูปแบบ A + iB โดย A คือ ส่วนจริง (Real part) และ B คือ ส่วนจินตภาพ (Imaginary part)
ฟังก์ชันสำหรับจำนวนเชิงซ้อนอยู่ใน header <complex> (ไม่ใช่ <complex.h>)
เมธอด pow() รับ 2 พารามิเตอร์: ฐาน (complex number) และ เลขยกกำลัง (exponent) คืนค่าเป็น complex number
ตัวอย่าง
Input − C = 4 + 3i, a = 2
Output − (7, 24) // หมายถึง 7 + 24i
ตัวอย่างโค้ด C++
#include <iostream>
#include <complex>
using namespace std;
int main() {
complex<double> complexnumber(4, 3); // 4 + 3i
cout << "Square of (4+ 3i) = " << pow(complexnumber, 2) << endl;
return 0;
}
ผลลัพธ์
Square of (4+ 3i) = (7,24)
คำอธิบายเพิ่มเติม
การคำนวณ: (4 + 3i)² = 16 + 24i + 9i² = 16 + 24i - 9 = 7 + 24i
ใน C++ ผลลัพธ์แสดงเป็นคู่ลำดับ (real, imaginary)