1 // The template and inlines 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.
18 // As a special exception, if you link this library with files compiled
19 // with a GNU compiler to produce an executable, this does not cause the
20 // resulting executable to be covered by the GNU General Public License.
21 // This exception does not however invalidate any other reasons why the
22 // executable file might be covered by the GNU General Public License.
24 // Written by Jason Merrill based upon the specification in the 27 May 1994
25 // C++ working paper, ANSI document X3J16/94-0098.
36 #if ! defined (__GNUG__) && ! defined (__attribute__)
37 #define __attribute__(foo) /* Ignore. */
44 template <class _FLT
> class complex;
45 template <class _FLT
> complex<_FLT
>&
46 __doapl (complex<_FLT
>* ths
, const complex<_FLT
>& r
);
47 template <class _FLT
> complex<_FLT
>&
48 __doami (complex<_FLT
>* ths
, const complex<_FLT
>& r
);
49 template <class _FLT
> complex<_FLT
>&
50 __doaml (complex<_FLT
>* ths
, const complex<_FLT
>& r
);
51 template <class _FLT
> complex<_FLT
>&
52 __doadv (complex<_FLT
>* ths
, const complex<_FLT
>& r
);
58 complex (_FLT r
= 0, _FLT i
= 0): re (r
), im (i
) { }
59 complex& operator += (const complex&);
60 complex& operator -= (const complex&);
61 complex& operator *= (const complex&);
62 complex& operator /= (const complex&);
63 _FLT
real () const { return re
; }
64 _FLT
imag () const { return im
; }
68 friend complex& __doapl
<> (complex *, const complex&);
69 friend complex& __doami
<> (complex *, const complex&);
70 friend complex& __doaml
<> (complex *, const complex&);
71 friend complex& __doadv
<> (complex *, const complex&);
74 // Declare specializations.
76 class complex<double>;
77 class complex<long double>;
81 __doapl (complex<_FLT
>* ths
, const complex<_FLT
>& r
)
89 complex<_FLT
>::operator += (const complex<_FLT
>& r
)
91 return __doapl (this, r
);
96 __doami (complex<_FLT
>* ths
, const complex<_FLT
>& r
)
102 template <class _FLT
>
103 inline complex<_FLT
>&
104 complex<_FLT
>::operator -= (const complex<_FLT
>& r
)
106 return __doami (this, r
);
109 template <class _FLT
>
110 inline complex<_FLT
>&
111 __doaml (complex<_FLT
>* ths
, const complex<_FLT
>& r
)
113 _FLT f
= ths
->re
* r
.re
- ths
->im
* r
.im
;
114 ths
->im
= ths
->re
* r
.im
+ ths
->im
* r
.re
;
118 template <class _FLT
>
119 inline complex<_FLT
>&
120 complex<_FLT
>::operator *= (const complex<_FLT
>& r
)
122 return __doaml (this, r
);
125 template <class _FLT
>
126 inline complex<_FLT
>&
127 complex<_FLT
>::operator /= (const complex<_FLT
>& r
)
129 return __doadv (this, r
);
132 template <class _FLT
> inline _FLT
133 imag (const complex<_FLT
>& x
) __attribute__ ((const));
135 template <class _FLT
> inline _FLT
136 imag (const complex<_FLT
>& x
)
141 template <class _FLT
> inline _FLT
142 real (const complex<_FLT
>& x
) __attribute__ ((const));
144 template <class _FLT
> inline _FLT
145 real (const complex<_FLT
>& x
)
150 template <class _FLT
> inline complex<_FLT
>
151 operator + (const complex<_FLT
>& x
, const complex<_FLT
>& y
) __attribute__ ((const));
153 template <class _FLT
> inline complex<_FLT
>
154 operator + (const complex<_FLT
>& x
, const complex<_FLT
>& y
)
156 return complex<_FLT
> (real (x
) + real (y
), imag (x
) + imag (y
));
159 template <class _FLT
> inline complex<_FLT
>
160 operator + (const complex<_FLT
>& x
, _FLT y
) __attribute__ ((const));
162 template <class _FLT
> inline complex<_FLT
>
163 operator + (const complex<_FLT
>& x
, _FLT y
)
165 return complex<_FLT
> (real (x
) + y
, imag (x
));
168 template <class _FLT
> inline complex<_FLT
>
169 operator + (_FLT x
, const complex<_FLT
>& y
) __attribute__ ((const));
171 template <class _FLT
> inline complex<_FLT
>
172 operator + (_FLT x
, const complex<_FLT
>& y
)
174 return complex<_FLT
> (x
+ real (y
), imag (y
));
177 template <class _FLT
> inline complex<_FLT
>
178 operator - (const complex<_FLT
>& x
, const complex<_FLT
>& y
) __attribute__ ((const));
180 template <class _FLT
> inline complex<_FLT
>
181 operator - (const complex<_FLT
>& x
, const complex<_FLT
>& y
)
183 return complex<_FLT
> (real (x
) - real (y
), imag (x
) - imag (y
));
186 template <class _FLT
> inline complex<_FLT
>
187 operator - (const complex<_FLT
>& x
, _FLT y
) __attribute__ ((const));
189 template <class _FLT
> inline complex<_FLT
>
190 operator - (const complex<_FLT
>& x
, _FLT y
)
192 return complex<_FLT
> (real (x
) - y
, imag (x
));
195 template <class _FLT
> inline complex<_FLT
>
196 operator - (_FLT x
, const complex<_FLT
>& y
) __attribute__ ((const));
198 template <class _FLT
> inline complex<_FLT
>
199 operator - (_FLT x
, const complex<_FLT
>& y
)
201 return complex<_FLT
> (x
- real (y
), - imag (y
));
204 template <class _FLT
> inline complex<_FLT
>
205 operator * (const complex<_FLT
>& x
, const complex<_FLT
>& y
) __attribute__ ((const));
207 template <class _FLT
> inline complex<_FLT
>
208 operator * (const complex<_FLT
>& x
, const complex<_FLT
>& y
)
210 return complex<_FLT
> (real (x
) * real (y
) - imag (x
) * imag (y
),
211 real (x
) * imag (y
) + imag (x
) * real (y
));
214 template <class _FLT
> inline complex<_FLT
>
215 operator * (const complex<_FLT
>& x
, _FLT y
) __attribute__ ((const));
217 template <class _FLT
> inline complex<_FLT
>
218 operator * (const complex<_FLT
>& x
, _FLT y
)
220 return complex<_FLT
> (real (x
) * y
, imag (x
) * y
);
223 template <class _FLT
> inline complex<_FLT
>
224 operator * (_FLT x
, const complex<_FLT
>& y
) __attribute__ ((const));
226 template <class _FLT
> inline complex<_FLT
>
227 operator * (_FLT x
, const complex<_FLT
>& y
)
229 return complex<_FLT
> (x
* real (y
), x
* imag (y
));
232 template <class _FLT
> complex<_FLT
>
233 operator / (const complex<_FLT
>& x
, _FLT y
) __attribute__ ((const));
235 template <class _FLT
> complex<_FLT
>
236 operator / (const complex<_FLT
>& x
, _FLT y
)
238 return complex<_FLT
> (real (x
) / y
, imag (x
) / y
);
241 template <class _FLT
> inline complex<_FLT
>
242 operator + (const complex<_FLT
>& x
) __attribute__ ((const));
244 template <class _FLT
> inline complex<_FLT
>
245 operator + (const complex<_FLT
>& x
)
250 template <class _FLT
> inline complex<_FLT
>
251 operator - (const complex<_FLT
>& x
) __attribute__ ((const));
253 template <class _FLT
> inline complex<_FLT
>
254 operator - (const complex<_FLT
>& x
)
256 return complex<_FLT
> (-real (x
), -imag (x
));
259 template <class _FLT
> inline bool
260 operator == (const complex<_FLT
>& x
, const complex<_FLT
>& y
) __attribute__ ((const));
262 template <class _FLT
> inline bool
263 operator == (const complex<_FLT
>& x
, const complex<_FLT
>& y
)
265 return real (x
) == real (y
) && imag (x
) == imag (y
);
268 template <class _FLT
> inline bool
269 operator == (const complex<_FLT
>& x
, _FLT y
) __attribute__ ((const));
271 template <class _FLT
> inline bool
272 operator == (const complex<_FLT
>& x
, _FLT y
)
274 return real (x
) == y
&& imag (x
) == 0;
277 template <class _FLT
> inline bool
278 operator == (_FLT x
, const complex<_FLT
>& y
) __attribute__ ((const));
280 template <class _FLT
> inline bool
281 operator == (_FLT x
, const complex<_FLT
>& y
)
283 return x
== real (y
) && imag (y
) == 0;
286 template <class _FLT
> inline bool
287 operator != (const complex<_FLT
>& x
, const complex<_FLT
>& y
) __attribute__ ((const));
289 template <class _FLT
> inline bool
290 operator != (const complex<_FLT
>& x
, const complex<_FLT
>& y
)
292 return real (x
) != real (y
) || imag (x
) != imag (y
);
295 template <class _FLT
> inline bool
296 operator != (const complex<_FLT
>& x
, _FLT y
) __attribute__ ((const));
298 template <class _FLT
> inline bool
299 operator != (const complex<_FLT
>& x
, _FLT y
)
301 return real (x
) != y
|| imag (x
) != 0;
304 template <class _FLT
> inline bool
305 operator != (_FLT x
, const complex<_FLT
>& y
) __attribute__ ((const));
307 template <class _FLT
> inline bool
308 operator != (_FLT x
, const complex<_FLT
>& y
)
310 return x
!= real (y
) || imag (y
) != 0;
313 // Some targets don't provide a prototype for hypot when -ansi.
314 extern "C" double hypot (double, double) __attribute__ ((const));
316 template <class _FLT
> inline _FLT
317 abs (const complex<_FLT
>& x
) __attribute__ ((const));
319 template <class _FLT
> inline _FLT
320 abs (const complex<_FLT
>& x
)
322 return hypot (real (x
), imag (x
));
325 template <class _FLT
> inline _FLT
326 arg (const complex<_FLT
>& x
) __attribute__ ((const));
328 template <class _FLT
> inline _FLT
329 arg (const complex<_FLT
>& x
)
331 return atan2 (imag (x
), real (x
));
334 template <class _FLT
> inline complex<_FLT
>
335 polar (_FLT r
, _FLT t
) __attribute__ ((const));
337 template <class _FLT
> inline complex<_FLT
>
338 polar (_FLT r
, _FLT t
)
340 return complex<_FLT
> (r
* cos (t
), r
* sin (t
));
343 template <class _FLT
> inline complex<_FLT
>
344 conj (const complex<_FLT
>& x
) __attribute__ ((const));
346 template <class _FLT
> inline complex<_FLT
>
347 conj (const complex<_FLT
>& x
)
349 return complex<_FLT
> (real (x
), -imag (x
));
352 template <class _FLT
> inline _FLT
353 norm (const complex<_FLT
>& x
) __attribute__ ((const));
355 template <class _FLT
> inline _FLT
356 norm (const complex<_FLT
>& x
)
358 return real (x
) * real (x
) + imag (x
) * imag (x
);
361 // Declarations of templates in complext.ccI
363 template <class _FLT
> complex<_FLT
>
364 operator / (const complex<_FLT
>&, const complex<_FLT
>&) __attribute__ ((const));
365 template <class _FLT
> complex<_FLT
>
366 operator / (_FLT
, const complex<_FLT
>&) __attribute__ ((const));
367 template <class _FLT
> complex<_FLT
>
368 cos (const complex<_FLT
>&) __attribute__ ((const));
369 template <class _FLT
> complex<_FLT
>
370 cosh (const complex<_FLT
>&) __attribute__ ((const));
371 template <class _FLT
> complex<_FLT
>
372 exp (const complex<_FLT
>&) __attribute__ ((const));
373 template <class _FLT
> complex<_FLT
>
374 log (const complex<_FLT
>&) __attribute__ ((const));
375 template <class _FLT
> complex<_FLT
>
376 pow (const complex<_FLT
>&, const complex<_FLT
>&) __attribute__ ((const));
377 template <class _FLT
> complex<_FLT
>
378 pow (const complex<_FLT
>&, _FLT
) __attribute__ ((const));
379 template <class _FLT
> complex<_FLT
>
380 pow (const complex<_FLT
>&, int) __attribute__ ((const));
381 template <class _FLT
> complex<_FLT
>
382 pow (_FLT
, const complex<_FLT
>&) __attribute__ ((const));
383 template <class _FLT
> complex<_FLT
>
384 sin (const complex<_FLT
>&) __attribute__ ((const));
385 template <class _FLT
> complex<_FLT
>
386 sinh (const complex<_FLT
>&) __attribute__ ((const));
387 template <class _FLT
> complex<_FLT
>
388 sqrt (const complex<_FLT
>&) __attribute__ ((const));
390 template <class _FLT
> istream
& operator >> (istream
&, complex<_FLT
>&);
391 template <class _FLT
> ostream
& operator << (ostream
&, const complex<_FLT
>&);
394 // Specializations and such
396 #include <std/fcomplex.h>
397 #include <std/dcomplex.h>
398 #include <std/ldcomplex.h>