Computer >> Máy Tính >  >> Lập trình >> C ++

hàm polar () cho số phức trong C ++

Hàm cực của số phức được sử dụng để trả về một số phức.

Cực () hàm được định nghĩa trong tệp tiêu đề phức tạp trong c ++. Nó lấy độ lớn và góc pha của một số phức và tạo ra một số phức bằng cách sử dụng các giá trị này.

Cú pháp

polar(mag, phase);

Thông số - cần hai giá trị là tham số, pha và độ lớn của số phức được tạo.

Giá trị trả lại - hàm trả về một số phức.

polar(0.2, 0.5)
-> (0.175517,0.0958851)

Ví dụ

#include<iostream>
#include>complex.h>
using namespace std;
int main () {
   cout<<"\t\tRUN 1\n";
   cout<<"Complex number with magnitude: 5.2 and phase angle: 1.6 is ";
   cout<<polar(5.2,1.6)<<endl;
   cout<<"\t\tRUN 2\n";
   cout<<"Complex number with magnitude: 0.5 and phase angle: 0.2 is ";
   cout<<polar(0.5,0.2)<<endl;
   cout<<"\t\tRUN 3\n";
   cout<<"Complex number with magnitude: 0.2 and phase angle: 0.5 is ";
   cout<<polar(0.2,0.5)<<endl;
   return 0;
}

Đầu ra

RUN 1
Complex number with magnitude: 5.2 and phase angle: 1.6 is (-0.151838,5.19778)
RUN 2
Complex number with magnitude: 0.5 and phase angle: 0.2 is (0.490033,0.0993347)
RUN 3
Complex number with magnitude: 0.2 and phase angle: 0.5 is (0.175517,0.0958851)