Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Hàm tan() cho số phức trong C++

std::tan cho số phức (header <complex>) tính giá trị tangens của một số phức. Là phiên bản mở rộng của std::tan cho số thực (<cmath>).

Cú pháp

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

Công thức toán học

Đối với số phức z = x + iy:

tan(z) = sin(z) / cos(z)
       = (sin(2x) + i*sinh(2y)) / (cos(2x) + cosh(2y))

Ví dụ 1: Số phức tổng quát

#include <iostream>
#include <complex>
using namespace std;

int main() {
    complex<double> z(9.1, 2.6); // 9.1 + 2.6i
    complex<double> result = tan(z);
    
    cout << "tan(" << z << ") = " << result << '\n';
    // Output: tan(9.1, 2.6) = (-0.00661488, 0.99123)
    return 0;
}

Đầu ra

tan(9.1, 2.6) = (-0.00661488, 0.99123)

Ví dụ 2: Số thực (phần ảo = 0)

#include <iostream>
#include <complex>
using namespace std;

int main() {
    complex<double> z(1.0, 0.0); // Số thực 1.0
    complex<double> result = tan(z);
    
    cout << "tan(" << z << ") = " << result << '\n';
    // Output: tan(1, 0) = (1.55741, 0) ≈ tan(1 radian)
    return 0;
}

Đầu ra

tan(1, 0) = (1.55741, 0)

Ví dụ 3: Số ảo thuần túy

#include <iostream>
#include <complex>
using namespace std;

int main() {
    complex<double> z(0.0, 1.0); // i
    complex<double> result = tan(z);
    
    cout << "tan(i) = " << result << '\n';
    // tan(i) = i * tanh(1) ≈ i * 0.761594 = (0, 0.761594)
    return 0;
}

Đầu ra

tan(i) = (0, 0.761594)

Các hàm lượng giác phức khác

HàmHeaderMô tả
sin(z)<complex>Sinus phức
cos(z)<complex>Cosinus phức
tan(z)<complex>Tangens phức
asin(z)<complex>Arcsin phức
acos(z)<complex>Arccos phức
atan(z)<complex>Arctan phức
sinh(z)<complex>Hyperbolic sin
cosh(z)<complex>Hyperbolic cos
tanh(z)<complex>Hyperbolic tan

Lưu ý

  • Kết quả là số phức complex<T> cùng kiểu với tham số.
  • Độ chính xác phụ thuộc implementation.
  • Có thể ném exception nếu kết quả vượt quá phạm vi biểu diễn.