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

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

บทนำ

งานนี้แสดงการทำงานของ cosh() สำหรับจำนวนเชิงซ้อน (Complex Numbers) ใน C++

cosh() เป็นส่วนหนึ่งของ C++ STL ต่างจาก cosh() มาตรฐานที่คำนวณ Hyperbolic Cosine ของมุมในเรเดียน ฟังก์ชันนี้คำนวณ Complex Hyperbolic Cosine ของจำนวนเชิงซ้อน

สูตรคณิตศาสตร์

cosh(z) = (e^z + e^(-z)) / 2

โดย z = จำนวนเชิงซ้อน, i = iota (√-1)

การประกาศจำนวนเชิงซ้อน

complex<double> name(a, b)

<double> หมายถึงเก็บ ordered pair ทั้งคู่เป็น double (Real part และ Imaginary part) ต้อง include <complex>

Syntax

cosh(complexnumber)

ตัวอย่าง

Input: complexnumber(5, 5)
Output: <-27.0349, -3.85115>

คำอธิบาย - 5 คือ Real part, 5 คือ Imaginary part ส่งเข้า cosh() ได้ผลลัพธ์ Complex Hyperbolic Cosine

แนวทางในโปรแกรม

  • ประกาศจำนวนเชิงซ้อน complexnumber(a,b) กำหนดค่า
  • ค่าแรก = Real part, ค่าที่สอง = Imaginary part (เช่น complexnumber(1,3) = 1+3i)
  • ส่งเข้า cosh()

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

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

int main() {
    complex<double> cno(1, 3);
    cout << cosh(cno);
    return 0;
}

ผลลัพธ์

<-1.52764, 0.165844>

Real part = 1, Imaginary part = 3 ส่งเข้า cosh() ได้ผลลัพธ์ดังกล่าว