C++将角度转为复数

发布时间 2023-10-06 18:35:20作者: 朱小勇
  • 1.角度转复数,使用std::polar
#include <iostream>
#include <complex>
#include <cmath>

int main () {
   float theta = 45;
   float theta_pi = theta*(M_PI/180); 
   std::cout << " is " << std::polar (1.0f, theta_pi) << '\n';

   return 0;
}
  • 2.角度转复数,使用cos+i*sin
#include <iostream>
#include <complex>
#include <cmath>

int main () {
   double theta = 45;
   double theta_pi = theta*(M_PI/180);
   std::complex<double> rst;
   std::complex<double> I = std::complex<double>(0.0f, 1.0f);
   rst = cos(theta_pi) + I*sin(theta_pi);
   std::cout << " is " << rst << '\n';
   return 0;
}
  • 3.角度转复指数,使用C库<complex.h>中的_Complex_I
#include <iostream>
#include <complex.h>
#include <cmath>

int main () {
   float theta = 45;
   float theta_pi = theta*(M_PI/180);
   std::cout << " is " << std::exp(_Complex_I * theta_pi);

   return 0;
}
  • 4.角度转复指数,使用(0,1)表示虚数单位
#include <iostream>
#include <complex>
#include <cmath>

int main () {
   float theta = 45;
   float theta_pi = theta*(M_PI/180);
   std::cout << " is " << std::exp(std::complex<float>(0, 1) * theta_pi);

   return 0;
}