1 // now #include the definition of std::complex<>
2 // and define the doublify() function.
3 
4 
5 #include <iostream>
6 #include <complex>
7 #ifdef _MSC_VER
8 #  include "vcl_msvc_warnings.h"
9 #endif
10 
11 
doublify(std::complex<float> const & z)12 std::complex<double> doublify(std::complex<float> const &z)
13 {
14   return {std::real(z), std::imag(z)};
15 }
16 
17 
test_complex_main(int,char * [])18 int test_complex_main(int /*argc*/,char* /*argv*/[])
19 {
20   std::complex<double> dc1(1.1,1.2), dc2(2.1,2.2);
21   std::complex<float> fc1(1.1f,1.2f), fc2(2.1f,2.2f);
22 
23   std::cout << dc1 << " + " << dc2 << " = " << (dc1+dc2) << std::endl
24            << fc1 << " + " << fc2 << " = " << (fc1+fc2) << std::endl;
25 
26   std::complex<double> dc3(std::real(dc1),std::imag(dc2));
27   std::complex<float> fc3(std::real(fc1),std::imag(fc2));
28 
29   std::cout << dc3 << " / " << dc1 << " = " << dc3/dc1 << std::endl
30            << fc3 << " / " << fc1 << " = " << fc3/fc1 << std::endl;
31 
32   std::cout << "polar representation of " << dc3 << " is [" << std::abs(dc3) << ',' << std::arg(dc3) << "]\n"
33            << "going back: " << dc3 << " must be = " << std::polar(std::abs(dc3), std::arg(dc3)) << std::endl;
34   std::complex<float> fcsr3 = std::sqrt(fc3);
35   std::cout << "sqrt(" << fc3 << ") is " << fcsr3 << ", so " << fcsr3 << " * " << fcsr3 << " = " << fcsr3*fcsr3 << std::endl;
36 
37   // Should also test form of complex stream input and output. The standard says:
38   // [26.2.6.12] operator>> understands "u", "(u)" and  "(u,v)" where u, v are real.
39   // [26.2.6.13] operator<< does f << '(' << x.real() << ',' << x.imag() << ')';
40   // In particular, complex numbers written with operator<< can be read again with
41   // operator>>.
42 
43   // complex should have a type called value_type;
44   std::complex<float>::value_type tmp = 1.0f;
45   (void)tmp; // to avoid unused variable warnings.
46 
47 
48   // Test the std::pow functions
49 
50   bool success = true;
51 
52   const std::complex<double> neg1(-1.0, 0.0);
53   const std::complex<double> i(0.0,1.0);
54   std::complex<double> sqrt_neg1 = std::pow(neg1, 0.5);
55   std::cout << "pow("<<neg1<<",0.5) = "<<sqrt_neg1<<
56     " and should be (0,1)"<<std::endl;
57   double error = std::abs(sqrt_neg1-i);
58 // need to be careful of quiet NANs
59   if ( error < 0.0  || 1e-6 < error)
60   {
61     std::cout << "** FAILURE **\n";
62     success = false;
63   }
64 
65   const std::complex<double> half(0.5,0.0);
66   sqrt_neg1 = std::pow(neg1, half);
67   std::cout << "pow("<<neg1<<','<<half<<") = "<<sqrt_neg1<<
68     " and should be (0,1)"<<std::endl;
69   error = std::abs(sqrt_neg1-i);
70   if ( error < 0.0  || 1e-6 < error)
71   {
72     std::cout << "** FAILURE **\n";
73     success = false;
74   }
75 
76   std::complex<double> zero(0.0,0.0);
77   std::cout << "Implementation defines std::pow((0,0),(0,0)) = "
78            << std::pow(zero, zero) << std::endl;
79 
80   {
81     std::complex<double> x(2, 3);
82     std::complex<double> xc = std::conj(x);
83     std::cout << "Conjugate " << x << " = " << xc << '\n';
84     if ( xc != std::complex<double>(2,-3) ) {
85       success = false;
86     }
87   }
88 
89   return success?0:1;
90 }
91