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

ฟังก์ชัน log() สำหรับจำนวนเชิงซ้อนใน C++

ในบทความนี้ เราจะพูดถึงการทำงาน ไวยากรณ์ และตัวอย่างของฟังก์ชัน log() ใน C++ STL

log() Function คืออะไร?

log() เป็นฟังก์ชันในตัวของ C++ STL อยู่ใน header <complex> คืนค่า natural logarithmic value แบบ complex ของค่า complex ความต่างกับ log() ใน math header คือ log() ของ complex คำนวณ complex logarithmic ส่วน math header คำนวณ normal logarithmic

ไวยากรณ์

template<class T> complex<T> log(const complex<T>& x);

พารามิเตอร์

ฟังก์ชันรับพารามิเตอร์ 1 ตัว คือ complex value ที่ต้องการหา log

ค่าที่คืน

คืนค่า logarithmic value ของ x

ตัวอย่าง

Input: complex<double> C_number(-7.0, 1.0); log(C_number); Output: log of (-7,1) is (1.95601,2.9997)

ตัวอย่างโค้ด 1

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

int main() {
    complex<double> C_number(-7.0, 1.0);
    cout << "log of " << C_number << " is " << log(C_number) << endl;
    return 0;
}

เอาต์พุต

log of (-7,1) is (1.95601,2.9997)

ตัวอย่างโค้ด 2

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

int main() {
    complex<double> C_number(-4.0, -1.0);
    cout << "log of " << C_number << " is " << log(C_number);
    return 0;
}

เอาต์พุต

log of (-4,-1) is (1.41661,-2.89661)