Ceci est une ancienne révision du document !
#include <iostream> #include <complex> using namespace std::literals; int main() { std::cout << "i * i = " << (1.0i * 1.0i) << std::endl; std::cout << "2+3i = " << (2.0 + 3.0i) << std::endl; }
affiche :
i * i = (-1,0) (2,3)
1. vérifie que i² = -1 2. notation dans la console : affiche (reel, imaginaire)
Note : création d'un type masqué : std::complex<double>
Remarque : on peut additionner un réel et un complexe, mais pas un entier et un complexe :
#include <iostream> #include <complex> using namespace std::literals; int main() { std::cout << (1 + (2.0 + 3.0i)) << std::endl; }
affiche (et beaucoup de messages ensuite) :
main.cpp: In function 'int main()': main.cpp:6:21: error: no match for 'operator+' (operand types are 'int' and 'std::complex<double>') std::cout << (1 + (2.0 + 3.0i)) << std::endl; ^
En utilisant un réel (notez bien le point ajouté sur 1) :
#include <iostream> #include <complex> using namespace std::literals; int main() { std::cout << (1.0 + (2.0 + 3.0i)) << std::endl; }
affiche (et beaucoup de messages ensuite) :
(3,3)
Fonctions :
#include <iostream> #include <complex> using namespace std::literals; int main() { std::cout << real(2.0 + 3.0i) << std::endl; std::cout << imag(2.0 + 3.0i) << std::endl; std::cout << abs(2.0 + 3.0i) << std::endl; std::cout << arg(2.0 + 3.0i) << std::endl; std::cout << norm(2.0 + 3.0i) << std::endl; std::cout << conj(2.0 + 3.0i) << std::endl; std::cout << proj(2.0 + 3.0i) << std::endl; std::cout << polar(2.0 + 3.0i) << std::endl; }
affiche :
2 3 3.60555 0.982794 13 (2,-3) (2,3) ((2,3),(0,0))
Note : plusieurs notation pour i : i
pour double (réels 64b), if
pour les floats (réels 32b) et il
pour les long double (128b)