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

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

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

tanh() สำหรับจำนวนเชิงซ้อน เป็นฟังก์ชันใน header <complex> ใช้หา hyperbolic tangent ของจำนวนเชิงซ้อน เป็นเวอร์ชัน complex ของ tanh() ใน <cmath>

tanh คืออะไร?

Tanh คือ hyperbolic tangent function สามารถนิยามได้ว่า tanh = hyperbolic sine / hyperbolic cosine

ไวยากรณ์

complex<T> tanh(complex& num);

พารามิเตอร์

ฟังก์ชันรับพารามิเตอร์ 1 ตัว คือ num ชนิด complex type

ค่าที่คืน

คืนค่า hyperbolic tangent ของ num

ตัวอย่าง

Input: complex<double> num(0.0, 1.0); tanh(num); Output: (0, 1.55741)
Input: complex<double> num(1.0, 0.0); tanh(num); Output: (0.761594, 0)

แนวทางการทำงาน

  • ใน main function กำหนด complex number
  • จากนั้นพิมพ์ complex number

  • สุดท้ายพิมพ์ hyperbolic tangent ของ complex number

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

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

int main() {
    // defining complex number
    complex<double> x(1, 0);
    cout << "tanh " << x << " = " << tanh(x) << endl;
    
    complex<double> y(0, 1);
    cout << "tanh " << y << " = " << tanh(y) << endl;
    
    return 0;
}

เอาต์พุต

tanh(1.000000,0.000000) = (0.761594, 0.000000)
tanh(0.000000,1.000000) = (0.000000, 1.557408)