1 // Tests for the -*- C++ -*- complex number classes.
2 // Copyright (C) 1994 Free Software Foundation
4 // This file is part of the GNU ANSI C++ Library. This library is free
5 // software; you can redistribute it and/or modify it under the terms of
6 // the GNU General Public License as published by the Free Software
7 // Foundation; either version 2, or (at your option) any later version.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this library; see the file COPYING. If not, write to the Free
16 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 // to test near-equality
24 const double eps
= 0.000001;
26 static void close_enough(const double_complex
& a
, const double_complex
& b
)
28 assert(fabs(real(a
) - real(b
)) < eps
&&
29 fabs(imag(a
) - imag(b
)) < eps
);
33 void test3(double_complex
& a
, double_complex
& b
, double_complex
& c
)
36 close_enough(-(-a
) , a
);
37 close_enough((a
+ b
) , (b
+ a
));
38 close_enough((a
+ (-b
)) , (a
- b
));
39 close_enough((a
* b
) , (b
* a
));
40 close_enough((a
* (-b
)) , -(a
* b
));
41 close_enough((a
/ (-b
)) , -(a
/ b
));
42 close_enough((a
- b
) , -(b
- a
));
43 close_enough((a
+ (b
+ c
)) , ((a
+ b
) + c
));
44 close_enough((a
* (b
* c
)) , ((a
* b
) * c
));
45 close_enough((a
* (b
+ c
)) , ((a
* b
) + (a
* c
)));
46 close_enough(((a
- b
) + b
) , a
);
47 close_enough(((a
+ b
) - b
) , a
);
48 close_enough(((a
* b
) / b
) , a
);
49 close_enough(((a
/ b
) * b
) , a
);
54 close_enough(x
, (a
* b
));
56 close_enough(x
, ((a
* b
) + c
));
58 close_enough(x
, (((a
* b
) + c
) - a
));
60 close_enough(x
, ((((a
* b
) + c
) - a
) / b
));
66 double_complex one
= 1.0;
67 double_complex
i (0.0, 1.0);
68 double_complex neg_one
= -1.0;
70 cout
<< "double_complex one = " << one
<< "\n";
71 cout
<< "i = " << i
<< "\n";
72 cout
<< "neg_one = " << neg_one
<< "\n";
73 cout
<< "sqrt(neg_one) = " << sqrt(neg_one
) << "\n";
75 double_complex
a (2.0, 3.0);
76 double_complex
b (4.0, 5.0);
78 cout
<< "a = " << a
<< "\n";
79 cout
<< "b = " << b
<< "\n";
81 cout
<< "a + one = " << (a
+ one
) << "\n";
82 (close_enough((a
+one
), double_complex(3.0, 3.0)));
83 cout
<< "a - one = " << (a
- one
) << "\n";
84 (close_enough((a
-one
), double_complex(1.0, 3.0)));
85 cout
<< "a * one = " << (a
* one
) << "\n";
86 (close_enough((a
*one
), a
));
87 cout
<< "a / one = " << (a
/ one
) << "\n";
88 (close_enough((a
/one
), a
));
90 cout
<< "a + b = " << (a
+ b
) << "\n";
91 (close_enough((a
+b
), double_complex(6.0, 8.0)));
92 cout
<< "a - b = " << (a
- b
) << "\n";
93 (close_enough((a
-b
), double_complex(-2.0, -2.0)));
94 cout
<< "a * b = " << (a
* b
) << "\n";
95 (close_enough((a
*b
), double_complex(-7.0, 22.0)));
96 cout
<< "a / b = " << (a
/ b
) << "\n";
97 (close_enough((a
/b
), double_complex(0.5609760976, 0.0487804878)));
101 c
= a
; cout
<< "c = a; c += b = " << (c
+= b
) << "\n";
102 c
= a
; cout
<< "c = a; c -= b = " << (c
-= b
) << "\n";
103 c
= a
; cout
<< "c = a; c *= b = " << (c
*= b
) << "\n";
104 c
= a
; cout
<< "c = a; c /= b = " << (c
/= b
) << "\n";
106 cout
<< "-a = " << (-a
) << "\n";
107 cout
<< "real(a) = " << real(a
) << "\n";
108 assert(real(a
) == 2.0);
109 cout
<< "imag(a) = " << imag(a
) << "\n";
110 assert(imag(a
) == 3.0);
111 cout
<< "conj(a) = " << conj(a
) << "\n";
112 assert(conj(a
) == double_complex(2.0, -3.0));
113 cout
<< "norm(a) = " << norm(a
) << "\n";
114 assert(norm(a
) == 13.0);
116 cout
<< "abs(a) = " << abs(a
) << "\n";
117 cout
<< "arg(a) = " << arg(a
) << "\n";
118 cout
<< "cos(a) = " << cos(a
) << "\n";
119 cout
<< "sin(a) = " << sin(a
) << "\n";
120 cout
<< "cosh(a) = " << cosh(a
) << "\n";
121 cout
<< "sinh(a) = " << sinh(a
) << "\n";
122 cout
<< "log(a) = " << log(a
) << "\n";
123 cout
<< "exp(a) = " << exp(a
) << "\n";
124 cout
<< "sqrt(a) = " << sqrt(a
) << "\n";
125 cout
<< "pow(a, 2) = " << pow(a
, 2) << "\n";
127 double_complex p
= pow(a
, b
);
128 if(sizeof(float)==sizeof(double)) {
129 long w
= (long)(p
.imag()*100000);
131 p
=double_complex(-0.753046,-0.986429);
133 cout
<< "pow(a, b) = " << p
<< "\n";
136 double_complex
d (10, 20);
137 double_complex e
= pow(a
, 2);
139 test3(one
, one
, one
);
145 cout
<< "enter a complex number in form a or (a) or (a, b): ";
147 cout
<< "number = " << c
<< "\n";
149 cout
<< "\nEnd of test\n";