]>
Commit | Line | Data |
---|---|---|
54c1bf78 | 1 | // The template and inlines for the -*- C++ -*- complex number classes. |
de96ac46 | 2 | |
5624e564 | 3 | // Copyright (C) 1997-2015 Free Software Foundation, Inc. |
de96ac46 BK |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
de96ac46 BK |
9 | // any later version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
748086b7 JJ |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
de96ac46 | 19 | |
748086b7 JJ |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
de96ac46 | 24 | |
ad68e9fc | 25 | /** @file include/complex |
143c27b0 BK |
26 | * This is a Standard C++ Library header. |
27 | */ | |
28 | ||
54c1bf78 BK |
29 | // |
30 | // ISO C++ 14882: 26.2 Complex Numbers | |
31 | // Note: this is not a conforming implementation. | |
32 | // Initially implemented by Ulrich Drepper <drepper@cygnus.com> | |
33 | // Improved by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr> | |
34 | // | |
35 | ||
1143680e SE |
36 | #ifndef _GLIBCXX_COMPLEX |
37 | #define _GLIBCXX_COMPLEX 1 | |
54c1bf78 BK |
38 | |
39 | #pragma GCC system_header | |
40 | ||
41 | #include <bits/c++config.h> | |
42 | #include <bits/cpp_type_traits.h> | |
e133ace8 | 43 | #include <ext/type_traits.h> |
54c1bf78 BK |
44 | #include <cmath> |
45 | #include <sstream> | |
46 | ||
1c259e8b MG |
47 | // Get rid of a macro possibly defined in <complex.h> |
48 | #undef complex | |
49 | ||
12ffa228 BK |
50 | namespace std _GLIBCXX_VISIBILITY(default) |
51 | { | |
52 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
3cbc7af0 | 53 | |
5b9daa7e BK |
54 | /** |
55 | * @defgroup complex_numbers Complex Numbers | |
56 | * @ingroup numerics | |
57 | * | |
58 | * Classes and functions for complex numbers. | |
59 | * @{ | |
60 | */ | |
61 | ||
52e6723c | 62 | // Forward declarations. |
54c1bf78 BK |
63 | template<typename _Tp> class complex; |
64 | template<> class complex<float>; | |
65 | template<> class complex<double>; | |
66 | template<> class complex<long double>; | |
67 | ||
ffcec5c8 | 68 | /// Return magnitude of @a z. |
54c1bf78 | 69 | template<typename _Tp> _Tp abs(const complex<_Tp>&); |
ffcec5c8 | 70 | /// Return phase angle of @a z. |
54c1bf78 | 71 | template<typename _Tp> _Tp arg(const complex<_Tp>&); |
ffcec5c8 | 72 | /// Return @a z magnitude squared. |
54c1bf78 BK |
73 | template<typename _Tp> _Tp norm(const complex<_Tp>&); |
74 | ||
ffcec5c8 | 75 | /// Return complex conjugate of @a z. |
54c1bf78 | 76 | template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&); |
ffcec5c8 | 77 | /// Return complex with magnitude @a rho and angle @a theta. |
54c1bf78 BK |
78 | template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0); |
79 | ||
80 | // Transcendentals: | |
ffcec5c8 | 81 | /// Return complex cosine of @a z. |
54c1bf78 | 82 | template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&); |
ffcec5c8 | 83 | /// Return complex hyperbolic cosine of @a z. |
54c1bf78 | 84 | template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&); |
ffcec5c8 | 85 | /// Return complex base e exponential of @a z. |
54c1bf78 | 86 | template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&); |
ffcec5c8 | 87 | /// Return complex natural logarithm of @a z. |
54c1bf78 | 88 | template<typename _Tp> complex<_Tp> log(const complex<_Tp>&); |
ffcec5c8 | 89 | /// Return complex base 10 logarithm of @a z. |
54c1bf78 | 90 | template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&); |
3fd29fa9 | 91 | /// Return @a x to the @a y'th power. |
54c1bf78 | 92 | template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int); |
ffcec5c8 | 93 | /// Return @a x to the @a y'th power. |
54c1bf78 | 94 | template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&); |
ffcec5c8 | 95 | /// Return @a x to the @a y'th power. |
54c1bf78 | 96 | template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, |
a4ddde0d | 97 | const complex<_Tp>&); |
ffcec5c8 | 98 | /// Return @a x to the @a y'th power. |
54c1bf78 | 99 | template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&); |
ffcec5c8 | 100 | /// Return complex sine of @a z. |
54c1bf78 | 101 | template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&); |
ffcec5c8 | 102 | /// Return complex hyperbolic sine of @a z. |
54c1bf78 | 103 | template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&); |
ffcec5c8 | 104 | /// Return complex square root of @a z. |
54c1bf78 | 105 | template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&); |
ffcec5c8 | 106 | /// Return complex tangent of @a z. |
54c1bf78 | 107 | template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&); |
ffcec5c8 | 108 | /// Return complex hyperbolic tangent of @a z. |
54c1bf78 BK |
109 | template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&); |
110 | ||
111 | ||
112 | // 26.2.2 Primary template class complex | |
ffcec5c8 JQ |
113 | /** |
114 | * Template to represent complex numbers. | |
115 | * | |
116 | * Specializations for float, double, and long double are part of the | |
117 | * library. Results with any other type are not guaranteed. | |
118 | * | |
119 | * @param Tp Type of real and imaginary values. | |
120 | */ | |
54c1bf78 | 121 | template<typename _Tp> |
a4ddde0d | 122 | struct complex |
54c1bf78 | 123 | { |
ffcec5c8 | 124 | /// Value typedef. |
54c1bf78 BK |
125 | typedef _Tp value_type; |
126 | ||
ffcec5c8 JQ |
127 | /// Default constructor. First parameter is x, second parameter is y. |
128 | /// Unspecified parameters default to 0. | |
94a86be0 | 129 | _GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp()) |
2acceeac | 130 | : _M_real(__r), _M_imag(__i) { } |
54c1bf78 | 131 | |
fa794dc6 JW |
132 | // Let the compiler synthesize the copy constructor |
133 | #if __cplusplus >= 201103L | |
134 | constexpr complex(const complex&) = default; | |
135 | #endif | |
136 | ||
137 | /// Converting constructor. | |
54c1bf78 | 138 | template<typename _Up> |
94a86be0 | 139 | _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z) |
2acceeac | 140 | : _M_real(__z.real()), _M_imag(__z.imag()) { } |
3b3bfc0e | 141 | |
734f5023 | 142 | #if __cplusplus >= 201103L |
23ed71c6 PC |
143 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
144 | // DR 387. std::complex over-encapsulated. | |
3b31a727 | 145 | _GLIBCXX_ABI_TAG_CXX11 |
94a86be0 | 146 | constexpr _Tp |
327a79a5 | 147 | real() const { return _M_real; } |
23ed71c6 | 148 | |
3b31a727 | 149 | _GLIBCXX_ABI_TAG_CXX11 |
94a86be0 | 150 | constexpr _Tp |
327a79a5 | 151 | imag() const { return _M_imag; } |
23ed71c6 | 152 | #else |
ffcec5c8 | 153 | /// Return real part of complex number. |
94a86be0 BK |
154 | _Tp& |
155 | real() { return _M_real; } | |
2acceeac | 156 | |
ffcec5c8 | 157 | /// Return real part of complex number. |
94a86be0 BK |
158 | const _Tp& |
159 | real() const { return _M_real; } | |
2acceeac | 160 | |
ffcec5c8 | 161 | /// Return imaginary part of complex number. |
94a86be0 BK |
162 | _Tp& |
163 | imag() { return _M_imag; } | |
2acceeac | 164 | |
ffcec5c8 | 165 | /// Return imaginary part of complex number. |
94a86be0 BK |
166 | const _Tp& |
167 | imag() const { return _M_imag; } | |
23ed71c6 PC |
168 | #endif |
169 | ||
170 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
171 | // DR 387. std::complex over-encapsulated. | |
94a86be0 BK |
172 | void |
173 | real(_Tp __val) { _M_real = __val; } | |
23ed71c6 | 174 | |
94a86be0 BK |
175 | void |
176 | imag(_Tp __val) { _M_imag = __val; } | |
54c1bf78 | 177 | |
fa794dc6 | 178 | /// Assign a scalar to this complex number. |
54c1bf78 | 179 | complex<_Tp>& operator=(const _Tp&); |
2acceeac | 180 | |
fa794dc6 | 181 | /// Add a scalar to this complex number. |
2acceeac PC |
182 | // 26.2.5/1 |
183 | complex<_Tp>& | |
184 | operator+=(const _Tp& __t) | |
185 | { | |
186 | _M_real += __t; | |
187 | return *this; | |
188 | } | |
189 | ||
fa794dc6 | 190 | /// Subtract a scalar from this complex number. |
2acceeac PC |
191 | // 26.2.5/3 |
192 | complex<_Tp>& | |
193 | operator-=(const _Tp& __t) | |
194 | { | |
195 | _M_real -= __t; | |
196 | return *this; | |
197 | } | |
198 | ||
fa794dc6 | 199 | /// Multiply this complex number by a scalar. |
54c1bf78 | 200 | complex<_Tp>& operator*=(const _Tp&); |
fa794dc6 | 201 | /// Divide this complex number by a scalar. |
54c1bf78 BK |
202 | complex<_Tp>& operator/=(const _Tp&); |
203 | ||
fa794dc6 JW |
204 | // Let the compiler synthesize the copy assignment operator |
205 | #if __cplusplus >= 201103L | |
206 | complex& operator=(const complex&) = default; | |
207 | #endif | |
208 | ||
209 | /// Assign another complex number to this one. | |
54c1bf78 BK |
210 | template<typename _Up> |
211 | complex<_Tp>& operator=(const complex<_Up>&); | |
fa794dc6 | 212 | /// Add another complex number to this one. |
54c1bf78 BK |
213 | template<typename _Up> |
214 | complex<_Tp>& operator+=(const complex<_Up>&); | |
fa794dc6 | 215 | /// Subtract another complex number from this one. |
54c1bf78 BK |
216 | template<typename _Up> |
217 | complex<_Tp>& operator-=(const complex<_Up>&); | |
fa794dc6 | 218 | /// Multiply this complex number by another. |
54c1bf78 BK |
219 | template<typename _Up> |
220 | complex<_Tp>& operator*=(const complex<_Up>&); | |
fa794dc6 | 221 | /// Divide this complex number by another. |
54c1bf78 BK |
222 | template<typename _Up> |
223 | complex<_Tp>& operator/=(const complex<_Up>&); | |
224 | ||
3fa591d4 | 225 | _GLIBCXX_CONSTEXPR complex __rep() const |
2acceeac | 226 | { return *this; } |
a4ddde0d | 227 | |
54c1bf78 | 228 | private: |
3b3bfc0e GDR |
229 | _Tp _M_real; |
230 | _Tp _M_imag; | |
54c1bf78 BK |
231 | }; |
232 | ||
54c1bf78 BK |
233 | template<typename _Tp> |
234 | complex<_Tp>& | |
235 | complex<_Tp>::operator=(const _Tp& __t) | |
236 | { | |
237 | _M_real = __t; | |
238 | _M_imag = _Tp(); | |
239 | return *this; | |
240 | } | |
241 | ||
54c1bf78 BK |
242 | // 26.2.5/5 |
243 | template<typename _Tp> | |
244 | complex<_Tp>& | |
245 | complex<_Tp>::operator*=(const _Tp& __t) | |
246 | { | |
247 | _M_real *= __t; | |
248 | _M_imag *= __t; | |
249 | return *this; | |
250 | } | |
251 | ||
252 | // 26.2.5/7 | |
253 | template<typename _Tp> | |
254 | complex<_Tp>& | |
255 | complex<_Tp>::operator/=(const _Tp& __t) | |
256 | { | |
257 | _M_real /= __t; | |
258 | _M_imag /= __t; | |
259 | return *this; | |
260 | } | |
261 | ||
262 | template<typename _Tp> | |
263 | template<typename _Up> | |
264 | complex<_Tp>& | |
265 | complex<_Tp>::operator=(const complex<_Up>& __z) | |
266 | { | |
267 | _M_real = __z.real(); | |
268 | _M_imag = __z.imag(); | |
269 | return *this; | |
270 | } | |
271 | ||
272 | // 26.2.5/9 | |
273 | template<typename _Tp> | |
274 | template<typename _Up> | |
275 | complex<_Tp>& | |
276 | complex<_Tp>::operator+=(const complex<_Up>& __z) | |
277 | { | |
278 | _M_real += __z.real(); | |
279 | _M_imag += __z.imag(); | |
280 | return *this; | |
281 | } | |
282 | ||
283 | // 26.2.5/11 | |
284 | template<typename _Tp> | |
285 | template<typename _Up> | |
286 | complex<_Tp>& | |
287 | complex<_Tp>::operator-=(const complex<_Up>& __z) | |
288 | { | |
289 | _M_real -= __z.real(); | |
290 | _M_imag -= __z.imag(); | |
291 | return *this; | |
292 | } | |
293 | ||
294 | // 26.2.5/13 | |
295 | // XXX: This is a grammar school implementation. | |
296 | template<typename _Tp> | |
297 | template<typename _Up> | |
298 | complex<_Tp>& | |
299 | complex<_Tp>::operator*=(const complex<_Up>& __z) | |
300 | { | |
301 | const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag(); | |
302 | _M_imag = _M_real * __z.imag() + _M_imag * __z.real(); | |
303 | _M_real = __r; | |
304 | return *this; | |
305 | } | |
306 | ||
307 | // 26.2.5/15 | |
308 | // XXX: This is a grammar school implementation. | |
309 | template<typename _Tp> | |
310 | template<typename _Up> | |
311 | complex<_Tp>& | |
312 | complex<_Tp>::operator/=(const complex<_Up>& __z) | |
313 | { | |
314 | const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag(); | |
eb9a4231 | 315 | const _Tp __n = std::norm(__z); |
54c1bf78 BK |
316 | _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n; |
317 | _M_real = __r / __n; | |
318 | return *this; | |
319 | } | |
320 | ||
321 | // Operators: | |
ffcec5c8 JQ |
322 | //@{ |
323 | /// Return new complex value @a x plus @a y. | |
54c1bf78 BK |
324 | template<typename _Tp> |
325 | inline complex<_Tp> | |
326 | operator+(const complex<_Tp>& __x, const complex<_Tp>& __y) | |
3b3bfc0e GDR |
327 | { |
328 | complex<_Tp> __r = __x; | |
329 | __r += __y; | |
330 | return __r; | |
331 | } | |
54c1bf78 BK |
332 | |
333 | template<typename _Tp> | |
334 | inline complex<_Tp> | |
335 | operator+(const complex<_Tp>& __x, const _Tp& __y) | |
3b3bfc0e GDR |
336 | { |
337 | complex<_Tp> __r = __x; | |
23cdf8e8 | 338 | __r += __y; |
3b3bfc0e GDR |
339 | return __r; |
340 | } | |
54c1bf78 BK |
341 | |
342 | template<typename _Tp> | |
343 | inline complex<_Tp> | |
344 | operator+(const _Tp& __x, const complex<_Tp>& __y) | |
3b3bfc0e GDR |
345 | { |
346 | complex<_Tp> __r = __y; | |
23cdf8e8 | 347 | __r += __x; |
3b3bfc0e GDR |
348 | return __r; |
349 | } | |
ffcec5c8 | 350 | //@} |
54c1bf78 | 351 | |
ffcec5c8 JQ |
352 | //@{ |
353 | /// Return new complex value @a x minus @a y. | |
54c1bf78 BK |
354 | template<typename _Tp> |
355 | inline complex<_Tp> | |
356 | operator-(const complex<_Tp>& __x, const complex<_Tp>& __y) | |
3b3bfc0e GDR |
357 | { |
358 | complex<_Tp> __r = __x; | |
359 | __r -= __y; | |
360 | return __r; | |
361 | } | |
54c1bf78 BK |
362 | |
363 | template<typename _Tp> | |
364 | inline complex<_Tp> | |
365 | operator-(const complex<_Tp>& __x, const _Tp& __y) | |
3b3bfc0e GDR |
366 | { |
367 | complex<_Tp> __r = __x; | |
23cdf8e8 | 368 | __r -= __y; |
3b3bfc0e GDR |
369 | return __r; |
370 | } | |
54c1bf78 BK |
371 | |
372 | template<typename _Tp> | |
373 | inline complex<_Tp> | |
374 | operator-(const _Tp& __x, const complex<_Tp>& __y) | |
3b3bfc0e GDR |
375 | { |
376 | complex<_Tp> __r(__x, -__y.imag()); | |
23cdf8e8 | 377 | __r -= __y.real(); |
3b3bfc0e GDR |
378 | return __r; |
379 | } | |
ffcec5c8 | 380 | //@} |
54c1bf78 | 381 | |
ffcec5c8 JQ |
382 | //@{ |
383 | /// Return new complex value @a x times @a y. | |
54c1bf78 BK |
384 | template<typename _Tp> |
385 | inline complex<_Tp> | |
386 | operator*(const complex<_Tp>& __x, const complex<_Tp>& __y) | |
3b3bfc0e GDR |
387 | { |
388 | complex<_Tp> __r = __x; | |
389 | __r *= __y; | |
390 | return __r; | |
391 | } | |
54c1bf78 BK |
392 | |
393 | template<typename _Tp> | |
394 | inline complex<_Tp> | |
395 | operator*(const complex<_Tp>& __x, const _Tp& __y) | |
3b3bfc0e GDR |
396 | { |
397 | complex<_Tp> __r = __x; | |
398 | __r *= __y; | |
399 | return __r; | |
400 | } | |
54c1bf78 BK |
401 | |
402 | template<typename _Tp> | |
403 | inline complex<_Tp> | |
404 | operator*(const _Tp& __x, const complex<_Tp>& __y) | |
3b3bfc0e GDR |
405 | { |
406 | complex<_Tp> __r = __y; | |
407 | __r *= __x; | |
408 | return __r; | |
409 | } | |
ffcec5c8 | 410 | //@} |
54c1bf78 | 411 | |
ffcec5c8 JQ |
412 | //@{ |
413 | /// Return new complex value @a x divided by @a y. | |
54c1bf78 BK |
414 | template<typename _Tp> |
415 | inline complex<_Tp> | |
416 | operator/(const complex<_Tp>& __x, const complex<_Tp>& __y) | |
3b3bfc0e GDR |
417 | { |
418 | complex<_Tp> __r = __x; | |
419 | __r /= __y; | |
420 | return __r; | |
421 | } | |
54c1bf78 BK |
422 | |
423 | template<typename _Tp> | |
424 | inline complex<_Tp> | |
425 | operator/(const complex<_Tp>& __x, const _Tp& __y) | |
3b3bfc0e GDR |
426 | { |
427 | complex<_Tp> __r = __x; | |
428 | __r /= __y; | |
429 | return __r; | |
430 | } | |
54c1bf78 BK |
431 | |
432 | template<typename _Tp> | |
433 | inline complex<_Tp> | |
434 | operator/(const _Tp& __x, const complex<_Tp>& __y) | |
3b3bfc0e GDR |
435 | { |
436 | complex<_Tp> __r = __x; | |
437 | __r /= __y; | |
438 | return __r; | |
439 | } | |
ffcec5c8 | 440 | //@} |
54c1bf78 | 441 | |
ffcec5c8 | 442 | /// Return @a x. |
54c1bf78 BK |
443 | template<typename _Tp> |
444 | inline complex<_Tp> | |
445 | operator+(const complex<_Tp>& __x) | |
446 | { return __x; } | |
447 | ||
ffcec5c8 | 448 | /// Return complex negation of @a x. |
54c1bf78 BK |
449 | template<typename _Tp> |
450 | inline complex<_Tp> | |
451 | operator-(const complex<_Tp>& __x) | |
452 | { return complex<_Tp>(-__x.real(), -__x.imag()); } | |
453 | ||
ffcec5c8 JQ |
454 | //@{ |
455 | /// Return true if @a x is equal to @a y. | |
54c1bf78 | 456 | template<typename _Tp> |
6684c443 | 457 | inline _GLIBCXX_CONSTEXPR bool |
54c1bf78 BK |
458 | operator==(const complex<_Tp>& __x, const complex<_Tp>& __y) |
459 | { return __x.real() == __y.real() && __x.imag() == __y.imag(); } | |
460 | ||
461 | template<typename _Tp> | |
6684c443 | 462 | inline _GLIBCXX_CONSTEXPR bool |
54c1bf78 BK |
463 | operator==(const complex<_Tp>& __x, const _Tp& __y) |
464 | { return __x.real() == __y && __x.imag() == _Tp(); } | |
465 | ||
466 | template<typename _Tp> | |
6684c443 | 467 | inline _GLIBCXX_CONSTEXPR bool |
54c1bf78 BK |
468 | operator==(const _Tp& __x, const complex<_Tp>& __y) |
469 | { return __x == __y.real() && _Tp() == __y.imag(); } | |
ffcec5c8 | 470 | //@} |
54c1bf78 | 471 | |
ffcec5c8 JQ |
472 | //@{ |
473 | /// Return false if @a x is equal to @a y. | |
54c1bf78 | 474 | template<typename _Tp> |
6684c443 | 475 | inline _GLIBCXX_CONSTEXPR bool |
54c1bf78 BK |
476 | operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y) |
477 | { return __x.real() != __y.real() || __x.imag() != __y.imag(); } | |
478 | ||
479 | template<typename _Tp> | |
6684c443 | 480 | inline _GLIBCXX_CONSTEXPR bool |
54c1bf78 BK |
481 | operator!=(const complex<_Tp>& __x, const _Tp& __y) |
482 | { return __x.real() != __y || __x.imag() != _Tp(); } | |
483 | ||
484 | template<typename _Tp> | |
6684c443 | 485 | inline _GLIBCXX_CONSTEXPR bool |
54c1bf78 BK |
486 | operator!=(const _Tp& __x, const complex<_Tp>& __y) |
487 | { return __x != __y.real() || _Tp() != __y.imag(); } | |
ffcec5c8 | 488 | //@} |
54c1bf78 | 489 | |
ffcec5c8 | 490 | /// Extraction operator for complex values. |
54c1bf78 BK |
491 | template<typename _Tp, typename _CharT, class _Traits> |
492 | basic_istream<_CharT, _Traits>& | |
493 | operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x) | |
494 | { | |
495 | _Tp __re_x, __im_x; | |
496 | _CharT __ch; | |
497 | __is >> __ch; | |
498 | if (__ch == '(') | |
499 | { | |
500 | __is >> __re_x >> __ch; | |
501 | if (__ch == ',') | |
502 | { | |
503 | __is >> __im_x >> __ch; | |
504 | if (__ch == ')') | |
505 | __x = complex<_Tp>(__re_x, __im_x); | |
506 | else | |
507 | __is.setstate(ios_base::failbit); | |
508 | } | |
509 | else if (__ch == ')') | |
3b3bfc0e | 510 | __x = __re_x; |
54c1bf78 BK |
511 | else |
512 | __is.setstate(ios_base::failbit); | |
513 | } | |
514 | else | |
515 | { | |
516 | __is.putback(__ch); | |
517 | __is >> __re_x; | |
3b3bfc0e | 518 | __x = __re_x; |
54c1bf78 BK |
519 | } |
520 | return __is; | |
521 | } | |
522 | ||
ffcec5c8 | 523 | /// Insertion operator for complex values. |
54c1bf78 BK |
524 | template<typename _Tp, typename _CharT, class _Traits> |
525 | basic_ostream<_CharT, _Traits>& | |
526 | operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) | |
527 | { | |
528 | basic_ostringstream<_CharT, _Traits> __s; | |
529 | __s.flags(__os.flags()); | |
530 | __s.imbue(__os.getloc()); | |
531 | __s.precision(__os.precision()); | |
f815521c | 532 | __s << '(' << __x.real() << ',' << __x.imag() << ')'; |
54c1bf78 BK |
533 | return __os << __s.str(); |
534 | } | |
535 | ||
536 | // Values | |
734f5023 | 537 | #if __cplusplus >= 201103L |
23ed71c6 | 538 | template<typename _Tp> |
a4eeb822 | 539 | constexpr _Tp |
23ed71c6 PC |
540 | real(const complex<_Tp>& __z) |
541 | { return __z.real(); } | |
a4eeb822 | 542 | |
23ed71c6 | 543 | template<typename _Tp> |
a4eeb822 | 544 | constexpr _Tp |
23ed71c6 PC |
545 | imag(const complex<_Tp>& __z) |
546 | { return __z.imag(); } | |
547 | #else | |
54c1bf78 | 548 | template<typename _Tp> |
3b3bfc0e GDR |
549 | inline _Tp& |
550 | real(complex<_Tp>& __z) | |
551 | { return __z.real(); } | |
552 | ||
553 | template<typename _Tp> | |
554 | inline const _Tp& | |
54c1bf78 BK |
555 | real(const complex<_Tp>& __z) |
556 | { return __z.real(); } | |
557 | ||
558 | template<typename _Tp> | |
3b3bfc0e GDR |
559 | inline _Tp& |
560 | imag(complex<_Tp>& __z) | |
561 | { return __z.imag(); } | |
562 | ||
563 | template<typename _Tp> | |
564 | inline const _Tp& | |
54c1bf78 BK |
565 | imag(const complex<_Tp>& __z) |
566 | { return __z.imag(); } | |
23ed71c6 | 567 | #endif |
54c1bf78 | 568 | |
a4ddde0d | 569 | // 26.2.7/3 abs(__z): Returns the magnitude of __z. |
54c1bf78 BK |
570 | template<typename _Tp> |
571 | inline _Tp | |
a4ddde0d | 572 | __complex_abs(const complex<_Tp>& __z) |
54c1bf78 BK |
573 | { |
574 | _Tp __x = __z.real(); | |
575 | _Tp __y = __z.imag(); | |
a8784c4c | 576 | const _Tp __s = std::max(abs(__x), abs(__y)); |
54c1bf78 BK |
577 | if (__s == _Tp()) // well ... |
578 | return __s; | |
579 | __x /= __s; | |
580 | __y /= __s; | |
52e6723c | 581 | return __s * sqrt(__x * __x + __y * __y); |
54c1bf78 BK |
582 | } |
583 | ||
ab9b9d2c | 584 | #if _GLIBCXX_USE_C99_COMPLEX |
a4ddde0d GDR |
585 | inline float |
586 | __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); } | |
587 | ||
588 | inline double | |
589 | __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); } | |
590 | ||
591 | inline long double | |
592 | __complex_abs(const __complex__ long double& __z) | |
52e6723c BK |
593 | { return __builtin_cabsl(__z); } |
594 | ||
a4ddde0d GDR |
595 | template<typename _Tp> |
596 | inline _Tp | |
597 | abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); } | |
52e6723c BK |
598 | #else |
599 | template<typename _Tp> | |
600 | inline _Tp | |
601 | abs(const complex<_Tp>& __z) { return __complex_abs(__z); } | |
602 | #endif | |
a4ddde0d GDR |
603 | |
604 | ||
605 | // 26.2.7/4: arg(__z): Returns the phase angle of __z. | |
54c1bf78 BK |
606 | template<typename _Tp> |
607 | inline _Tp | |
a4ddde0d | 608 | __complex_arg(const complex<_Tp>& __z) |
52e6723c | 609 | { return atan2(__z.imag(), __z.real()); } |
a4ddde0d | 610 | |
ab9b9d2c | 611 | #if _GLIBCXX_USE_C99_COMPLEX |
a4ddde0d GDR |
612 | inline float |
613 | __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); } | |
614 | ||
615 | inline double | |
616 | __complex_arg(__complex__ double __z) { return __builtin_carg(__z); } | |
617 | ||
618 | inline long double | |
619 | __complex_arg(const __complex__ long double& __z) | |
620 | { return __builtin_cargl(__z); } | |
621 | ||
622 | template<typename _Tp> | |
623 | inline _Tp | |
624 | arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); } | |
52e6723c BK |
625 | #else |
626 | template<typename _Tp> | |
627 | inline _Tp | |
628 | arg(const complex<_Tp>& __z) { return __complex_arg(__z); } | |
629 | #endif | |
54c1bf78 | 630 | |
28dac70a | 631 | // 26.2.7/5: norm(__z) returns the squared magnitude of __z. |
54c1bf78 | 632 | // As defined, norm() is -not- a norm is the common mathematical |
fa794dc6 | 633 | // sense used in numerics. The helper class _Norm_helper<> tries to |
54c1bf78 BK |
634 | // distinguish between builtin floating point and the rest, so as |
635 | // to deliver an answer as close as possible to the real value. | |
636 | template<bool> | |
637 | struct _Norm_helper | |
638 | { | |
639 | template<typename _Tp> | |
640 | static inline _Tp _S_do_it(const complex<_Tp>& __z) | |
641 | { | |
642 | const _Tp __x = __z.real(); | |
643 | const _Tp __y = __z.imag(); | |
644 | return __x * __x + __y * __y; | |
645 | } | |
646 | }; | |
647 | ||
648 | template<> | |
649 | struct _Norm_helper<true> | |
650 | { | |
651 | template<typename _Tp> | |
652 | static inline _Tp _S_do_it(const complex<_Tp>& __z) | |
653 | { | |
eb9a4231 | 654 | _Tp __res = std::abs(__z); |
54c1bf78 BK |
655 | return __res * __res; |
656 | } | |
657 | }; | |
658 | ||
659 | template<typename _Tp> | |
660 | inline _Tp | |
661 | norm(const complex<_Tp>& __z) | |
662 | { | |
4d73fac9 | 663 | return _Norm_helper<__is_floating<_Tp>::__value |
52e6723c | 664 | && !_GLIBCXX_FAST_MATH>::_S_do_it(__z); |
54c1bf78 BK |
665 | } |
666 | ||
667 | template<typename _Tp> | |
668 | inline complex<_Tp> | |
669 | polar(const _Tp& __rho, const _Tp& __theta) | |
a8784c4c | 670 | { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); } |
54c1bf78 BK |
671 | |
672 | template<typename _Tp> | |
673 | inline complex<_Tp> | |
674 | conj(const complex<_Tp>& __z) | |
675 | { return complex<_Tp>(__z.real(), -__z.imag()); } | |
676 | ||
677 | // Transcendentals | |
a4ddde0d GDR |
678 | |
679 | // 26.2.8/1 cos(__z): Returns the cosine of __z. | |
54c1bf78 BK |
680 | template<typename _Tp> |
681 | inline complex<_Tp> | |
a4ddde0d | 682 | __complex_cos(const complex<_Tp>& __z) |
54c1bf78 BK |
683 | { |
684 | const _Tp __x = __z.real(); | |
685 | const _Tp __y = __z.imag(); | |
a8784c4c | 686 | return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y)); |
54c1bf78 BK |
687 | } |
688 | ||
ab9b9d2c | 689 | #if _GLIBCXX_USE_C99_COMPLEX |
a4ddde0d GDR |
690 | inline __complex__ float |
691 | __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); } | |
692 | ||
693 | inline __complex__ double | |
694 | __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); } | |
695 | ||
696 | inline __complex__ long double | |
697 | __complex_cos(const __complex__ long double& __z) | |
698 | { return __builtin_ccosl(__z); } | |
52e6723c | 699 | |
54c1bf78 BK |
700 | template<typename _Tp> |
701 | inline complex<_Tp> | |
a4ddde0d | 702 | cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); } |
52e6723c BK |
703 | #else |
704 | template<typename _Tp> | |
705 | inline complex<_Tp> | |
706 | cos(const complex<_Tp>& __z) { return __complex_cos(__z); } | |
707 | #endif | |
a4ddde0d GDR |
708 | |
709 | // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z. | |
710 | template<typename _Tp> | |
711 | inline complex<_Tp> | |
712 | __complex_cosh(const complex<_Tp>& __z) | |
9ef313e3 JD |
713 | { |
714 | const _Tp __x = __z.real(); | |
715 | const _Tp __y = __z.imag(); | |
716 | return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y)); | |
717 | } | |
a4ddde0d | 718 | |
ab9b9d2c | 719 | #if _GLIBCXX_USE_C99_COMPLEX |
a4ddde0d GDR |
720 | inline __complex__ float |
721 | __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); } | |
722 | ||
723 | inline __complex__ double | |
724 | __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); } | |
725 | ||
726 | inline __complex__ long double | |
727 | __complex_cosh(const __complex__ long double& __z) | |
728 | { return __builtin_ccoshl(__z); } | |
54c1bf78 BK |
729 | |
730 | template<typename _Tp> | |
731 | inline complex<_Tp> | |
a4ddde0d | 732 | cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); } |
52e6723c BK |
733 | #else |
734 | template<typename _Tp> | |
735 | inline complex<_Tp> | |
736 | cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); } | |
737 | #endif | |
a4ddde0d GDR |
738 | |
739 | // 26.2.8/3 exp(__z): Returns the complex base e exponential of x | |
740 | template<typename _Tp> | |
741 | inline complex<_Tp> | |
742 | __complex_exp(const complex<_Tp>& __z) | |
742f66e7 | 743 | { return std::polar<_Tp>(exp(__z.real()), __z.imag()); } |
54c1bf78 | 744 | |
ab9b9d2c | 745 | #if _GLIBCXX_USE_C99_COMPLEX |
a4ddde0d GDR |
746 | inline __complex__ float |
747 | __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); } | |
748 | ||
749 | inline __complex__ double | |
750 | __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); } | |
751 | ||
752 | inline __complex__ long double | |
9ef313e3 JD |
753 | __complex_exp(const __complex__ long double& __z) |
754 | { return __builtin_cexpl(__z); } | |
52e6723c | 755 | |
54c1bf78 BK |
756 | template<typename _Tp> |
757 | inline complex<_Tp> | |
a4ddde0d | 758 | exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); } |
52e6723c BK |
759 | #else |
760 | template<typename _Tp> | |
761 | inline complex<_Tp> | |
762 | exp(const complex<_Tp>& __z) { return __complex_exp(__z); } | |
763 | #endif | |
a4ddde0d | 764 | |
28dac70a | 765 | // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z. |
a4ddde0d GDR |
766 | // The branch cut is along the negative axis. |
767 | template<typename _Tp> | |
768 | inline complex<_Tp> | |
769 | __complex_log(const complex<_Tp>& __z) | |
a8784c4c | 770 | { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); } |
54c1bf78 | 771 | |
d5c405cc | 772 | #if _GLIBCXX_USE_C99_COMPLEX |
a4ddde0d GDR |
773 | inline __complex__ float |
774 | __complex_log(__complex__ float __z) { return __builtin_clogf(__z); } | |
775 | ||
776 | inline __complex__ double | |
777 | __complex_log(__complex__ double __z) { return __builtin_clog(__z); } | |
778 | ||
779 | inline __complex__ long double | |
780 | __complex_log(const __complex__ long double& __z) | |
d5c405cc | 781 | { return __builtin_clogl(__z); } |
a4ddde0d | 782 | |
d5c405cc PC |
783 | template<typename _Tp> |
784 | inline complex<_Tp> | |
785 | log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); } | |
786 | #else | |
a4ddde0d GDR |
787 | template<typename _Tp> |
788 | inline complex<_Tp> | |
b53dcf3e | 789 | log(const complex<_Tp>& __z) { return __complex_log(__z); } |
d5c405cc | 790 | #endif |
a4ddde0d | 791 | |
54c1bf78 BK |
792 | template<typename _Tp> |
793 | inline complex<_Tp> | |
794 | log10(const complex<_Tp>& __z) | |
a8784c4c | 795 | { return std::log(__z) / log(_Tp(10.0)); } |
54c1bf78 | 796 | |
a4ddde0d | 797 | // 26.2.8/10 sin(__z): Returns the sine of __z. |
54c1bf78 BK |
798 | template<typename _Tp> |
799 | inline complex<_Tp> | |
a4ddde0d | 800 | __complex_sin(const complex<_Tp>& __z) |
54c1bf78 BK |
801 | { |
802 | const _Tp __x = __z.real(); | |
803 | const _Tp __y = __z.imag(); | |
a8784c4c | 804 | return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y)); |
54c1bf78 BK |
805 | } |
806 | ||
ab9b9d2c | 807 | #if _GLIBCXX_USE_C99_COMPLEX |
a4ddde0d GDR |
808 | inline __complex__ float |
809 | __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); } | |
810 | ||
811 | inline __complex__ double | |
812 | __complex_sin(__complex__ double __z) { return __builtin_csin(__z); } | |
813 | ||
814 | inline __complex__ long double | |
815 | __complex_sin(const __complex__ long double& __z) | |
816 | { return __builtin_csinl(__z); } | |
817 | ||
54c1bf78 BK |
818 | template<typename _Tp> |
819 | inline complex<_Tp> | |
9ef313e3 | 820 | sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); } |
52e6723c BK |
821 | #else |
822 | template<typename _Tp> | |
823 | inline complex<_Tp> | |
824 | sin(const complex<_Tp>& __z) { return __complex_sin(__z); } | |
825 | #endif | |
a4ddde0d GDR |
826 | |
827 | // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z. | |
828 | template<typename _Tp> | |
829 | inline complex<_Tp> | |
830 | __complex_sinh(const complex<_Tp>& __z) | |
54c1bf78 BK |
831 | { |
832 | const _Tp __x = __z.real(); | |
833 | const _Tp __y = __z.imag(); | |
a8784c4c | 834 | return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y)); |
54c1bf78 BK |
835 | } |
836 | ||
ab9b9d2c | 837 | #if _GLIBCXX_USE_C99_COMPLEX |
a4ddde0d GDR |
838 | inline __complex__ float |
839 | __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); } | |
840 | ||
841 | inline __complex__ double | |
842 | __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); } | |
843 | ||
844 | inline __complex__ long double | |
845 | __complex_sinh(const __complex__ long double& __z) | |
846 | { return __builtin_csinhl(__z); } | |
847 | ||
848 | template<typename _Tp> | |
849 | inline complex<_Tp> | |
850 | sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); } | |
52e6723c BK |
851 | #else |
852 | template<typename _Tp> | |
853 | inline complex<_Tp> | |
854 | sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); } | |
855 | #endif | |
a4ddde0d GDR |
856 | |
857 | // 26.2.8/13 sqrt(__z): Returns the complex square root of __z. | |
858 | // The branch cut is on the negative axis. | |
54c1bf78 BK |
859 | template<typename _Tp> |
860 | complex<_Tp> | |
a4ddde0d | 861 | __complex_sqrt(const complex<_Tp>& __z) |
54c1bf78 BK |
862 | { |
863 | _Tp __x = __z.real(); | |
864 | _Tp __y = __z.imag(); | |
865 | ||
866 | if (__x == _Tp()) | |
867 | { | |
a8784c4c | 868 | _Tp __t = sqrt(abs(__y) / 2); |
54c1bf78 BK |
869 | return complex<_Tp>(__t, __y < _Tp() ? -__t : __t); |
870 | } | |
871 | else | |
872 | { | |
a8784c4c | 873 | _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x))); |
54c1bf78 BK |
874 | _Tp __u = __t / 2; |
875 | return __x > _Tp() | |
876 | ? complex<_Tp>(__u, __y / __t) | |
a8784c4c | 877 | : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u); |
54c1bf78 BK |
878 | } |
879 | } | |
880 | ||
ab9b9d2c | 881 | #if _GLIBCXX_USE_C99_COMPLEX |
a4ddde0d GDR |
882 | inline __complex__ float |
883 | __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); } | |
884 | ||
885 | inline __complex__ double | |
886 | __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); } | |
887 | ||
888 | inline __complex__ long double | |
889 | __complex_sqrt(const __complex__ long double& __z) | |
890 | { return __builtin_csqrtl(__z); } | |
891 | ||
54c1bf78 BK |
892 | template<typename _Tp> |
893 | inline complex<_Tp> | |
a4ddde0d | 894 | sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); } |
52e6723c BK |
895 | #else |
896 | template<typename _Tp> | |
897 | inline complex<_Tp> | |
898 | sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); } | |
899 | #endif | |
54c1bf78 | 900 | |
a4ddde0d GDR |
901 | // 26.2.8/14 tan(__z): Return the complex tangent of __z. |
902 | ||
54c1bf78 BK |
903 | template<typename _Tp> |
904 | inline complex<_Tp> | |
a4ddde0d GDR |
905 | __complex_tan(const complex<_Tp>& __z) |
906 | { return std::sin(__z) / std::cos(__z); } | |
907 | ||
ab9b9d2c | 908 | #if _GLIBCXX_USE_C99_COMPLEX |
a4ddde0d GDR |
909 | inline __complex__ float |
910 | __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); } | |
911 | ||
912 | inline __complex__ double | |
913 | __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); } | |
914 | ||
915 | inline __complex__ long double | |
916 | __complex_tan(const __complex__ long double& __z) | |
917 | { return __builtin_ctanl(__z); } | |
918 | ||
919 | template<typename _Tp> | |
920 | inline complex<_Tp> | |
921 | tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); } | |
52e6723c BK |
922 | #else |
923 | template<typename _Tp> | |
924 | inline complex<_Tp> | |
925 | tan(const complex<_Tp>& __z) { return __complex_tan(__z); } | |
926 | #endif | |
927 | ||
a4ddde0d GDR |
928 | |
929 | // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z. | |
930 | ||
931 | template<typename _Tp> | |
932 | inline complex<_Tp> | |
933 | __complex_tanh(const complex<_Tp>& __z) | |
934 | { return std::sinh(__z) / std::cosh(__z); } | |
935 | ||
ab9b9d2c | 936 | #if _GLIBCXX_USE_C99_COMPLEX |
a4ddde0d GDR |
937 | inline __complex__ float |
938 | __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); } | |
939 | ||
940 | inline __complex__ double | |
941 | __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); } | |
942 | ||
943 | inline __complex__ long double | |
944 | __complex_tanh(const __complex__ long double& __z) | |
945 | { return __builtin_ctanhl(__z); } | |
54c1bf78 | 946 | |
a4ddde0d GDR |
947 | template<typename _Tp> |
948 | inline complex<_Tp> | |
949 | tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); } | |
52e6723c BK |
950 | #else |
951 | template<typename _Tp> | |
952 | inline complex<_Tp> | |
953 | tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); } | |
954 | #endif | |
955 | ||
a4ddde0d GDR |
956 | |
957 | // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x | |
958 | // raised to the __y-th power. The branch | |
959 | // cut is on the negative axis. | |
f3961bdf PC |
960 | template<typename _Tp> |
961 | complex<_Tp> | |
962 | __complex_pow_unsigned(complex<_Tp> __x, unsigned __n) | |
963 | { | |
964 | complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1); | |
965 | ||
966 | while (__n >>= 1) | |
967 | { | |
968 | __x *= __x; | |
969 | if (__n % 2) | |
970 | __y *= __x; | |
971 | } | |
972 | ||
973 | return __y; | |
974 | } | |
975 | ||
4e30cb71 | 976 | // In C++11 mode we used to implement the resolution of |
3fd29fa9 | 977 | // DR 844. complex pow return type is ambiguous. |
4e30cb71 PC |
978 | // thus the following overload was disabled in that mode. However, doing |
979 | // that causes all sorts of issues, see, for example: | |
980 | // http://gcc.gnu.org/ml/libstdc++/2013-01/msg00058.html | |
981 | // and also PR57974. | |
54c1bf78 BK |
982 | template<typename _Tp> |
983 | inline complex<_Tp> | |
984 | pow(const complex<_Tp>& __z, int __n) | |
f3961bdf PC |
985 | { |
986 | return __n < 0 | |
91f4a9e3 | 987 | ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n) |
f3961bdf PC |
988 | : std::__complex_pow_unsigned(__z, __n); |
989 | } | |
54c1bf78 BK |
990 | |
991 | template<typename _Tp> | |
1db0418a | 992 | complex<_Tp> |
54c1bf78 BK |
993 | pow(const complex<_Tp>& __x, const _Tp& __y) |
994 | { | |
b0de8599 PC |
995 | #ifndef _GLIBCXX_USE_C99_COMPLEX |
996 | if (__x == _Tp()) | |
997 | return _Tp(); | |
998 | #endif | |
52ddaf41 | 999 | if (__x.imag() == _Tp() && __x.real() > _Tp()) |
a8784c4c | 1000 | return pow(__x.real(), __y); |
1db0418a | 1001 | |
c6feb697 | 1002 | complex<_Tp> __t = std::log(__x); |
742f66e7 | 1003 | return std::polar<_Tp>(exp(__y * __t.real()), __y * __t.imag()); |
54c1bf78 BK |
1004 | } |
1005 | ||
a4ddde0d GDR |
1006 | template<typename _Tp> |
1007 | inline complex<_Tp> | |
1008 | __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y) | |
1009 | { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); } | |
1010 | ||
ab9b9d2c | 1011 | #if _GLIBCXX_USE_C99_COMPLEX |
a4ddde0d GDR |
1012 | inline __complex__ float |
1013 | __complex_pow(__complex__ float __x, __complex__ float __y) | |
1014 | { return __builtin_cpowf(__x, __y); } | |
1015 | ||
1016 | inline __complex__ double | |
1017 | __complex_pow(__complex__ double __x, __complex__ double __y) | |
1018 | { return __builtin_cpow(__x, __y); } | |
1019 | ||
1020 | inline __complex__ long double | |
cff001b2 PC |
1021 | __complex_pow(const __complex__ long double& __x, |
1022 | const __complex__ long double& __y) | |
a4ddde0d | 1023 | { return __builtin_cpowl(__x, __y); } |
52e6723c | 1024 | |
cff001b2 PC |
1025 | template<typename _Tp> |
1026 | inline complex<_Tp> | |
1027 | pow(const complex<_Tp>& __x, const complex<_Tp>& __y) | |
1028 | { return __complex_pow(__x.__rep(), __y.__rep()); } | |
1029 | #else | |
54c1bf78 BK |
1030 | template<typename _Tp> |
1031 | inline complex<_Tp> | |
1032 | pow(const complex<_Tp>& __x, const complex<_Tp>& __y) | |
a4ddde0d | 1033 | { return __complex_pow(__x, __y); } |
cff001b2 | 1034 | #endif |
54c1bf78 BK |
1035 | |
1036 | template<typename _Tp> | |
1037 | inline complex<_Tp> | |
1038 | pow(const _Tp& __x, const complex<_Tp>& __y) | |
1039 | { | |
742f66e7 MG |
1040 | return __x > _Tp() ? std::polar<_Tp>(pow(__x, __y.real()), |
1041 | __y.imag() * log(__x)) | |
2acceeac | 1042 | : std::pow(complex<_Tp>(__x), __y); |
54c1bf78 BK |
1043 | } |
1044 | ||
7897a1c0 BK |
1045 | /// 26.2.3 complex specializations |
1046 | /// complex<float> specialization | |
a4ddde0d GDR |
1047 | template<> |
1048 | struct complex<float> | |
1049 | { | |
1050 | typedef float value_type; | |
1051 | typedef __complex__ float _ComplexT; | |
1052 | ||
94a86be0 | 1053 | _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } |
a4ddde0d | 1054 | |
94a86be0 | 1055 | _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f) |
734f5023 | 1056 | #if __cplusplus >= 201103L |
9f1163b1 PC |
1057 | : _M_value{ __r, __i } { } |
1058 | #else | |
1059 | { | |
1060 | __real__ _M_value = __r; | |
1061 | __imag__ _M_value = __i; | |
1062 | } | |
1063 | #endif | |
02a65d23 | 1064 | |
94a86be0 BK |
1065 | explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&); |
1066 | explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&); | |
2acceeac | 1067 | |
734f5023 | 1068 | #if __cplusplus >= 201103L |
23ed71c6 PC |
1069 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
1070 | // DR 387. std::complex over-encapsulated. | |
7a3a9e68 | 1071 | __attribute ((__abi_tag__ ("cxx11"))) |
94a86be0 | 1072 | constexpr float |
9191d7a8 | 1073 | real() const { return __real__ _M_value; } |
23ed71c6 | 1074 | |
7a3a9e68 | 1075 | __attribute ((__abi_tag__ ("cxx11"))) |
94a86be0 | 1076 | constexpr float |
9191d7a8 | 1077 | imag() const { return __imag__ _M_value; } |
23ed71c6 | 1078 | #else |
94a86be0 BK |
1079 | float& |
1080 | real() { return __real__ _M_value; } | |
2acceeac | 1081 | |
94a86be0 BK |
1082 | const float& |
1083 | real() const { return __real__ _M_value; } | |
2acceeac | 1084 | |
94a86be0 BK |
1085 | float& |
1086 | imag() { return __imag__ _M_value; } | |
2acceeac | 1087 | |
94a86be0 BK |
1088 | const float& |
1089 | imag() const { return __imag__ _M_value; } | |
23ed71c6 PC |
1090 | #endif |
1091 | ||
1092 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
1093 | // DR 387. std::complex over-encapsulated. | |
94a86be0 BK |
1094 | void |
1095 | real(float __val) { __real__ _M_value = __val; } | |
23ed71c6 | 1096 | |
94a86be0 BK |
1097 | void |
1098 | imag(float __val) { __imag__ _M_value = __val; } | |
2acceeac | 1099 | |
f941c3e2 | 1100 | complex& |
2acceeac PC |
1101 | operator=(float __f) |
1102 | { | |
f941c3e2 | 1103 | _M_value = __f; |
2acceeac PC |
1104 | return *this; |
1105 | } | |
1106 | ||
f941c3e2 | 1107 | complex& |
2acceeac PC |
1108 | operator+=(float __f) |
1109 | { | |
f941c3e2 | 1110 | _M_value += __f; |
2acceeac PC |
1111 | return *this; |
1112 | } | |
1113 | ||
f941c3e2 | 1114 | complex& |
2acceeac PC |
1115 | operator-=(float __f) |
1116 | { | |
f941c3e2 | 1117 | _M_value -= __f; |
2acceeac PC |
1118 | return *this; |
1119 | } | |
1120 | ||
f941c3e2 | 1121 | complex& |
2acceeac PC |
1122 | operator*=(float __f) |
1123 | { | |
1124 | _M_value *= __f; | |
1125 | return *this; | |
1126 | } | |
1127 | ||
f941c3e2 | 1128 | complex& |
2acceeac PC |
1129 | operator/=(float __f) |
1130 | { | |
1131 | _M_value /= __f; | |
1132 | return *this; | |
1133 | } | |
a4ddde0d | 1134 | |
28dac70a | 1135 | // Let the compiler synthesize the copy and assignment |
a4ddde0d | 1136 | // operator. It always does a pretty good job. |
2acceeac PC |
1137 | // complex& operator=(const complex&); |
1138 | ||
a4ddde0d | 1139 | template<typename _Tp> |
f941c3e2 | 1140 | complex& |
2acceeac PC |
1141 | operator=(const complex<_Tp>& __z) |
1142 | { | |
1143 | __real__ _M_value = __z.real(); | |
1144 | __imag__ _M_value = __z.imag(); | |
1145 | return *this; | |
1146 | } | |
1147 | ||
a4ddde0d | 1148 | template<typename _Tp> |
f941c3e2 | 1149 | complex& |
2acceeac PC |
1150 | operator+=(const complex<_Tp>& __z) |
1151 | { | |
1152 | __real__ _M_value += __z.real(); | |
1153 | __imag__ _M_value += __z.imag(); | |
1154 | return *this; | |
1155 | } | |
1156 | ||
a4ddde0d | 1157 | template<class _Tp> |
f941c3e2 | 1158 | complex& |
2acceeac PC |
1159 | operator-=(const complex<_Tp>& __z) |
1160 | { | |
1161 | __real__ _M_value -= __z.real(); | |
1162 | __imag__ _M_value -= __z.imag(); | |
1163 | return *this; | |
1164 | } | |
1165 | ||
a4ddde0d | 1166 | template<class _Tp> |
f941c3e2 | 1167 | complex& |
2acceeac PC |
1168 | operator*=(const complex<_Tp>& __z) |
1169 | { | |
1170 | _ComplexT __t; | |
1171 | __real__ __t = __z.real(); | |
1172 | __imag__ __t = __z.imag(); | |
1173 | _M_value *= __t; | |
1174 | return *this; | |
1175 | } | |
1176 | ||
a4ddde0d | 1177 | template<class _Tp> |
f941c3e2 | 1178 | complex& |
2acceeac PC |
1179 | operator/=(const complex<_Tp>& __z) |
1180 | { | |
1181 | _ComplexT __t; | |
1182 | __real__ __t = __z.real(); | |
1183 | __imag__ __t = __z.imag(); | |
1184 | _M_value /= __t; | |
1185 | return *this; | |
1186 | } | |
a4ddde0d | 1187 | |
3fa591d4 | 1188 | _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; } |
a4ddde0d GDR |
1189 | |
1190 | private: | |
1191 | _ComplexT _M_value; | |
1192 | }; | |
54c1bf78 | 1193 | |
7897a1c0 BK |
1194 | /// 26.2.3 complex specializations |
1195 | /// complex<double> specialization | |
a4ddde0d GDR |
1196 | template<> |
1197 | struct complex<double> | |
1198 | { | |
1199 | typedef double value_type; | |
1200 | typedef __complex__ double _ComplexT; | |
1201 | ||
94a86be0 | 1202 | _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } |
54c1bf78 | 1203 | |
94a86be0 | 1204 | _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0) |
734f5023 | 1205 | #if __cplusplus >= 201103L |
9f1163b1 PC |
1206 | : _M_value{ __r, __i } { } |
1207 | #else | |
1208 | { | |
1209 | __real__ _M_value = __r; | |
1210 | __imag__ _M_value = __i; | |
1211 | } | |
1212 | #endif | |
2acceeac | 1213 | |
94a86be0 | 1214 | _GLIBCXX_CONSTEXPR complex(const complex<float>& __z) |
2acceeac PC |
1215 | : _M_value(__z.__rep()) { } |
1216 | ||
94a86be0 | 1217 | explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&); |
2acceeac | 1218 | |
734f5023 | 1219 | #if __cplusplus >= 201103L |
23ed71c6 PC |
1220 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
1221 | // DR 387. std::complex over-encapsulated. | |
7a3a9e68 | 1222 | __attribute ((__abi_tag__ ("cxx11"))) |
94a86be0 | 1223 | constexpr double |
9191d7a8 | 1224 | real() const { return __real__ _M_value; } |
23ed71c6 | 1225 | |
7a3a9e68 | 1226 | __attribute ((__abi_tag__ ("cxx11"))) |
94a86be0 | 1227 | constexpr double |
9191d7a8 | 1228 | imag() const { return __imag__ _M_value; } |
23ed71c6 | 1229 | #else |
94a86be0 BK |
1230 | double& |
1231 | real() { return __real__ _M_value; } | |
2acceeac | 1232 | |
94a86be0 BK |
1233 | const double& |
1234 | real() const { return __real__ _M_value; } | |
2acceeac | 1235 | |
94a86be0 BK |
1236 | double& |
1237 | imag() { return __imag__ _M_value; } | |
2acceeac | 1238 | |
94a86be0 BK |
1239 | const double& |
1240 | imag() const { return __imag__ _M_value; } | |
23ed71c6 PC |
1241 | #endif |
1242 | ||
1243 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
1244 | // DR 387. std::complex over-encapsulated. | |
94a86be0 BK |
1245 | void |
1246 | real(double __val) { __real__ _M_value = __val; } | |
23ed71c6 | 1247 | |
94a86be0 BK |
1248 | void |
1249 | imag(double __val) { __imag__ _M_value = __val; } | |
2acceeac | 1250 | |
f941c3e2 | 1251 | complex& |
2acceeac PC |
1252 | operator=(double __d) |
1253 | { | |
f941c3e2 | 1254 | _M_value = __d; |
2acceeac PC |
1255 | return *this; |
1256 | } | |
1257 | ||
f941c3e2 | 1258 | complex& |
2acceeac PC |
1259 | operator+=(double __d) |
1260 | { | |
f941c3e2 | 1261 | _M_value += __d; |
2acceeac PC |
1262 | return *this; |
1263 | } | |
1264 | ||
f941c3e2 | 1265 | complex& |
2acceeac PC |
1266 | operator-=(double __d) |
1267 | { | |
f941c3e2 | 1268 | _M_value -= __d; |
2acceeac PC |
1269 | return *this; |
1270 | } | |
1271 | ||
f941c3e2 | 1272 | complex& |
2acceeac PC |
1273 | operator*=(double __d) |
1274 | { | |
1275 | _M_value *= __d; | |
1276 | return *this; | |
1277 | } | |
1278 | ||
f941c3e2 | 1279 | complex& |
2acceeac PC |
1280 | operator/=(double __d) |
1281 | { | |
1282 | _M_value /= __d; | |
1283 | return *this; | |
1284 | } | |
a4ddde0d | 1285 | |
28dac70a | 1286 | // The compiler will synthesize this, efficiently. |
2acceeac PC |
1287 | // complex& operator=(const complex&); |
1288 | ||
a4ddde0d | 1289 | template<typename _Tp> |
f941c3e2 | 1290 | complex& |
2acceeac PC |
1291 | operator=(const complex<_Tp>& __z) |
1292 | { | |
1293 | __real__ _M_value = __z.real(); | |
1294 | __imag__ _M_value = __z.imag(); | |
1295 | return *this; | |
1296 | } | |
1297 | ||
a4ddde0d | 1298 | template<typename _Tp> |
f941c3e2 | 1299 | complex& |
2acceeac PC |
1300 | operator+=(const complex<_Tp>& __z) |
1301 | { | |
1302 | __real__ _M_value += __z.real(); | |
1303 | __imag__ _M_value += __z.imag(); | |
1304 | return *this; | |
1305 | } | |
1306 | ||
a4ddde0d | 1307 | template<typename _Tp> |
f941c3e2 | 1308 | complex& |
2acceeac PC |
1309 | operator-=(const complex<_Tp>& __z) |
1310 | { | |
1311 | __real__ _M_value -= __z.real(); | |
1312 | __imag__ _M_value -= __z.imag(); | |
1313 | return *this; | |
1314 | } | |
1315 | ||
a4ddde0d | 1316 | template<typename _Tp> |
f941c3e2 | 1317 | complex& |
2acceeac PC |
1318 | operator*=(const complex<_Tp>& __z) |
1319 | { | |
1320 | _ComplexT __t; | |
1321 | __real__ __t = __z.real(); | |
1322 | __imag__ __t = __z.imag(); | |
1323 | _M_value *= __t; | |
1324 | return *this; | |
1325 | } | |
1326 | ||
a4ddde0d | 1327 | template<typename _Tp> |
f941c3e2 | 1328 | complex& |
2acceeac PC |
1329 | operator/=(const complex<_Tp>& __z) |
1330 | { | |
1331 | _ComplexT __t; | |
1332 | __real__ __t = __z.real(); | |
1333 | __imag__ __t = __z.imag(); | |
1334 | _M_value /= __t; | |
1335 | return *this; | |
1336 | } | |
3b3bfc0e | 1337 | |
3fa591d4 | 1338 | _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; } |
a4ddde0d GDR |
1339 | |
1340 | private: | |
1341 | _ComplexT _M_value; | |
1342 | }; | |
54c1bf78 | 1343 | |
7897a1c0 BK |
1344 | /// 26.2.3 complex specializations |
1345 | /// complex<long double> specialization | |
a4ddde0d GDR |
1346 | template<> |
1347 | struct complex<long double> | |
1348 | { | |
1349 | typedef long double value_type; | |
1350 | typedef __complex__ long double _ComplexT; | |
54c1bf78 | 1351 | |
94a86be0 | 1352 | _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } |
a4ddde0d | 1353 | |
94a86be0 BK |
1354 | _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L, |
1355 | long double __i = 0.0L) | |
734f5023 | 1356 | #if __cplusplus >= 201103L |
9f1163b1 PC |
1357 | : _M_value{ __r, __i } { } |
1358 | #else | |
1359 | { | |
1360 | __real__ _M_value = __r; | |
1361 | __imag__ _M_value = __i; | |
1362 | } | |
1363 | #endif | |
2acceeac | 1364 | |
94a86be0 | 1365 | _GLIBCXX_CONSTEXPR complex(const complex<float>& __z) |
2acceeac PC |
1366 | : _M_value(__z.__rep()) { } |
1367 | ||
94a86be0 | 1368 | _GLIBCXX_CONSTEXPR complex(const complex<double>& __z) |
2acceeac PC |
1369 | : _M_value(__z.__rep()) { } |
1370 | ||
734f5023 | 1371 | #if __cplusplus >= 201103L |
23ed71c6 PC |
1372 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
1373 | // DR 387. std::complex over-encapsulated. | |
7a3a9e68 | 1374 | __attribute ((__abi_tag__ ("cxx11"))) |
94a86be0 | 1375 | constexpr long double |
9191d7a8 | 1376 | real() const { return __real__ _M_value; } |
23ed71c6 | 1377 | |
7a3a9e68 | 1378 | __attribute ((__abi_tag__ ("cxx11"))) |
94a86be0 | 1379 | constexpr long double |
9191d7a8 | 1380 | imag() const { return __imag__ _M_value; } |
23ed71c6 | 1381 | #else |
94a86be0 BK |
1382 | long double& |
1383 | real() { return __real__ _M_value; } | |
2acceeac | 1384 | |
94a86be0 BK |
1385 | const long double& |
1386 | real() const { return __real__ _M_value; } | |
2acceeac | 1387 | |
94a86be0 BK |
1388 | long double& |
1389 | imag() { return __imag__ _M_value; } | |
2acceeac | 1390 | |
94a86be0 BK |
1391 | const long double& |
1392 | imag() const { return __imag__ _M_value; } | |
23ed71c6 PC |
1393 | #endif |
1394 | ||
1395 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
1396 | // DR 387. std::complex over-encapsulated. | |
94a86be0 BK |
1397 | void |
1398 | real(long double __val) { __real__ _M_value = __val; } | |
23ed71c6 | 1399 | |
94a86be0 BK |
1400 | void |
1401 | imag(long double __val) { __imag__ _M_value = __val; } | |
2acceeac | 1402 | |
f941c3e2 | 1403 | complex& |
2acceeac PC |
1404 | operator=(long double __r) |
1405 | { | |
f941c3e2 | 1406 | _M_value = __r; |
2acceeac PC |
1407 | return *this; |
1408 | } | |
1409 | ||
f941c3e2 | 1410 | complex& |
2acceeac PC |
1411 | operator+=(long double __r) |
1412 | { | |
f941c3e2 | 1413 | _M_value += __r; |
2acceeac PC |
1414 | return *this; |
1415 | } | |
1416 | ||
f941c3e2 | 1417 | complex& |
2acceeac PC |
1418 | operator-=(long double __r) |
1419 | { | |
f941c3e2 | 1420 | _M_value -= __r; |
2acceeac PC |
1421 | return *this; |
1422 | } | |
1423 | ||
f941c3e2 | 1424 | complex& |
2acceeac PC |
1425 | operator*=(long double __r) |
1426 | { | |
1427 | _M_value *= __r; | |
1428 | return *this; | |
1429 | } | |
1430 | ||
f941c3e2 | 1431 | complex& |
2acceeac PC |
1432 | operator/=(long double __r) |
1433 | { | |
1434 | _M_value /= __r; | |
1435 | return *this; | |
1436 | } | |
a4ddde0d GDR |
1437 | |
1438 | // The compiler knows how to do this efficiently | |
2acceeac PC |
1439 | // complex& operator=(const complex&); |
1440 | ||
a4ddde0d | 1441 | template<typename _Tp> |
f941c3e2 | 1442 | complex& |
2acceeac PC |
1443 | operator=(const complex<_Tp>& __z) |
1444 | { | |
1445 | __real__ _M_value = __z.real(); | |
1446 | __imag__ _M_value = __z.imag(); | |
1447 | return *this; | |
1448 | } | |
1449 | ||
a4ddde0d | 1450 | template<typename _Tp> |
f941c3e2 | 1451 | complex& |
2acceeac PC |
1452 | operator+=(const complex<_Tp>& __z) |
1453 | { | |
1454 | __real__ _M_value += __z.real(); | |
1455 | __imag__ _M_value += __z.imag(); | |
1456 | return *this; | |
1457 | } | |
1458 | ||
a4ddde0d | 1459 | template<typename _Tp> |
f941c3e2 | 1460 | complex& |
2acceeac PC |
1461 | operator-=(const complex<_Tp>& __z) |
1462 | { | |
1463 | __real__ _M_value -= __z.real(); | |
1464 | __imag__ _M_value -= __z.imag(); | |
1465 | return *this; | |
1466 | } | |
1467 | ||
a4ddde0d | 1468 | template<typename _Tp> |
f941c3e2 | 1469 | complex& |
2acceeac PC |
1470 | operator*=(const complex<_Tp>& __z) |
1471 | { | |
1472 | _ComplexT __t; | |
1473 | __real__ __t = __z.real(); | |
1474 | __imag__ __t = __z.imag(); | |
1475 | _M_value *= __t; | |
1476 | return *this; | |
1477 | } | |
1478 | ||
a4ddde0d | 1479 | template<typename _Tp> |
f941c3e2 | 1480 | complex& |
2acceeac PC |
1481 | operator/=(const complex<_Tp>& __z) |
1482 | { | |
1483 | _ComplexT __t; | |
1484 | __real__ __t = __z.real(); | |
1485 | __imag__ __t = __z.imag(); | |
1486 | _M_value /= __t; | |
1487 | return *this; | |
1488 | } | |
a4ddde0d | 1489 | |
3fa591d4 | 1490 | _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; } |
a4ddde0d GDR |
1491 | |
1492 | private: | |
1493 | _ComplexT _M_value; | |
1494 | }; | |
54c1bf78 | 1495 | |
54c1bf78 BK |
1496 | // These bits have to be at the end of this file, so that the |
1497 | // specializations have all been defined. | |
94a86be0 | 1498 | inline _GLIBCXX_CONSTEXPR |
54c1bf78 | 1499 | complex<float>::complex(const complex<double>& __z) |
a4ddde0d | 1500 | : _M_value(__z.__rep()) { } |
54c1bf78 | 1501 | |
94a86be0 | 1502 | inline _GLIBCXX_CONSTEXPR |
54c1bf78 | 1503 | complex<float>::complex(const complex<long double>& __z) |
a4ddde0d | 1504 | : _M_value(__z.__rep()) { } |
54c1bf78 | 1505 | |
94a86be0 | 1506 | inline _GLIBCXX_CONSTEXPR |
54c1bf78 | 1507 | complex<double>::complex(const complex<long double>& __z) |
d0cbf089 | 1508 | : _M_value(__z.__rep()) { } |
54c1bf78 | 1509 | |
74b332b8 PC |
1510 | // Inhibit implicit instantiations for required instantiations, |
1511 | // which are defined via explicit instantiations elsewhere. | |
1512 | // NB: This syntax is a GNU extension. | |
1513 | #if _GLIBCXX_EXTERN_TEMPLATE | |
1514 | extern template istream& operator>>(istream&, complex<float>&); | |
1515 | extern template ostream& operator<<(ostream&, const complex<float>&); | |
1516 | extern template istream& operator>>(istream&, complex<double>&); | |
1517 | extern template ostream& operator<<(ostream&, const complex<double>&); | |
1518 | extern template istream& operator>>(istream&, complex<long double>&); | |
1519 | extern template ostream& operator<<(ostream&, const complex<long double>&); | |
1520 | ||
1521 | #ifdef _GLIBCXX_USE_WCHAR_T | |
1522 | extern template wistream& operator>>(wistream&, complex<float>&); | |
1523 | extern template wostream& operator<<(wostream&, const complex<float>&); | |
1524 | extern template wistream& operator>>(wistream&, complex<double>&); | |
1525 | extern template wostream& operator<<(wostream&, const complex<double>&); | |
1526 | extern template wistream& operator>>(wistream&, complex<long double>&); | |
1527 | extern template wostream& operator<<(wostream&, const complex<long double>&); | |
1528 | #endif | |
1529 | #endif | |
1530 | ||
5b9daa7e BK |
1531 | // @} group complex_numbers |
1532 | ||
12ffa228 BK |
1533 | _GLIBCXX_END_NAMESPACE_VERSION |
1534 | } // namespace | |
54c1bf78 | 1535 | |
12ffa228 BK |
1536 | namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) |
1537 | { | |
1538 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
e133ace8 PC |
1539 | |
1540 | // See ext/type_traits.h for the primary template. | |
1541 | template<typename _Tp, typename _Up> | |
1542 | struct __promote_2<std::complex<_Tp>, _Up> | |
1543 | { | |
1544 | public: | |
1545 | typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type; | |
1546 | }; | |
1547 | ||
1548 | template<typename _Tp, typename _Up> | |
1549 | struct __promote_2<_Tp, std::complex<_Up> > | |
1550 | { | |
1551 | public: | |
1552 | typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type; | |
1553 | }; | |
1554 | ||
1555 | template<typename _Tp, typename _Up> | |
1556 | struct __promote_2<std::complex<_Tp>, std::complex<_Up> > | |
1557 | { | |
1558 | public: | |
1559 | typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type; | |
1560 | }; | |
1561 | ||
12ffa228 BK |
1562 | _GLIBCXX_END_NAMESPACE_VERSION |
1563 | } // namespace | |
e133ace8 | 1564 | |
734f5023 | 1565 | #if __cplusplus >= 201103L |
3cd54fc9 | 1566 | |
12ffa228 BK |
1567 | namespace std _GLIBCXX_VISIBILITY(default) |
1568 | { | |
1569 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
3cd54fc9 | 1570 | |
53dc5044 PC |
1571 | // Forward declarations. |
1572 | template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&); | |
1573 | template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&); | |
1574 | template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&); | |
1575 | ||
1576 | template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&); | |
1577 | template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&); | |
1578 | template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&); | |
1579 | // DR 595. | |
1580 | template<typename _Tp> _Tp fabs(const std::complex<_Tp>&); | |
1581 | ||
1582 | template<typename _Tp> | |
1583 | inline std::complex<_Tp> | |
1584 | __complex_acos(const std::complex<_Tp>& __z) | |
1585 | { | |
1586 | const std::complex<_Tp> __t = std::asin(__z); | |
1587 | const _Tp __pi_2 = 1.5707963267948966192313216916397514L; | |
1588 | return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag()); | |
1589 | } | |
1590 | ||
1591 | #if _GLIBCXX_USE_C99_COMPLEX_TR1 | |
1592 | inline __complex__ float | |
1593 | __complex_acos(__complex__ float __z) | |
1594 | { return __builtin_cacosf(__z); } | |
1595 | ||
1596 | inline __complex__ double | |
1597 | __complex_acos(__complex__ double __z) | |
1598 | { return __builtin_cacos(__z); } | |
1599 | ||
1600 | inline __complex__ long double | |
1601 | __complex_acos(const __complex__ long double& __z) | |
1602 | { return __builtin_cacosl(__z); } | |
1603 | ||
1604 | template<typename _Tp> | |
1605 | inline std::complex<_Tp> | |
1606 | acos(const std::complex<_Tp>& __z) | |
1607 | { return __complex_acos(__z.__rep()); } | |
1608 | #else | |
1609 | /// acos(__z) [8.1.2]. | |
1610 | // Effects: Behaves the same as C99 function cacos, defined | |
1611 | // in subclause 7.3.5.1. | |
1612 | template<typename _Tp> | |
1613 | inline std::complex<_Tp> | |
1614 | acos(const std::complex<_Tp>& __z) | |
1615 | { return __complex_acos(__z); } | |
1616 | #endif | |
1617 | ||
1618 | template<typename _Tp> | |
1619 | inline std::complex<_Tp> | |
1620 | __complex_asin(const std::complex<_Tp>& __z) | |
1621 | { | |
1622 | std::complex<_Tp> __t(-__z.imag(), __z.real()); | |
1623 | __t = std::asinh(__t); | |
1624 | return std::complex<_Tp>(__t.imag(), -__t.real()); | |
1625 | } | |
1626 | ||
1627 | #if _GLIBCXX_USE_C99_COMPLEX_TR1 | |
1628 | inline __complex__ float | |
1629 | __complex_asin(__complex__ float __z) | |
1630 | { return __builtin_casinf(__z); } | |
1631 | ||
1632 | inline __complex__ double | |
1633 | __complex_asin(__complex__ double __z) | |
1634 | { return __builtin_casin(__z); } | |
1635 | ||
1636 | inline __complex__ long double | |
1637 | __complex_asin(const __complex__ long double& __z) | |
1638 | { return __builtin_casinl(__z); } | |
1639 | ||
1640 | template<typename _Tp> | |
1641 | inline std::complex<_Tp> | |
1642 | asin(const std::complex<_Tp>& __z) | |
1643 | { return __complex_asin(__z.__rep()); } | |
1644 | #else | |
1645 | /// asin(__z) [8.1.3]. | |
1646 | // Effects: Behaves the same as C99 function casin, defined | |
1647 | // in subclause 7.3.5.2. | |
1648 | template<typename _Tp> | |
1649 | inline std::complex<_Tp> | |
1650 | asin(const std::complex<_Tp>& __z) | |
1651 | { return __complex_asin(__z); } | |
1652 | #endif | |
1653 | ||
1654 | template<typename _Tp> | |
1655 | std::complex<_Tp> | |
1656 | __complex_atan(const std::complex<_Tp>& __z) | |
1657 | { | |
1658 | const _Tp __r2 = __z.real() * __z.real(); | |
1659 | const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag(); | |
1660 | ||
1661 | _Tp __num = __z.imag() + _Tp(1.0); | |
1662 | _Tp __den = __z.imag() - _Tp(1.0); | |
1663 | ||
1664 | __num = __r2 + __num * __num; | |
1665 | __den = __r2 + __den * __den; | |
1666 | ||
1667 | return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x), | |
1668 | _Tp(0.25) * log(__num / __den)); | |
1669 | } | |
1670 | ||
1671 | #if _GLIBCXX_USE_C99_COMPLEX_TR1 | |
1672 | inline __complex__ float | |
1673 | __complex_atan(__complex__ float __z) | |
1674 | { return __builtin_catanf(__z); } | |
1675 | ||
1676 | inline __complex__ double | |
1677 | __complex_atan(__complex__ double __z) | |
1678 | { return __builtin_catan(__z); } | |
1679 | ||
1680 | inline __complex__ long double | |
1681 | __complex_atan(const __complex__ long double& __z) | |
1682 | { return __builtin_catanl(__z); } | |
1683 | ||
1684 | template<typename _Tp> | |
1685 | inline std::complex<_Tp> | |
1686 | atan(const std::complex<_Tp>& __z) | |
1687 | { return __complex_atan(__z.__rep()); } | |
1688 | #else | |
1689 | /// atan(__z) [8.1.4]. | |
1690 | // Effects: Behaves the same as C99 function catan, defined | |
1691 | // in subclause 7.3.5.3. | |
1692 | template<typename _Tp> | |
1693 | inline std::complex<_Tp> | |
1694 | atan(const std::complex<_Tp>& __z) | |
1695 | { return __complex_atan(__z); } | |
1696 | #endif | |
1697 | ||
1698 | template<typename _Tp> | |
1699 | std::complex<_Tp> | |
1700 | __complex_acosh(const std::complex<_Tp>& __z) | |
1701 | { | |
af7c1858 RK |
1702 | // Kahan's formula. |
1703 | return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0))) | |
1704 | + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0)))); | |
53dc5044 PC |
1705 | } |
1706 | ||
1707 | #if _GLIBCXX_USE_C99_COMPLEX_TR1 | |
1708 | inline __complex__ float | |
1709 | __complex_acosh(__complex__ float __z) | |
1710 | { return __builtin_cacoshf(__z); } | |
1711 | ||
1712 | inline __complex__ double | |
1713 | __complex_acosh(__complex__ double __z) | |
1714 | { return __builtin_cacosh(__z); } | |
1715 | ||
1716 | inline __complex__ long double | |
1717 | __complex_acosh(const __complex__ long double& __z) | |
1718 | { return __builtin_cacoshl(__z); } | |
1719 | ||
1720 | template<typename _Tp> | |
1721 | inline std::complex<_Tp> | |
1722 | acosh(const std::complex<_Tp>& __z) | |
1723 | { return __complex_acosh(__z.__rep()); } | |
1724 | #else | |
1725 | /// acosh(__z) [8.1.5]. | |
1726 | // Effects: Behaves the same as C99 function cacosh, defined | |
1727 | // in subclause 7.3.6.1. | |
1728 | template<typename _Tp> | |
1729 | inline std::complex<_Tp> | |
1730 | acosh(const std::complex<_Tp>& __z) | |
1731 | { return __complex_acosh(__z); } | |
1732 | #endif | |
1733 | ||
1734 | template<typename _Tp> | |
1735 | std::complex<_Tp> | |
1736 | __complex_asinh(const std::complex<_Tp>& __z) | |
1737 | { | |
1738 | std::complex<_Tp> __t((__z.real() - __z.imag()) | |
1739 | * (__z.real() + __z.imag()) + _Tp(1.0), | |
1740 | _Tp(2.0) * __z.real() * __z.imag()); | |
1741 | __t = std::sqrt(__t); | |
1742 | ||
1743 | return std::log(__t + __z); | |
1744 | } | |
1745 | ||
1746 | #if _GLIBCXX_USE_C99_COMPLEX_TR1 | |
1747 | inline __complex__ float | |
1748 | __complex_asinh(__complex__ float __z) | |
1749 | { return __builtin_casinhf(__z); } | |
1750 | ||
1751 | inline __complex__ double | |
1752 | __complex_asinh(__complex__ double __z) | |
1753 | { return __builtin_casinh(__z); } | |
1754 | ||
1755 | inline __complex__ long double | |
1756 | __complex_asinh(const __complex__ long double& __z) | |
1757 | { return __builtin_casinhl(__z); } | |
1758 | ||
1759 | template<typename _Tp> | |
1760 | inline std::complex<_Tp> | |
1761 | asinh(const std::complex<_Tp>& __z) | |
1762 | { return __complex_asinh(__z.__rep()); } | |
1763 | #else | |
1764 | /// asinh(__z) [8.1.6]. | |
1765 | // Effects: Behaves the same as C99 function casin, defined | |
1766 | // in subclause 7.3.6.2. | |
1767 | template<typename _Tp> | |
1768 | inline std::complex<_Tp> | |
1769 | asinh(const std::complex<_Tp>& __z) | |
1770 | { return __complex_asinh(__z); } | |
1771 | #endif | |
1772 | ||
1773 | template<typename _Tp> | |
1774 | std::complex<_Tp> | |
1775 | __complex_atanh(const std::complex<_Tp>& __z) | |
1776 | { | |
1777 | const _Tp __i2 = __z.imag() * __z.imag(); | |
1778 | const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real(); | |
1779 | ||
1780 | _Tp __num = _Tp(1.0) + __z.real(); | |
1781 | _Tp __den = _Tp(1.0) - __z.real(); | |
1782 | ||
1783 | __num = __i2 + __num * __num; | |
1784 | __den = __i2 + __den * __den; | |
1785 | ||
1786 | return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)), | |
1787 | _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x)); | |
1788 | } | |
1789 | ||
1790 | #if _GLIBCXX_USE_C99_COMPLEX_TR1 | |
1791 | inline __complex__ float | |
1792 | __complex_atanh(__complex__ float __z) | |
1793 | { return __builtin_catanhf(__z); } | |
1794 | ||
1795 | inline __complex__ double | |
1796 | __complex_atanh(__complex__ double __z) | |
1797 | { return __builtin_catanh(__z); } | |
1798 | ||
1799 | inline __complex__ long double | |
1800 | __complex_atanh(const __complex__ long double& __z) | |
1801 | { return __builtin_catanhl(__z); } | |
1802 | ||
1803 | template<typename _Tp> | |
1804 | inline std::complex<_Tp> | |
1805 | atanh(const std::complex<_Tp>& __z) | |
1806 | { return __complex_atanh(__z.__rep()); } | |
1807 | #else | |
1808 | /// atanh(__z) [8.1.7]. | |
1809 | // Effects: Behaves the same as C99 function catanh, defined | |
1810 | // in subclause 7.3.6.3. | |
1811 | template<typename _Tp> | |
1812 | inline std::complex<_Tp> | |
1813 | atanh(const std::complex<_Tp>& __z) | |
1814 | { return __complex_atanh(__z); } | |
1815 | #endif | |
1816 | ||
1817 | template<typename _Tp> | |
1818 | inline _Tp | |
1819 | /// fabs(__z) [8.1.8]. | |
1820 | // Effects: Behaves the same as C99 function cabs, defined | |
1821 | // in subclause 7.3.8.1. | |
1822 | fabs(const std::complex<_Tp>& __z) | |
1823 | { return std::abs(__z); } | |
1824 | ||
1825 | /// Additional overloads [8.1.9]. | |
1826 | template<typename _Tp> | |
1827 | inline typename __gnu_cxx::__promote<_Tp>::__type | |
1828 | arg(_Tp __x) | |
1829 | { | |
1830 | typedef typename __gnu_cxx::__promote<_Tp>::__type __type; | |
1831 | #if (_GLIBCXX_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC) | |
1832 | return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L) | |
1833 | : __type(); | |
1834 | #else | |
1835 | return std::arg(std::complex<__type>(__x)); | |
1836 | #endif | |
1837 | } | |
1838 | ||
1839 | template<typename _Tp> | |
1840 | inline typename __gnu_cxx::__promote<_Tp>::__type | |
1841 | imag(_Tp) | |
1842 | { return _Tp(); } | |
1843 | ||
1844 | template<typename _Tp> | |
1845 | inline typename __gnu_cxx::__promote<_Tp>::__type | |
1846 | norm(_Tp __x) | |
1847 | { | |
1848 | typedef typename __gnu_cxx::__promote<_Tp>::__type __type; | |
1849 | return __type(__x) * __type(__x); | |
1850 | } | |
1851 | ||
1852 | template<typename _Tp> | |
1853 | inline typename __gnu_cxx::__promote<_Tp>::__type | |
1854 | real(_Tp __x) | |
1855 | { return __x; } | |
1856 | ||
1857 | template<typename _Tp, typename _Up> | |
1858 | inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> | |
1859 | pow(const std::complex<_Tp>& __x, const _Up& __y) | |
1860 | { | |
1861 | typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; | |
1862 | return std::pow(std::complex<__type>(__x), __type(__y)); | |
1863 | } | |
1864 | ||
1865 | template<typename _Tp, typename _Up> | |
1866 | inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> | |
1867 | pow(const _Tp& __x, const std::complex<_Up>& __y) | |
1868 | { | |
1869 | typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; | |
1870 | return std::pow(__type(__x), std::complex<__type>(__y)); | |
1871 | } | |
1872 | ||
1873 | template<typename _Tp, typename _Up> | |
1874 | inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> | |
1875 | pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y) | |
1876 | { | |
1877 | typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; | |
1878 | return std::pow(std::complex<__type>(__x), | |
1879 | std::complex<__type>(__y)); | |
1880 | } | |
1881 | ||
3cd54fc9 PC |
1882 | // Forward declarations. |
1883 | // DR 781. | |
1884 | template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&); | |
1885 | ||
1886 | template<typename _Tp> | |
1887 | std::complex<_Tp> | |
1888 | __complex_proj(const std::complex<_Tp>& __z) | |
1889 | { | |
1890 | const _Tp __den = (__z.real() * __z.real() | |
1891 | + __z.imag() * __z.imag() + _Tp(1.0)); | |
1892 | ||
1893 | return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den, | |
1894 | (_Tp(2.0) * __z.imag()) / __den); | |
1895 | } | |
1896 | ||
1897 | #if _GLIBCXX_USE_C99_COMPLEX | |
1898 | inline __complex__ float | |
1899 | __complex_proj(__complex__ float __z) | |
1900 | { return __builtin_cprojf(__z); } | |
1901 | ||
1902 | inline __complex__ double | |
1903 | __complex_proj(__complex__ double __z) | |
1904 | { return __builtin_cproj(__z); } | |
1905 | ||
1906 | inline __complex__ long double | |
1907 | __complex_proj(const __complex__ long double& __z) | |
1908 | { return __builtin_cprojl(__z); } | |
1909 | ||
1910 | template<typename _Tp> | |
1911 | inline std::complex<_Tp> | |
1912 | proj(const std::complex<_Tp>& __z) | |
1913 | { return __complex_proj(__z.__rep()); } | |
1914 | #else | |
1915 | template<typename _Tp> | |
1916 | inline std::complex<_Tp> | |
1917 | proj(const std::complex<_Tp>& __z) | |
1918 | { return __complex_proj(__z); } | |
1919 | #endif | |
1920 | ||
681f05d4 | 1921 | // DR 1137. |
3cd54fc9 | 1922 | template<typename _Tp> |
681f05d4 | 1923 | inline typename __gnu_cxx::__promote<_Tp>::__type |
3cd54fc9 | 1924 | proj(_Tp __x) |
681f05d4 PC |
1925 | { return __x; } |
1926 | ||
1927 | template<typename _Tp> | |
1928 | inline typename __gnu_cxx::__promote<_Tp>::__type | |
1929 | conj(_Tp __x) | |
1930 | { return __x; } | |
3cd54fc9 | 1931 | |
ae5543e6 ESR |
1932 | #if __cplusplus > 201103L |
1933 | ||
1934 | inline namespace literals { | |
1935 | inline namespace complex_literals { | |
1936 | ||
a15f7cb8 ESR |
1937 | #define __cpp_lib_complex_udls 201309 |
1938 | ||
ae5543e6 ESR |
1939 | constexpr std::complex<float> |
1940 | operator""if(long double __num) | |
1941 | { return std::complex<float>{0.0F, static_cast<float>(__num)}; } | |
1942 | ||
1943 | constexpr std::complex<float> | |
1944 | operator""if(unsigned long long __num) | |
1945 | { return std::complex<float>{0.0F, static_cast<float>(__num)}; } | |
1946 | ||
1947 | constexpr std::complex<double> | |
1948 | operator""i(long double __num) | |
1949 | { return std::complex<double>{0.0, static_cast<double>(__num)}; } | |
1950 | ||
1951 | constexpr std::complex<double> | |
1952 | operator""i(unsigned long long __num) | |
1953 | { return std::complex<double>{0.0, static_cast<double>(__num)}; } | |
1954 | ||
1955 | constexpr std::complex<long double> | |
1956 | operator""il(long double __num) | |
1957 | { return std::complex<long double>{0.0L, __num}; } | |
1958 | ||
1959 | constexpr std::complex<long double> | |
1960 | operator""il(unsigned long long __num) | |
1961 | { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; } | |
1962 | ||
1963 | } // inline namespace complex_literals | |
1964 | } // inline namespace literals | |
1965 | ||
1966 | #endif // C++14 | |
1967 | ||
12ffa228 BK |
1968 | _GLIBCXX_END_NAMESPACE_VERSION |
1969 | } // namespace | |
3cd54fc9 | 1970 | |
734f5023 | 1971 | #endif // C++11 |
af13a7a6 | 1972 | |
53dc5044 | 1973 | #endif /* _GLIBCXX_COMPLEX */ |