ในบทความนี้ เราจะพูดถึงการทำงาน ไวยากรณ์ และตัวอย่างของฟังก์ชัน tan() สำหรับจำนวนเชิงซ้อนใน C++ STL
tan() สำหรับจำนวนเชิงซ้อน เป็นฟังก์ชันใน header <complex> ใช้หา tangent ของจำนวนเชิงซ้อนที่เกี่ยวข้อง เป็นเวอร์ชัน complex ของ tan() ใน <cmath>
จำนวนเชิงซ้อนคืออะไร?
จำนวนเชิงซ้อนเป็นการรวมของจำนวนจริงและจำนวนจินตาการ เขียนในรูป a+bi โดย a, b เป็นจำนวนจริง และ i เป็นจำนวนจินตาการ
ไวยากรณ์
template<class T> complex<T> tan(const complex<T>& num);
พารามิเตอร์
ฟังก์ชันรับพารามิเตอร์ 1 ตัว คือ num ชนิด complex value
ค่าที่คืน
คืนค่า tangent ของ num
ตัวอย่าง
Input: complex<double> num(0.0, 1.0); tan(num); Output: (0, 1.55741)
ตัวอย่างโค้ด 1
#include <iostream>
#include <complex>
using namespace std;
int main() {
complex<double> tan_num(9.1, 2.6);
cout << "tan of " << tan_num << " is " << tan(tan_num);
return 0;
}
เอาต์พุต
tan of (9.1, 2.6) is (-0.00661488, 0.99123)
ตัวอย่างโค้ด 2
#include <iostream>
#include <complex>
using namespace std;
int main() {
complex<double> tan_num(1.0, 0.0);
cout << "tan of " << tan_num << " is " << tan(tan_num);
return 0;
}
เอาต์พุต
tan of (1, 0) is (1.55741, 0)