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

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

บทนำ

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

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

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

cos(z) = (e^(iz) + e^(-iz)) / 2

โดย z = จำนวนเชิงซ้อน, i = iota

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

complex<double> name(a, b)

เก็บ ordered pair double 2 ค่า: Real part และ Imaginary part ต้อง include <complex>

Syntax

cos(complexnumber)

ตัวอย่าง

Input: complexnumber(3, 4)
Output: -27.0349, -3.85115

คำอธิบาย - 3 = Real part, 4 = Imaginary part ส่งเข้า cos() ได้ Complex Cosine

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

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

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

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

int main() {
    complex<double> complexnumber(2, 3);
    cout << cos(complexnumber);
    return 0;
}

ผลลัพธ์

<-1.56563, -3.29789>

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