// Euler's Identity: e**(i * pi) + 1 = 0
#include <complex>
#include <iostream>
const std::complex<double> I(0.0, 1.0);
const double PI = 3.141592653589793238463;
int main()
{
std::complex<double> eRaisedToTheITimesPiPower;
std::complex<double> lhs ;
std::complex<double> rhs ;
double lhsAbs ;
double rhsAbs ;
double lhsArg ;
double rhsArg ;
double lhsReal;
double rhsReal;
double lhsImag;
double rhsImag;
std::complex<double> lhs2 ;
std::complex<double> rhs2 ;
bool lhsIsEqualToRhs ;
bool lhs2IsEqualToRhs2;
eRaisedToTheITimesPiPower = std::exp(I * PI);
lhs = eRaisedToTheITimesPiPower + 1.0;
rhs = std::complex<double>(0.0, 0.0);
lhsAbs = std::abs (lhs);
rhsAbs = std::abs (rhs);
lhsArg = std::arg (lhs);
rhsArg = std::arg (rhs);
lhsReal = std::real(lhs);
rhsReal = std::real(rhs);
lhsImag = std::imag(lhs);
rhsImag = std::imag(rhs);
lhs2 = std::polar<double>(lhsReal, lhsImag);
rhs2 = std::polar<double>(rhsReal, rhsImag);
lhsIsEqualToRhs = (lhs == rhs );
lhs2IsEqualToRhs2 = (lhs2 == rhs2);
std::cout << eRaisedToTheITimesPiPower << " ";
std::cout << lhs << " ";
std::cout << rhs << " ";
std::cout << lhsAbs << " ";
std::cout << rhsAbs << " ";
std::cout << std::endl;
std::cout << lhsArg << " ";
std::cout << rhsArg << " ";
std::cout << lhsReal << " ";
std::cout << rhsReal << " ";
std::cout << lhsImag << " ";
std::cout << rhsImag << " ";
std::cout << lhs2 << " ";
std::cout << rhs2 << " ";
std::cout << lhsIsEqualToRhs << " ";
std::cout << lhs2IsEqualToRhs2 << " ";
std::cout << std::endl;
return 0;
}
// Output: (-1,1.22465e-16) (0,1.22465e-16) (0,0) 1.22465e-16 0
// 1.5708 0 0 0 1.22465e-16 0 (0,0) (0,0) 0 1
Reference: http://en.cppreference.com/w/cpp/numeric/complex
Wednesday, December 27, 2017
C++98, 11, 14: Complex Numbers
The std::complex template class handles complex numbers. Here is an example:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment