1// -*- C++ -*-
2//===--------------------------- complex ----------------------------------===//
3//
4//                     The LLVM Compiler Infrastructure
5//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_COMPLEX
12#define _LIBCPP_COMPLEX
13
14/*
15    complex synopsis
16
17namespace std
18{
19
20template<class T>
21class complex
22{
23public:
24    typedef T value_type;
25
26    complex(const T& re = T(), const T& im = T());
27    complex(const complex&);
28    template<class X> complex(const complex<X>&);
29
30    T real() const;
31    T imag() const;
32
33    void real(T);
34    void imag(T);
35
36    complex<T>& operator= (const T&);
37    complex<T>& operator+=(const T&);
38    complex<T>& operator-=(const T&);
39    complex<T>& operator*=(const T&);
40    complex<T>& operator/=(const T&);
41
42    complex& operator=(const complex&);
43    template<class X> complex<T>& operator= (const complex<X>&);
44    template<class X> complex<T>& operator+=(const complex<X>&);
45    template<class X> complex<T>& operator-=(const complex<X>&);
46    template<class X> complex<T>& operator*=(const complex<X>&);
47    template<class X> complex<T>& operator/=(const complex<X>&);
48};
49
50template<>
51class complex<float>
52{
53public:
54    typedef float value_type;
55
56    constexpr complex(float re = 0.0f, float im = 0.0f);
57    explicit constexpr complex(const complex<double>&);
58    explicit constexpr complex(const complex<long double>&);
59
60    constexpr float real() const;
61    void real(float);
62    constexpr float imag() const;
63    void imag(float);
64
65    complex<float>& operator= (float);
66    complex<float>& operator+=(float);
67    complex<float>& operator-=(float);
68    complex<float>& operator*=(float);
69    complex<float>& operator/=(float);
70
71    complex<float>& operator=(const complex<float>&);
72    template<class X> complex<float>& operator= (const complex<X>&);
73    template<class X> complex<float>& operator+=(const complex<X>&);
74    template<class X> complex<float>& operator-=(const complex<X>&);
75    template<class X> complex<float>& operator*=(const complex<X>&);
76    template<class X> complex<float>& operator/=(const complex<X>&);
77};
78
79template<>
80class complex<double>
81{
82public:
83    typedef double value_type;
84
85    constexpr complex(double re = 0.0, double im = 0.0);
86    constexpr complex(const complex<float>&);
87    explicit constexpr complex(const complex<long double>&);
88
89    constexpr double real() const;
90    void real(double);
91    constexpr double imag() const;
92    void imag(double);
93
94    complex<double>& operator= (double);
95    complex<double>& operator+=(double);
96    complex<double>& operator-=(double);
97    complex<double>& operator*=(double);
98    complex<double>& operator/=(double);
99    complex<double>& operator=(const complex<double>&);
100
101    template<class X> complex<double>& operator= (const complex<X>&);
102    template<class X> complex<double>& operator+=(const complex<X>&);
103    template<class X> complex<double>& operator-=(const complex<X>&);
104    template<class X> complex<double>& operator*=(const complex<X>&);
105    template<class X> complex<double>& operator/=(const complex<X>&);
106};
107
108template<>
109class complex<long double>
110{
111public:
112    typedef long double value_type;
113
114    constexpr complex(long double re = 0.0L, long double im = 0.0L);
115    constexpr complex(const complex<float>&);
116    constexpr complex(const complex<double>&);
117
118    constexpr long double real() const;
119    void real(long double);
120    constexpr long double imag() const;
121    void imag(long double);
122
123    complex<long double>& operator=(const complex<long double>&);
124    complex<long double>& operator= (long double);
125    complex<long double>& operator+=(long double);
126    complex<long double>& operator-=(long double);
127    complex<long double>& operator*=(long double);
128    complex<long double>& operator/=(long double);
129
130    template<class X> complex<long double>& operator= (const complex<X>&);
131    template<class X> complex<long double>& operator+=(const complex<X>&);
132    template<class X> complex<long double>& operator-=(const complex<X>&);
133    template<class X> complex<long double>& operator*=(const complex<X>&);
134    template<class X> complex<long double>& operator/=(const complex<X>&);
135};
136
137// 26.3.6 operators:
138template<class T> complex<T> operator+(const complex<T>&, const complex<T>&);
139template<class T> complex<T> operator+(const complex<T>&, const T&);
140template<class T> complex<T> operator+(const T&, const complex<T>&);
141template<class T> complex<T> operator-(const complex<T>&, const complex<T>&);
142template<class T> complex<T> operator-(const complex<T>&, const T&);
143template<class T> complex<T> operator-(const T&, const complex<T>&);
144template<class T> complex<T> operator*(const complex<T>&, const complex<T>&);
145template<class T> complex<T> operator*(const complex<T>&, const T&);
146template<class T> complex<T> operator*(const T&, const complex<T>&);
147template<class T> complex<T> operator/(const complex<T>&, const complex<T>&);
148template<class T> complex<T> operator/(const complex<T>&, const T&);
149template<class T> complex<T> operator/(const T&, const complex<T>&);
150template<class T> complex<T> operator+(const complex<T>&);
151template<class T> complex<T> operator-(const complex<T>&);
152template<class T> bool operator==(const complex<T>&, const complex<T>&);
153template<class T> bool operator==(const complex<T>&, const T&);
154template<class T> bool operator==(const T&, const complex<T>&);
155template<class T> bool operator!=(const complex<T>&, const complex<T>&);
156template<class T> bool operator!=(const complex<T>&, const T&);
157template<class T> bool operator!=(const T&, const complex<T>&);
158
159template<class T, class charT, class traits>
160  basic_istream<charT, traits>&
161  operator>>(basic_istream<charT, traits>&, complex<T>&);
162template<class T, class charT, class traits>
163  basic_ostream<charT, traits>&
164  operator<<(basic_ostream<charT, traits>&, const complex<T>&);
165
166// 26.3.7 values:
167
168template<class T>              T real(const complex<T>&);
169                     long double real(long double);
170                          double real(double);
171template<Integral T>      double real(T);
172                          float  real(float);
173
174template<class T>              T imag(const complex<T>&);
175                     long double imag(long double);
176                          double imag(double);
177template<Integral T>      double imag(T);
178                          float  imag(float);
179
180template<class T> T abs(const complex<T>&);
181
182template<class T>              T arg(const complex<T>&);
183                     long double arg(long double);
184                          double arg(double);
185template<Integral T>      double arg(T);
186                          float  arg(float);
187
188template<class T>              T norm(const complex<T>&);
189                     long double norm(long double);
190                          double norm(double);
191template<Integral T>      double norm(T);
192                          float  norm(float);
193
194template<class T>      complex<T>  conj(const complex<T>&);
195                       long double conj(long double);
196                       double      conj(double);
197template<Integral T>   double      conj(T);
198                       float       conj(float);
199
200template<class T>    complex<T>  proj(const complex<T>&);
201                     long double proj(long double);
202                     double      proj(double);
203template<Integral T> double      proj(T);
204                     float       proj(float);
205
206template<class T> complex<T> polar(const T&, const T& = 0);
207
208// 26.3.8 transcendentals:
209template<class T> complex<T> acos(const complex<T>&);
210template<class T> complex<T> asin(const complex<T>&);
211template<class T> complex<T> atan(const complex<T>&);
212template<class T> complex<T> acosh(const complex<T>&);
213template<class T> complex<T> asinh(const complex<T>&);
214template<class T> complex<T> atanh(const complex<T>&);
215template<class T> complex<T> cos (const complex<T>&);
216template<class T> complex<T> cosh (const complex<T>&);
217template<class T> complex<T> exp (const complex<T>&);
218template<class T> complex<T> log (const complex<T>&);
219template<class T> complex<T> log10(const complex<T>&);
220
221template<class T> complex<T> pow(const complex<T>&, const T&);
222template<class T> complex<T> pow(const complex<T>&, const complex<T>&);
223template<class T> complex<T> pow(const T&, const complex<T>&);
224
225template<class T> complex<T> sin (const complex<T>&);
226template<class T> complex<T> sinh (const complex<T>&);
227template<class T> complex<T> sqrt (const complex<T>&);
228template<class T> complex<T> tan (const complex<T>&);
229template<class T> complex<T> tanh (const complex<T>&);
230
231template<class T, class charT, class traits>
232  basic_istream<charT, traits>&
233  operator>>(basic_istream<charT, traits>& is, complex<T>& x);
234
235template<class T, class charT, class traits>
236  basic_ostream<charT, traits>&
237  operator<<(basic_ostream<charT, traits>& o, const complex<T>& x);
238
239}  // std
240
241*/
242
243#include <__config>
244#include <type_traits>
245#include <stdexcept>
246#include <cmath>
247#include <sstream>
248#if defined(_LIBCPP_NO_EXCEPTIONS)
249    #include <cassert>
250#endif
251
252#pragma GCC system_header
253
254_LIBCPP_BEGIN_NAMESPACE_STD
255
256template<class _Tp> class complex;
257
258template<class _Tp> complex<_Tp> operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
259template<class _Tp> complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
260
261template<class _Tp>
262class complex
263{
264public:
265    typedef _Tp value_type;
266private:
267    value_type __re_;
268    value_type __im_;
269public:
270    _LIBCPP_INLINE_VISIBILITY
271    complex(const value_type& __re = value_type(), const value_type& __im = value_type())
272        : __re_(__re), __im_(__im) {}
273    template<class _Xp> _LIBCPP_INLINE_VISIBILITY
274    complex(const complex<_Xp>& __c)
275        : __re_(__c.real()), __im_(__c.imag()) {}
276
277    _LIBCPP_INLINE_VISIBILITY value_type real() const {return __re_;}
278    _LIBCPP_INLINE_VISIBILITY value_type imag() const {return __im_;}
279
280    _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
281    _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
282
283    _LIBCPP_INLINE_VISIBILITY complex& operator= (const value_type& __re) {__re_ = __re; return *this;}
284    _LIBCPP_INLINE_VISIBILITY complex& operator+=(const value_type& __re) {__re_ += __re; return *this;}
285    _LIBCPP_INLINE_VISIBILITY complex& operator-=(const value_type& __re) {__re_ -= __re; return *this;}
286    _LIBCPP_INLINE_VISIBILITY complex& operator*=(const value_type& __re) {__re_ *= __re; __im_ *= __re; return *this;}
287    _LIBCPP_INLINE_VISIBILITY complex& operator/=(const value_type& __re) {__re_ /= __re; __im_ /= __re; return *this;}
288
289    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
290        {
291            __re_ = __c.real();
292            __im_ = __c.imag();
293            return *this;
294        }
295    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
296        {
297            __re_ += __c.real();
298            __im_ += __c.imag();
299            return *this;
300        }
301    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
302        {
303            __re_ -= __c.real();
304            __im_ -= __c.imag();
305            return *this;
306        }
307    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
308        {
309            *this = *this * __c;
310            return *this;
311        }
312    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
313        {
314            *this = *this / __c;
315            return *this;
316        }
317};
318
319template<> class complex<double>;
320template<> class complex<long double>;
321
322template<>
323class complex<float>
324{
325    float __re_;
326    float __im_;
327public:
328    typedef float value_type;
329
330    /*constexpr*/ _LIBCPP_INLINE_VISIBILITY complex(float __re = 0.0f, float __im = 0.0f)
331        : __re_(__re), __im_(__im) {}
332    explicit /*constexpr*/ complex(const complex<double>& __c);
333    explicit /*constexpr*/ complex(const complex<long double>& __c);
334
335    /*constexpr*/ _LIBCPP_INLINE_VISIBILITY float real() const {return __re_;}
336    /*constexpr*/ _LIBCPP_INLINE_VISIBILITY float imag() const {return __im_;}
337
338    _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
339    _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
340
341    _LIBCPP_INLINE_VISIBILITY complex& operator= (float __re) {__re_ = __re; return *this;}
342    _LIBCPP_INLINE_VISIBILITY complex& operator+=(float __re) {__re_ += __re; return *this;}
343    _LIBCPP_INLINE_VISIBILITY complex& operator-=(float __re) {__re_ -= __re; return *this;}
344    _LIBCPP_INLINE_VISIBILITY complex& operator*=(float __re) {__re_ *= __re; __im_ *= __re; return *this;}
345    _LIBCPP_INLINE_VISIBILITY complex& operator/=(float __re) {__re_ /= __re; __im_ /= __re; return *this;}
346
347    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
348        {
349            __re_ = __c.real();
350            __im_ = __c.imag();
351            return *this;
352        }
353    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
354        {
355            __re_ += __c.real();
356            __im_ += __c.imag();
357            return *this;
358        }
359    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
360        {
361            __re_ -= __c.real();
362            __im_ -= __c.imag();
363            return *this;
364        }
365    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
366        {
367            *this = *this * __c;
368            return *this;
369        }
370    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
371        {
372            *this = *this / __c;
373            return *this;
374        }
375};
376
377template<>
378class complex<double>
379{
380    double __re_;
381    double __im_;
382public:
383    typedef double value_type;
384
385    /*constexpr*/ _LIBCPP_INLINE_VISIBILITY complex(double __re = 0.0, double __im = 0.0)
386        : __re_(__re), __im_(__im) {}
387    /*constexpr*/ complex(const complex<float>& __c);
388    explicit /*constexpr*/ complex(const complex<long double>& __c);
389
390    /*constexpr*/ _LIBCPP_INLINE_VISIBILITY double real() const {return __re_;}
391    /*constexpr*/ _LIBCPP_INLINE_VISIBILITY double imag() const {return __im_;}
392
393    _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
394    _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
395
396    _LIBCPP_INLINE_VISIBILITY complex& operator= (double __re) {__re_ = __re; return *this;}
397    _LIBCPP_INLINE_VISIBILITY complex& operator+=(double __re) {__re_ += __re; return *this;}
398    _LIBCPP_INLINE_VISIBILITY complex& operator-=(double __re) {__re_ -= __re; return *this;}
399    _LIBCPP_INLINE_VISIBILITY complex& operator*=(double __re) {__re_ *= __re; __im_ *= __re; return *this;}
400    _LIBCPP_INLINE_VISIBILITY complex& operator/=(double __re) {__re_ /= __re; __im_ /= __re; return *this;}
401
402    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
403        {
404            __re_ = __c.real();
405            __im_ = __c.imag();
406            return *this;
407        }
408    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
409        {
410            __re_ += __c.real();
411            __im_ += __c.imag();
412            return *this;
413        }
414    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
415        {
416            __re_ -= __c.real();
417            __im_ -= __c.imag();
418            return *this;
419        }
420    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
421        {
422            *this = *this * __c;
423            return *this;
424        }
425    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
426        {
427            *this = *this / __c;
428            return *this;
429        }
430};
431
432template<>
433class complex<long double>
434{
435    long double __re_;
436    long double __im_;
437public:
438    typedef long double value_type;
439
440    /*constexpr*/ _LIBCPP_INLINE_VISIBILITY complex(long double __re = 0.0L, long double __im = 0.0L)
441        : __re_(__re), __im_(__im) {}
442    /*constexpr*/ complex(const complex<float>& __c);
443    /*constexpr*/ complex(const complex<double>& __c);
444
445    /*constexpr*/ _LIBCPP_INLINE_VISIBILITY long double real() const {return __re_;}
446    /*constexpr*/ _LIBCPP_INLINE_VISIBILITY long double imag() const {return __im_;}
447
448    _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
449    _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
450
451    _LIBCPP_INLINE_VISIBILITY complex& operator= (long double __re) {__re_ = __re; return *this;}
452    _LIBCPP_INLINE_VISIBILITY complex& operator+=(long double __re) {__re_ += __re; return *this;}
453    _LIBCPP_INLINE_VISIBILITY complex& operator-=(long double __re) {__re_ -= __re; return *this;}
454    _LIBCPP_INLINE_VISIBILITY complex& operator*=(long double __re) {__re_ *= __re; __im_ *= __re; return *this;}
455    _LIBCPP_INLINE_VISIBILITY complex& operator/=(long double __re) {__re_ /= __re; __im_ /= __re; return *this;}
456
457    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
458        {
459            __re_ = __c.real();
460            __im_ = __c.imag();
461            return *this;
462        }
463    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
464        {
465            __re_ += __c.real();
466            __im_ += __c.imag();
467            return *this;
468        }
469    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
470        {
471            __re_ -= __c.real();
472            __im_ -= __c.imag();
473            return *this;
474        }
475    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
476        {
477            *this = *this * __c;
478            return *this;
479        }
480    template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
481        {
482            *this = *this / __c;
483            return *this;
484        }
485};
486
487//constexpr
488inline _LIBCPP_INLINE_VISIBILITY
489complex<float>::complex(const complex<double>& __c)
490    : __re_(__c.real()), __im_(__c.imag()) {}
491
492//constexpr
493inline _LIBCPP_INLINE_VISIBILITY
494complex<float>::complex(const complex<long double>& __c)
495    : __re_(__c.real()), __im_(__c.imag()) {}
496
497//constexpr
498inline _LIBCPP_INLINE_VISIBILITY
499complex<double>::complex(const complex<float>& __c)
500    : __re_(__c.real()), __im_(__c.imag()) {}
501
502//constexpr
503inline _LIBCPP_INLINE_VISIBILITY
504complex<double>::complex(const complex<long double>& __c)
505    : __re_(__c.real()), __im_(__c.imag()) {}
506
507//constexpr
508inline _LIBCPP_INLINE_VISIBILITY
509complex<long double>::complex(const complex<float>& __c)
510    : __re_(__c.real()), __im_(__c.imag()) {}
511
512//constexpr
513inline _LIBCPP_INLINE_VISIBILITY
514complex<long double>::complex(const complex<double>& __c)
515    : __re_(__c.real()), __im_(__c.imag()) {}
516
517// 26.3.6 operators:
518
519template<class _Tp>
520inline _LIBCPP_INLINE_VISIBILITY
521complex<_Tp>
522operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
523{
524    complex<_Tp> __t(__x);
525    __t += __y;
526    return __t;
527}
528
529template<class _Tp>
530inline _LIBCPP_INLINE_VISIBILITY
531complex<_Tp>
532operator+(const complex<_Tp>& __x, const _Tp& __y)
533{
534    complex<_Tp> __t(__x);
535    __t += __y;
536    return __t;
537}
538
539template<class _Tp>
540inline _LIBCPP_INLINE_VISIBILITY
541complex<_Tp>
542operator+(const _Tp& __x, const complex<_Tp>& __y)
543{
544    complex<_Tp> __t(__y);
545    __t += __x;
546    return __t;
547}
548
549template<class _Tp>
550inline _LIBCPP_INLINE_VISIBILITY
551complex<_Tp>
552operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
553{
554    complex<_Tp> __t(__x);
555    __t -= __y;
556    return __t;
557}
558
559template<class _Tp>
560inline _LIBCPP_INLINE_VISIBILITY
561complex<_Tp>
562operator-(const complex<_Tp>& __x, const _Tp& __y)
563{
564    complex<_Tp> __t(__x);
565    __t -= __y;
566    return __t;
567}
568
569template<class _Tp>
570inline _LIBCPP_INLINE_VISIBILITY
571complex<_Tp>
572operator-(const _Tp& __x, const complex<_Tp>& __y)
573{
574    complex<_Tp> __t(-__y);
575    __t += __x;
576    return __t;
577}
578
579template<class _Tp>
580complex<_Tp>
581operator*(const complex<_Tp>& __z, const complex<_Tp>& __w)
582{
583    _Tp __a = __z.real();
584    _Tp __b = __z.imag();
585    _Tp __c = __w.real();
586    _Tp __d = __w.imag();
587    _Tp __ac = __a * __c;
588    _Tp __bd = __b * __d;
589    _Tp __ad = __a * __d;
590    _Tp __bc = __b * __c;
591    _Tp __x = __ac - __bd;
592    _Tp __y = __ad + __bc;
593    if (isnan(__x) && isnan(__y))
594    {
595        bool __recalc = false;
596        if (isinf(__a) || isinf(__b))
597        {
598            __a = copysign(isinf(__a) ? _Tp(1) : _Tp(0), __a);
599            __b = copysign(isinf(__b) ? _Tp(1) : _Tp(0), __b);
600            if (isnan(__c))
601                __c = copysign(_Tp(0), __c);
602            if (isnan(__d))
603                __d = copysign(_Tp(0), __d);
604            __recalc = true;
605        }
606        if (isinf(__c) || isinf(__d))
607        {
608            __c = copysign(isinf(__c) ? _Tp(1) : _Tp(0), __c);
609            __d = copysign(isinf(__d) ? _Tp(1) : _Tp(0), __d);
610            if (isnan(__a))
611                __a = copysign(_Tp(0), __a);
612            if (isnan(__b))
613                __b = copysign(_Tp(0), __b);
614            __recalc = true;
615        }
616        if (!__recalc && (isinf(__ac) || isinf(__bd) ||
617                          isinf(__ad) || isinf(__bc)))
618        {
619            if (isnan(__a))
620                __a = copysign(_Tp(0), __a);
621            if (isnan(__b))
622                __b = copysign(_Tp(0), __b);
623            if (isnan(__c))
624                __c = copysign(_Tp(0), __c);
625            if (isnan(__d))
626                __d = copysign(_Tp(0), __d);
627            __recalc = true;
628        }
629        if (__recalc)
630        {
631            __x = _Tp(INFINITY) * (__a * __c - __b * __d);
632            __y = _Tp(INFINITY) * (__a * __d + __b * __c);
633        }
634    }
635    return complex<_Tp>(__x, __y);
636}
637
638template<class _Tp>
639inline _LIBCPP_INLINE_VISIBILITY
640complex<_Tp>
641operator*(const complex<_Tp>& __x, const _Tp& __y)
642{
643    complex<_Tp> __t(__x);
644    __t *= __y;
645    return __t;
646}
647
648template<class _Tp>
649inline _LIBCPP_INLINE_VISIBILITY
650complex<_Tp>
651operator*(const _Tp& __x, const complex<_Tp>& __y)
652{
653    complex<_Tp> __t(__y);
654    __t *= __x;
655    return __t;
656}
657
658template<class _Tp>
659complex<_Tp>
660operator/(const complex<_Tp>& __z, const complex<_Tp>& __w)
661{
662    int __ilogbw = 0;
663    _Tp __a = __z.real();
664    _Tp __b = __z.imag();
665    _Tp __c = __w.real();
666    _Tp __d = __w.imag();
667    _Tp __logbw = logb(fmax(fabs(__c), fabs(__d)));
668    if (isfinite(__logbw))
669    {
670        __ilogbw = static_cast<int>(__logbw);
671        __c = scalbn(__c, -__ilogbw);
672        __d = scalbn(__d, -__ilogbw);
673    }
674    _Tp __denom = __c * __c + __d * __d;
675    _Tp __x = scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
676    _Tp __y = scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
677    if (isnan(__x) && isnan(__y))
678    {
679        if ((__denom == _Tp(0)) && (!isnan(__a) || !isnan(__b)))
680        {
681            __x = copysign(_Tp(INFINITY), __c) * __a;
682            __y = copysign(_Tp(INFINITY), __c) * __b;
683        }
684        else if ((isinf(__a) || isinf(__b)) && isfinite(__c) && isfinite(__d))
685        {
686            __a = copysign(isinf(__a) ? _Tp(1) : _Tp(0), __a);
687            __b = copysign(isinf(__b) ? _Tp(1) : _Tp(0), __b);
688            __x = _Tp(INFINITY) * (__a * __c + __b * __d);
689            __y = _Tp(INFINITY) * (__b * __c - __a * __d);
690        }
691        else if (isinf(__logbw) && __logbw > _Tp(0) && isfinite(__a) && isfinite(__b))
692        {
693            __c = copysign(isinf(__c) ? _Tp(1) : _Tp(0), __c);
694            __d = copysign(isinf(__d) ? _Tp(1) : _Tp(0), __d);
695            __x = _Tp(0) * (__a * __c + __b * __d);
696            __y = _Tp(0) * (__b * __c - __a * __d);
697        }
698    }
699    return complex<_Tp>(__x, __y);
700}
701
702template<class _Tp>
703inline _LIBCPP_INLINE_VISIBILITY
704complex<_Tp>
705operator/(const complex<_Tp>& __x, const _Tp& __y)
706{
707    return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
708}
709
710template<class _Tp>
711inline _LIBCPP_INLINE_VISIBILITY
712complex<_Tp>
713operator/(const _Tp& __x, const complex<_Tp>& __y)
714{
715    complex<_Tp> __t(__x);
716    __t /= __y;
717    return __t;
718}
719
720template<class _Tp>
721inline _LIBCPP_INLINE_VISIBILITY
722complex<_Tp>
723operator+(const complex<_Tp>& __x)
724{
725    return __x;
726}
727
728template<class _Tp>
729inline _LIBCPP_INLINE_VISIBILITY
730complex<_Tp>
731operator-(const complex<_Tp>& __x)
732{
733    return complex<_Tp>(-__x.real(), -__x.imag());
734}
735
736template<class _Tp>
737inline _LIBCPP_INLINE_VISIBILITY
738bool
739operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
740{
741    return __x.real() == __y.real() && __x.imag() == __y.imag();
742}
743
744template<class _Tp>
745inline _LIBCPP_INLINE_VISIBILITY
746bool
747operator==(const complex<_Tp>& __x, const _Tp& __y)
748{
749    return __x.real() == __y && __x.imag() == 0;
750}
751
752template<class _Tp>
753inline _LIBCPP_INLINE_VISIBILITY
754bool
755operator==(const _Tp& __x, const complex<_Tp>& __y)
756{
757    return __x == __y.real() && 0 == __y.imag();
758}
759
760template<class _Tp>
761inline _LIBCPP_INLINE_VISIBILITY
762bool
763operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
764{
765    return !(__x == __y);
766}
767
768template<class _Tp>
769inline _LIBCPP_INLINE_VISIBILITY
770bool
771operator!=(const complex<_Tp>& __x, const _Tp& __y)
772{
773    return !(__x == __y);
774}
775
776template<class _Tp>
777inline _LIBCPP_INLINE_VISIBILITY
778bool
779operator!=(const _Tp& __x, const complex<_Tp>& __y)
780{
781    return !(__x == __y);
782}
783
784/*
785    Move to <istream>
786
787template<class T, class charT, class traits>
788basic_istream<charT, traits>&
789operator>>(basic_istream<charT, traits>&, complex<T>&);
790
791    Move to <ostream>
792
793template<class T, class charT, class traits>
794basic_ostream<charT, traits>&
795operator<<(basic_ostream<charT, traits>&, const complex<T>&);
796*/
797
798// 26.3.7 values:
799
800// real
801
802template<class _Tp>
803inline _LIBCPP_INLINE_VISIBILITY
804_Tp
805real(const complex<_Tp>& __c)
806{
807    return __c.real();
808}
809
810inline _LIBCPP_INLINE_VISIBILITY
811long double
812real(long double __re)
813{
814    return __re;
815}
816
817inline _LIBCPP_INLINE_VISIBILITY
818double
819real(double __re)
820{
821    return __re;
822}
823
824template<class _Tp>
825inline _LIBCPP_INLINE_VISIBILITY
826typename enable_if
827<
828    is_integral<_Tp>::value,
829    double
830>::type
831real(_Tp  __re)
832{
833    return __re;
834}
835
836inline _LIBCPP_INLINE_VISIBILITY
837float
838real(float  __re)
839{
840    return __re;
841}
842
843// imag
844
845template<class _Tp>
846inline _LIBCPP_INLINE_VISIBILITY
847_Tp
848imag(const complex<_Tp>& __c)
849{
850    return __c.imag();
851}
852
853inline _LIBCPP_INLINE_VISIBILITY
854long double
855imag(long double __re)
856{
857    return 0;
858}
859
860inline _LIBCPP_INLINE_VISIBILITY
861double
862imag(double __re)
863{
864    return 0;
865}
866
867template<class _Tp>
868inline _LIBCPP_INLINE_VISIBILITY
869typename enable_if
870<
871    is_integral<_Tp>::value,
872    double
873>::type
874imag(_Tp  __re)
875{
876    return 0;
877}
878
879inline _LIBCPP_INLINE_VISIBILITY
880float
881imag(float  __re)
882{
883    return 0;
884}
885
886// abs
887
888template<class _Tp>
889inline _LIBCPP_INLINE_VISIBILITY
890_Tp
891abs(const complex<_Tp>& __c)
892{
893    return hypot(__c.real(), __c.imag());
894}
895
896// arg
897
898template<class _Tp>
899inline _LIBCPP_INLINE_VISIBILITY
900_Tp
901arg(const complex<_Tp>& __c)
902{
903    return atan2(__c.imag(), __c.real());
904}
905
906inline _LIBCPP_INLINE_VISIBILITY
907long double
908arg(long double __re)
909{
910    return atan2l(0.L, __re);
911}
912
913inline _LIBCPP_INLINE_VISIBILITY
914double
915arg(double __re)
916{
917    return atan2(0., __re);
918}
919
920template<class _Tp>
921inline _LIBCPP_INLINE_VISIBILITY
922typename enable_if
923<
924    is_integral<_Tp>::value,
925    double
926>::type
927arg(_Tp __re)
928{
929    return atan2(0., __re);
930}
931
932inline _LIBCPP_INLINE_VISIBILITY
933float
934arg(float __re)
935{
936    return atan2f(0.F, __re);
937}
938
939// norm
940
941template<class _Tp>
942inline _LIBCPP_INLINE_VISIBILITY
943_Tp
944norm(const complex<_Tp>& __c)
945{
946    if (isinf(__c.real()))
947        return abs(__c.real());
948    if (isinf(__c.imag()))
949        return abs(__c.imag());
950    return __c.real() * __c.real() + __c.imag() * __c.imag();
951}
952
953inline _LIBCPP_INLINE_VISIBILITY
954long double
955norm(long double __re)
956{
957    return __re * __re;
958}
959
960inline _LIBCPP_INLINE_VISIBILITY
961double
962norm(double __re)
963{
964    return __re * __re;
965}
966
967template<class _Tp>
968inline _LIBCPP_INLINE_VISIBILITY
969typename enable_if
970<
971    is_integral<_Tp>::value,
972    double
973>::type
974norm(_Tp __re)
975{
976    return (double)__re * __re;
977}
978
979inline _LIBCPP_INLINE_VISIBILITY
980float
981norm(float __re)
982{
983    return __re * __re;
984}
985
986// conj
987
988template<class _Tp>
989inline _LIBCPP_INLINE_VISIBILITY
990complex<_Tp>
991conj(const complex<_Tp>& __c)
992{
993    return complex<_Tp>(__c.real(), -__c.imag());
994}
995
996inline _LIBCPP_INLINE_VISIBILITY
997long double
998conj(long double __re)
999{
1000    return __re;
1001}
1002
1003inline _LIBCPP_INLINE_VISIBILITY
1004double
1005conj(double __re)
1006{
1007    return __re;
1008}
1009
1010template<class _Tp>
1011inline _LIBCPP_INLINE_VISIBILITY
1012typename enable_if
1013<
1014    is_integral<_Tp>::value,
1015    double
1016>::type
1017conj(_Tp __re)
1018{
1019    return __re;
1020}
1021
1022inline _LIBCPP_INLINE_VISIBILITY
1023float
1024conj(float __re)
1025{
1026    return __re;
1027}
1028
1029// proj
1030
1031template<class _Tp>
1032inline _LIBCPP_INLINE_VISIBILITY
1033complex<_Tp>
1034proj(const complex<_Tp>& __c)
1035{
1036    std::complex<_Tp> __r = __c;
1037    if (isinf(__c.real()) || isinf(__c.imag()))
1038        __r = complex<_Tp>(INFINITY, copysign(_Tp(0), __c.imag()));
1039    return __r;
1040}
1041
1042inline _LIBCPP_INLINE_VISIBILITY
1043long double
1044proj(long double __re)
1045{
1046    if (isinf(__re))
1047        __re = abs(__re);
1048    return __re;
1049}
1050
1051inline _LIBCPP_INLINE_VISIBILITY
1052double
1053proj(double __re)
1054{
1055    if (isinf(__re))
1056        __re = abs(__re);
1057    return __re;
1058}
1059
1060template<class _Tp>
1061inline _LIBCPP_INLINE_VISIBILITY
1062typename enable_if
1063<
1064    is_integral<_Tp>::value,
1065    double
1066>::type
1067proj(_Tp __re)
1068{
1069    return __re;
1070}
1071
1072inline _LIBCPP_INLINE_VISIBILITY
1073float
1074proj(float __re)
1075{
1076    if (isinf(__re))
1077        __re = abs(__re);
1078    return __re;
1079}
1080
1081// polar
1082
1083template<class _Tp>
1084complex<_Tp>
1085polar(const _Tp& __rho, const _Tp& __theta = _Tp(0))
1086{
1087    if (isnan(__rho) || signbit(__rho))
1088        return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1089    if (isnan(__theta))
1090    {
1091        if (isinf(__rho))
1092            return complex<_Tp>(__rho, __theta);
1093        return complex<_Tp>(__theta, __theta);
1094    }
1095    if (isinf(__theta))
1096    {
1097        if (isinf(__rho))
1098            return complex<_Tp>(__rho, _Tp(NAN));
1099        return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1100    }
1101    _Tp __x = __rho * cos(__theta);
1102    if (isnan(__x))
1103        __x = 0;
1104    _Tp __y = __rho * sin(__theta);
1105    if (isnan(__y))
1106        __y = 0;
1107    return complex<_Tp>(__x, __y);
1108}
1109
1110// log
1111
1112template<class _Tp>
1113inline _LIBCPP_INLINE_VISIBILITY
1114complex<_Tp>
1115log(const complex<_Tp>& __x)
1116{
1117    return complex<_Tp>(log(abs(__x)), arg(__x));
1118}
1119
1120// log10
1121
1122template<class _Tp>
1123inline _LIBCPP_INLINE_VISIBILITY
1124complex<_Tp>
1125log10(const complex<_Tp>& __x)
1126{
1127    return log(__x) / log(_Tp(10));
1128}
1129
1130// sqrt
1131
1132template<class _Tp>
1133complex<_Tp>
1134sqrt(const complex<_Tp>& __x)
1135{
1136    if (isinf(__x.imag()))
1137        return complex<_Tp>(_Tp(INFINITY), __x.imag());
1138    if (isinf(__x.real()))
1139    {
1140        if (__x.real() > _Tp(0))
1141            return complex<_Tp>(__x.real(), isnan(__x.imag()) ? __x.imag() : copysign(_Tp(0), __x.imag()));
1142        return complex<_Tp>(isnan(__x.imag()) ? __x.imag() : _Tp(0), copysign(__x.real(), __x.imag()));
1143    }
1144    return polar(sqrt(abs(__x)), arg(__x) / _Tp(2));
1145}
1146
1147// exp
1148
1149template<class _Tp>
1150complex<_Tp>
1151exp(const complex<_Tp>& __x)
1152{
1153    _Tp __i = __x.imag();
1154    if (isinf(__x.real()))
1155    {
1156        if (__x.real() < _Tp(0))
1157        {
1158            if (!isfinite(__i))
1159                __i = _Tp(1);
1160        }
1161        else if (__i == 0 || !isfinite(__i))
1162        {
1163            if (isinf(__i))
1164                __i = _Tp(NAN);
1165            return complex<_Tp>(__x.real(), __i);
1166        }
1167    }
1168    else if (isnan(__x.real()) && __x.imag() == 0)
1169        return __x;
1170    _Tp __e = exp(__x.real());
1171    return complex<_Tp>(__e * cos(__i), __e * sin(__i));
1172}
1173
1174// pow
1175
1176template<class _Tp>
1177inline _LIBCPP_INLINE_VISIBILITY
1178complex<_Tp>
1179pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1180{
1181    return exp(__y * log(__x));
1182}
1183
1184template<class _Tp, class _Up>
1185inline _LIBCPP_INLINE_VISIBILITY
1186complex<typename __promote<_Tp, _Up>::type>
1187pow(const complex<_Tp>& __x, const complex<_Up>& __y)
1188{
1189    typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1190    return _STD::pow(result_type(__x), result_type(__y));
1191}
1192
1193template<class _Tp, class _Up>
1194inline _LIBCPP_INLINE_VISIBILITY
1195typename enable_if
1196<
1197    is_arithmetic<_Up>::value,
1198    complex<typename __promote<_Tp, _Up>::type>
1199>::type
1200pow(const complex<_Tp>& __x, const _Up& __y)
1201{
1202    typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1203    return _STD::pow(result_type(__x), result_type(__y));
1204}
1205
1206template<class _Tp, class _Up>
1207inline _LIBCPP_INLINE_VISIBILITY
1208typename enable_if
1209<
1210    is_arithmetic<_Tp>::value,
1211    complex<typename __promote<_Tp, _Up>::type>
1212>::type
1213pow(const _Tp& __x, const complex<_Up>& __y)
1214{
1215    typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1216    return _STD::pow(result_type(__x), result_type(__y));
1217}
1218
1219// asinh
1220
1221template<class _Tp>
1222complex<_Tp>
1223asinh(const complex<_Tp>& __x)
1224{
1225    const _Tp __pi(atan2(+0., -0.));
1226    if (isinf(__x.real()))
1227    {
1228        if (isnan(__x.imag()))
1229            return __x;
1230        if (isinf(__x.imag()))
1231            return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1232        return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1233    }
1234    if (isnan(__x.real()))
1235    {
1236        if (isinf(__x.imag()))
1237            return complex<_Tp>(__x.imag(), __x.real());
1238        if (__x.imag() == 0)
1239            return __x;
1240        return complex<_Tp>(__x.real(), __x.real());
1241    }
1242    if (isinf(__x.imag()))
1243        return complex<_Tp>(copysign(__x.imag(), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1244    complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) + _Tp(1)));
1245    return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1246}
1247
1248// acosh
1249
1250template<class _Tp>
1251complex<_Tp>
1252acosh(const complex<_Tp>& __x)
1253{
1254    const _Tp __pi(atan2(+0., -0.));
1255    if (isinf(__x.real()))
1256    {
1257        if (isnan(__x.imag()))
1258            return complex<_Tp>(abs(__x.real()), __x.imag());
1259        if (isinf(__x.imag()))
1260            if (__x.real() > 0)
1261                return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1262            else
1263                return complex<_Tp>(-__x.real(), copysign(__pi * _Tp(0.75), __x.imag()));
1264        if (__x.real() < 0)
1265            return complex<_Tp>(-__x.real(), copysign(__pi, __x.imag()));
1266        return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1267    }
1268    if (isnan(__x.real()))
1269    {
1270        if (isinf(__x.imag()))
1271            return complex<_Tp>(abs(__x.imag()), __x.real());
1272        return complex<_Tp>(__x.real(), __x.real());
1273    }
1274    if (isinf(__x.imag()))
1275        return complex<_Tp>(abs(__x.imag()), copysign(__pi/_Tp(2), __x.imag()));
1276    complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) - _Tp(1)));
1277    return complex<_Tp>(copysign(__z.real(), _Tp(0)), copysign(__z.imag(), __x.imag()));
1278}
1279
1280// atanh
1281
1282template<class _Tp>
1283complex<_Tp>
1284atanh(const complex<_Tp>& __x)
1285{
1286    const _Tp __pi(atan2(+0., -0.));
1287    if (isinf(__x.imag()))
1288    {
1289        return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1290    }
1291    if (isnan(__x.imag()))
1292    {
1293        if (isinf(__x.real()) || __x.real() == 0)
1294            return complex<_Tp>(copysign(_Tp(0), __x.real()), __x.imag());
1295        return complex<_Tp>(__x.imag(), __x.imag());
1296    }
1297    if (isnan(__x.real()))
1298    {
1299        return complex<_Tp>(__x.real(), __x.real());
1300    }
1301    if (isinf(__x.real()))
1302    {
1303        return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1304    }
1305    if (abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0))
1306    {
1307        return complex<_Tp>(copysign(_Tp(INFINITY), __x.real()), copysign(_Tp(0), __x.imag()));
1308    }
1309    complex<_Tp> __z = log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2);
1310    return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1311}
1312
1313// sinh
1314
1315template<class _Tp>
1316complex<_Tp>
1317sinh(const complex<_Tp>& __x)
1318{
1319    if (isinf(__x.real()) && !isfinite(__x.imag()))
1320        return complex<_Tp>(__x.real(), _Tp(NAN));
1321    if (__x.real() == 0 && !isfinite(__x.imag()))
1322        return complex<_Tp>(__x.real(), _Tp(NAN));
1323    if (__x.imag() == 0 && !isfinite(__x.real()))
1324        return __x;
1325    return complex<_Tp>(sinh(__x.real()) * cos(__x.imag()), cosh(__x.real()) * sin(__x.imag()));
1326}
1327
1328// cosh
1329
1330template<class _Tp>
1331complex<_Tp>
1332cosh(const complex<_Tp>& __x)
1333{
1334    if (isinf(__x.real()) && !isfinite(__x.imag()))
1335        return complex<_Tp>(abs(__x.real()), _Tp(NAN));
1336    if (__x.real() == 0 && !isfinite(__x.imag()))
1337        return complex<_Tp>(_Tp(NAN), __x.real());
1338    if (__x.real() == 0 && __x.imag() == 0)
1339        return complex<_Tp>(_Tp(1), __x.imag());
1340    if (__x.imag() == 0 && !isfinite(__x.real()))
1341        return complex<_Tp>(abs(__x.real()), __x.imag());
1342    return complex<_Tp>(cosh(__x.real()) * cos(__x.imag()), sinh(__x.real()) * sin(__x.imag()));
1343}
1344
1345// tanh
1346
1347template<class _Tp>
1348complex<_Tp>
1349tanh(const complex<_Tp>& __x)
1350{
1351    if (isinf(__x.real()))
1352    {
1353        if (!isfinite(__x.imag()))
1354            return complex<_Tp>(_Tp(1), _Tp(0));
1355        return complex<_Tp>(_Tp(1), copysign(_Tp(0), sin(_Tp(2) * __x.imag())));
1356    }
1357    if (isnan(__x.real()) && __x.imag() == 0)
1358        return __x;
1359    _Tp __2r(_Tp(2) * __x.real());
1360    _Tp __2i(_Tp(2) * __x.imag());
1361    _Tp __d(cosh(__2r) + cos(__2i));
1362    return  complex<_Tp>(sinh(__2r)/__d, sin(__2i)/__d);
1363}
1364
1365// asin
1366
1367template<class _Tp>
1368complex<_Tp>
1369asin(const complex<_Tp>& __x)
1370{
1371    complex<_Tp> __z = asinh(complex<_Tp>(-__x.imag(), __x.real()));
1372    return complex<_Tp>(__z.imag(), -__z.real());
1373}
1374
1375// acos
1376
1377template<class _Tp>
1378complex<_Tp>
1379acos(const complex<_Tp>& __x)
1380{
1381    const _Tp __pi(atan2(+0., -0.));
1382    if (isinf(__x.real()))
1383    {
1384        if (isnan(__x.imag()))
1385            return complex<_Tp>(__x.imag(), __x.real());
1386        if (isinf(__x.imag()))
1387        {
1388            if (__x.real() < _Tp(0))
1389                return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());
1390            return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag());
1391        }
1392        if (__x.real() < _Tp(0))
1393            return complex<_Tp>(__pi, signbit(__x.imag()) ? -__x.real() : __x.real());
1394        return complex<_Tp>(_Tp(0), signbit(__x.imag()) ? __x.real() : -__x.real());
1395    }
1396    if (isnan(__x.real()))
1397    {
1398        if (isinf(__x.imag()))
1399            return complex<_Tp>(__x.real(), -__x.imag());
1400        return complex<_Tp>(__x.real(), __x.real());
1401    }
1402    if (isinf(__x.imag()))
1403        return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1404    if (__x.real() == 0)
1405        return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1406    complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) - _Tp(1)));
1407    if (signbit(__x.imag()))
1408        return complex<_Tp>(abs(__z.imag()), abs(__z.real()));
1409    return complex<_Tp>(abs(__z.imag()), -abs(__z.real()));
1410}
1411
1412// atan
1413
1414template<class _Tp>
1415complex<_Tp>
1416atan(const complex<_Tp>& __x)
1417{
1418    complex<_Tp> __z = atanh(complex<_Tp>(-__x.imag(), __x.real()));
1419    return complex<_Tp>(__z.imag(), -__z.real());
1420}
1421
1422// sin
1423
1424template<class _Tp>
1425complex<_Tp>
1426sin(const complex<_Tp>& __x)
1427{
1428    complex<_Tp> __z = sinh(complex<_Tp>(-__x.imag(), __x.real()));
1429    return complex<_Tp>(__z.imag(), -__z.real());
1430}
1431
1432// cos
1433
1434template<class _Tp>
1435inline _LIBCPP_INLINE_VISIBILITY
1436complex<_Tp>
1437cos(const complex<_Tp>& __x)
1438{
1439    return cosh(complex<_Tp>(-__x.imag(), __x.real()));
1440}
1441
1442// tan
1443
1444template<class _Tp>
1445complex<_Tp>
1446tan(const complex<_Tp>& __x)
1447{
1448    complex<_Tp> __z = tanh(complex<_Tp>(-__x.imag(), __x.real()));
1449    return complex<_Tp>(__z.imag(), -__z.real());
1450}
1451
1452template<class _Tp, class _CharT, class _Traits>
1453basic_istream<_CharT, _Traits>&
1454operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
1455{
1456    if (__is.good())
1457    {
1458        ws(__is);
1459        if (__is.peek() == _CharT('('))
1460        {
1461            __is.get();
1462            _Tp __r;
1463            __is >> __r;
1464            if (!__is.fail())
1465            {
1466                ws(__is);
1467                _CharT __c = __is.peek();
1468                if (__c == _CharT(','))
1469                {
1470                    __is.get();
1471                    _Tp __i;
1472                    __is >> __i;
1473                    if (!__is.fail())
1474                    {
1475                        ws(__is);
1476                        __c = __is.peek();
1477                        if (__c == _CharT(')'))
1478                        {
1479                            __is.get();
1480                            __x = complex<_Tp>(__r, __i);
1481                        }
1482                        else
1483                            __is.setstate(ios_base::failbit);
1484                    }
1485                    else
1486                        __is.setstate(ios_base::failbit);
1487                }
1488                else if (__c == _CharT(')'))
1489                {
1490                    __is.get();
1491                    __x = complex<_Tp>(__r, _Tp(0));
1492                }
1493                else
1494                    __is.setstate(ios_base::failbit);
1495            }
1496            else
1497                __is.setstate(ios_base::failbit);
1498        }
1499        else
1500        {
1501            _Tp __r;
1502            __is >> __r;
1503            if (!__is.fail())
1504                __x = complex<_Tp>(__r, _Tp(0));
1505            else
1506                __is.setstate(ios_base::failbit);
1507        }
1508    }
1509    else
1510        __is.setstate(ios_base::failbit);
1511    return __is;
1512}
1513
1514template<class _Tp, class _CharT, class _Traits>
1515basic_ostream<_CharT, _Traits>&
1516operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
1517{
1518    basic_ostringstream<_CharT, _Traits> __s;
1519    __s.flags(__os.flags());
1520    __s.imbue(__os.getloc());
1521    __s.precision(__os.precision());
1522    __s << '(' << __x.real() << ',' << __x.imag() << ')';
1523    return __os << __s.str();
1524}
1525
1526_LIBCPP_END_NAMESPACE_STD
1527
1528#endif  // _LIBCPP_COMPLEX
1529