ในบทความนี้ เราจะพูดถึงการทำงาน ไวยากรณ์ และตัวอย่างของฟังก์ชัน imag() ใน C++
imag() คืออะไร
ฟังก์ชัน imag() เป็นฟังก์ชัน inbuilt ใน C++ STL ซึ่งกำหนดไว้ในไฟล์ส่วนหัว
จำนวนเชิงซ้อนคือจำนวนที่สร้างโดยผลรวมของจำนวนจริงกับจำนวนจินตภาพ จำนวนจริงคือจำนวนใดๆ ยกเว้นจำนวนอนันต์และจำนวนจินตภาพ
จำนวนจินตภาพคือตัวเลขที่มีกำลังสองเป็นจำนวนลบ ฟังก์ชันส่งคืนส่วนจินตภาพ ส่วนจินตภาพ ซึ่งเป็นปัจจัยในการคูณหน่วยจินตภาพ
ไวยากรณ์
Template <class T> T imag(const complex<T>& num);
พารามิเตอร์
ฟังก์ชันยอมรับพารามิเตอร์ต่อไปนี้ −
-
จำนวน − นี่คือจำนวนเชิงซ้อนที่กำหนด
คืนค่า
ฟังก์ชันนี้จะคืนค่าส่วนจินตภาพของจำนวน
ป้อนข้อมูล
complex<double> img(2.2,3.4); imag(img);
ผลผลิต
3.4
ตัวอย่าง
#include <bits/stdc++.h>
using namespace std;
int main(){
//complex number = (a + ib)
complex<double> img(2.2,3.4);
cout<<"The complex number is: "<<img;
cout<<"\nThe Imaginary part of the complex number is: "<<imag(img) << endl;
return 0;
} ผลลัพธ์
หากเราเรียกใช้โค้ดด้านบน มันจะสร้างผลลัพธ์ต่อไปนี้ -
The complex number is: (2.2,3.4) The Imaginary part of the complex number is: 3.4
ตัวอย่าง
#include <bits/stdc++.h>
using namespace std;
int main(){
//complex number = (a + ib)
complex<double> img(32,12);
cout<<"The complex number is: "<<img;
cout<<"\nThe Imaginary part of the complex number is: "<<imag(img) << endl;
return 0;
} ผลลัพธ์
หากเราเรียกใช้โค้ดด้านบน มันจะสร้างผลลัพธ์ต่อไปนี้ -
The complex number is: (32,12) The Imaginary part of the complex number is: 12