Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

imag() ใน C++: ดึงส่วนจินตภาพของ Complex Number

imag() คืออะไร?

imag() ฟังก์ชันใน <complex> คืนค่า **ส่วนจินตภาพ (Imaginary Part)** ของ complex number

Complex number: z = a + bi โดย a = real part, b = imaginary part

Syntax

template <class T> T imag(const complex<T>& z);

Parameters

  • z - complex number

Return Value

  • ค่า b จาก a + bi (ชนิด T เดียวกับ complex)

ตัวอย่าง

#include <bits/stdc++.h>
using namespace std;

int main() {
    complex<double> z1(2.2, 3.4);
    complex<double> z2(32, 12);
    complex<double> z3(-5, -7);
    
    cout << "z1 = " << z1 << ", imag = " << imag(z1) << "\n"; // (2.2,3.4), 3.4
    cout << "z2 = " << z2 << ", imag = " << imag(z2) << "\n"; // (32,12), 12
    cout << "z3 = " << z3 << ", imag = " << imag(z3) << "\n"; // (-5,-7), -7
    
    // เข้าถึงผ่าน member function ด้วย
    cout << "z1.imag() = " << z1.imag() << "\n"; // 3.4
    
    return 0;
}

Output

z1 = (2.2,3.4), imag = 3.4
z2 = (32,12), imag = 12
z3 = (-5,-7), imag = -7
z1.imag() = 3.4

ฟังก์ชันที่เกี่ยวข้อง

  • real(z) / z.real() - ส่วนจริง (a)
  • abs(z) - มอดุลัส |z| = sqrt(a²+b²)
  • arg(z) - มุมข้าง (phase angle) ในรاديアン
  • norm(z) - a²+b² (squared magnitude)
  • conj(z) - Complex conjugate a - bi
  • polar(r, theta) - สร้าง complex จาก polar coordinates

การใช้งานจริง

  • Digital Signal Processing (FFT, filters)
  • Electrical Engineering (impedance, phasors)
  • Physics simulations
  • Computer Graphics (rotations, transformations)

สรุป

imag(z) ดึงส่วนจินตภาพของ complex number ใช้ร่วมกับ real(), abs(), arg() ในงานวิศวกรรม/วิทยาศาสตร์