2 //===--------------------------- complex ----------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_COMPLEX
12 #define _LIBCPP_COMPLEX
26 complex(const T& re = T(), const T& im = T());
27 complex(const complex&);
28 template<class X> complex(const complex<X>&);
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&);
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>&);
54 typedef float value_type;
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>&);
60 constexpr float real() const;
62 constexpr float imag() const;
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);
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>&);
83 typedef double value_type;
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>&);
89 constexpr double real() const;
91 constexpr double imag() const;
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>&);
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>&);
109 class complex<long double>
112 typedef long double value_type;
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>&);
118 constexpr long double real() const;
119 void real(long double);
120 constexpr long double imag() const;
121 void imag(long double);
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);
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>&);
138 template<class T> complex<T> operator+(const complex<T>&, const complex<T>&);
139 template<class T> complex<T> operator+(const complex<T>&, const T&);
140 template<class T> complex<T> operator+(const T&, const complex<T>&);
141 template<class T> complex<T> operator-(const complex<T>&, const complex<T>&);
142 template<class T> complex<T> operator-(const complex<T>&, const T&);
143 template<class T> complex<T> operator-(const T&, const complex<T>&);
144 template<class T> complex<T> operator*(const complex<T>&, const complex<T>&);
145 template<class T> complex<T> operator*(const complex<T>&, const T&);
146 template<class T> complex<T> operator*(const T&, const complex<T>&);
147 template<class T> complex<T> operator/(const complex<T>&, const complex<T>&);
148 template<class T> complex<T> operator/(const complex<T>&, const T&);
149 template<class T> complex<T> operator/(const T&, const complex<T>&);
150 template<class T> complex<T> operator+(const complex<T>&);
151 template<class T> complex<T> operator-(const complex<T>&);
152 template<class T> bool operator==(const complex<T>&, const complex<T>&);
153 template<class T> bool operator==(const complex<T>&, const T&);
154 template<class T> bool operator==(const T&, const complex<T>&);
155 template<class T> bool operator!=(const complex<T>&, const complex<T>&);
156 template<class T> bool operator!=(const complex<T>&, const T&);
157 template<class T> bool operator!=(const T&, const complex<T>&);
159 template<class T, class charT, class traits>
160 basic_istream<charT, traits>&
161 operator>>(basic_istream<charT, traits>&, complex<T>&);
162 template<class T, class charT, class traits>
163 basic_ostream<charT, traits>&
164 operator<<(basic_ostream<charT, traits>&, const complex<T>&);
168 template<class T> T real(const complex<T>&);
169 long double real(long double);
171 template<Integral T> double real(T);
174 template<class T> T imag(const complex<T>&);
175 long double imag(long double);
177 template<Integral T> double imag(T);
180 template<class T> T abs(const complex<T>&);
182 template<class T> T arg(const complex<T>&);
183 long double arg(long double);
185 template<Integral T> double arg(T);
188 template<class T> T norm(const complex<T>&);
189 long double norm(long double);
191 template<Integral T> double norm(T);
194 template<class T> complex<T> conj(const complex<T>&);
195 complex<long double> conj(long double);
196 complex<double> conj(double);
197 template<Integral T> complex<double> conj(T);
198 complex<float> conj(float);
200 template<class T> complex<T> proj(const complex<T>&);
201 complex<long double> proj(long double);
202 complex<double> proj(double);
203 template<Integral T> complex<double> proj(T);
204 complex<float> proj(float);
206 template<class T> complex<T> polar(const T&, const T& = 0);
208 // 26.3.8 transcendentals:
209 template<class T> complex<T> acos(const complex<T>&);
210 template<class T> complex<T> asin(const complex<T>&);
211 template<class T> complex<T> atan(const complex<T>&);
212 template<class T> complex<T> acosh(const complex<T>&);
213 template<class T> complex<T> asinh(const complex<T>&);
214 template<class T> complex<T> atanh(const complex<T>&);
215 template<class T> complex<T> cos (const complex<T>&);
216 template<class T> complex<T> cosh (const complex<T>&);
217 template<class T> complex<T> exp (const complex<T>&);
218 template<class T> complex<T> log (const complex<T>&);
219 template<class T> complex<T> log10(const complex<T>&);
221 template<class T> complex<T> pow(const complex<T>&, const T&);
222 template<class T> complex<T> pow(const complex<T>&, const complex<T>&);
223 template<class T> complex<T> pow(const T&, const complex<T>&);
225 template<class T> complex<T> sin (const complex<T>&);
226 template<class T> complex<T> sinh (const complex<T>&);
227 template<class T> complex<T> sqrt (const complex<T>&);
228 template<class T> complex<T> tan (const complex<T>&);
229 template<class T> complex<T> tanh (const complex<T>&);
231 template<class T, class charT, class traits>
232 basic_istream<charT, traits>&
233 operator>>(basic_istream<charT, traits>& is, complex<T>& x);
235 template<class T, class charT, class traits>
236 basic_ostream<charT, traits>&
237 operator<<(basic_ostream<charT, traits>& o, const complex<T>& x);
244 #include <type_traits>
248 #if defined(_LIBCPP_NO_EXCEPTIONS)
252 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
253 #pragma GCC system_header
256 _LIBCPP_BEGIN_NAMESPACE_STD
258 template<class _Tp> class _LIBCPP_VISIBLE complex;
260 template<class _Tp> complex<_Tp> operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
261 template<class _Tp> complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
264 class _LIBCPP_VISIBLE complex
267 typedef _Tp value_type;
272 _LIBCPP_INLINE_VISIBILITY
273 complex(const value_type& __re = value_type(), const value_type& __im = value_type())
274 : __re_(__re), __im_(__im) {}
275 template<class _Xp> _LIBCPP_INLINE_VISIBILITY
276 complex(const complex<_Xp>& __c)
277 : __re_(__c.real()), __im_(__c.imag()) {}
279 _LIBCPP_INLINE_VISIBILITY value_type real() const {return __re_;}
280 _LIBCPP_INLINE_VISIBILITY value_type imag() const {return __im_;}
282 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
283 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
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; return *this;}
287 _LIBCPP_INLINE_VISIBILITY complex& operator-=(const value_type& __re) {__re_ -= __re; return *this;}
288 _LIBCPP_INLINE_VISIBILITY complex& operator*=(const value_type& __re) {__re_ *= __re; __im_ *= __re; return *this;}
289 _LIBCPP_INLINE_VISIBILITY complex& operator/=(const value_type& __re) {__re_ /= __re; __im_ /= __re; return *this;}
291 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
297 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
303 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
309 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
314 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
321 template<> class _LIBCPP_VISIBLE complex<double>;
322 template<> class _LIBCPP_VISIBLE complex<long double>;
325 class _LIBCPP_VISIBLE complex<float>
330 typedef float value_type;
332 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY complex(float __re = 0.0f, float __im = 0.0f)
333 : __re_(__re), __im_(__im) {}
334 explicit /*constexpr*/ complex(const complex<double>& __c);
335 explicit /*constexpr*/ complex(const complex<long double>& __c);
337 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY float real() const {return __re_;}
338 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY float imag() const {return __im_;}
340 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
341 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
343 _LIBCPP_INLINE_VISIBILITY complex& operator= (float __re) {__re_ = __re; return *this;}
344 _LIBCPP_INLINE_VISIBILITY complex& operator+=(float __re) {__re_ += __re; return *this;}
345 _LIBCPP_INLINE_VISIBILITY complex& operator-=(float __re) {__re_ -= __re; return *this;}
346 _LIBCPP_INLINE_VISIBILITY complex& operator*=(float __re) {__re_ *= __re; __im_ *= __re; return *this;}
347 _LIBCPP_INLINE_VISIBILITY complex& operator/=(float __re) {__re_ /= __re; __im_ /= __re; return *this;}
349 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
355 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
361 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
367 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
372 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
380 class _LIBCPP_VISIBLE complex<double>
385 typedef double value_type;
387 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY complex(double __re = 0.0, double __im = 0.0)
388 : __re_(__re), __im_(__im) {}
389 /*constexpr*/ complex(const complex<float>& __c);
390 explicit /*constexpr*/ complex(const complex<long double>& __c);
392 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY double real() const {return __re_;}
393 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY double imag() const {return __im_;}
395 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
396 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
398 _LIBCPP_INLINE_VISIBILITY complex& operator= (double __re) {__re_ = __re; return *this;}
399 _LIBCPP_INLINE_VISIBILITY complex& operator+=(double __re) {__re_ += __re; return *this;}
400 _LIBCPP_INLINE_VISIBILITY complex& operator-=(double __re) {__re_ -= __re; return *this;}
401 _LIBCPP_INLINE_VISIBILITY complex& operator*=(double __re) {__re_ *= __re; __im_ *= __re; return *this;}
402 _LIBCPP_INLINE_VISIBILITY complex& operator/=(double __re) {__re_ /= __re; __im_ /= __re; return *this;}
404 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
410 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
416 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
422 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
427 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
435 class _LIBCPP_VISIBLE complex<long double>
440 typedef long double value_type;
442 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY complex(long double __re = 0.0L, long double __im = 0.0L)
443 : __re_(__re), __im_(__im) {}
444 /*constexpr*/ complex(const complex<float>& __c);
445 /*constexpr*/ complex(const complex<double>& __c);
447 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY long double real() const {return __re_;}
448 /*constexpr*/ _LIBCPP_INLINE_VISIBILITY long double imag() const {return __im_;}
450 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
451 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
453 _LIBCPP_INLINE_VISIBILITY complex& operator= (long double __re) {__re_ = __re; return *this;}
454 _LIBCPP_INLINE_VISIBILITY complex& operator+=(long double __re) {__re_ += __re; return *this;}
455 _LIBCPP_INLINE_VISIBILITY complex& operator-=(long double __re) {__re_ -= __re; return *this;}
456 _LIBCPP_INLINE_VISIBILITY complex& operator*=(long double __re) {__re_ *= __re; __im_ *= __re; return *this;}
457 _LIBCPP_INLINE_VISIBILITY complex& operator/=(long double __re) {__re_ /= __re; __im_ /= __re; return *this;}
459 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
465 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
471 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
477 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
482 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
490 inline _LIBCPP_INLINE_VISIBILITY
491 complex<float>::complex(const complex<double>& __c)
492 : __re_(__c.real()), __im_(__c.imag()) {}
495 inline _LIBCPP_INLINE_VISIBILITY
496 complex<float>::complex(const complex<long double>& __c)
497 : __re_(__c.real()), __im_(__c.imag()) {}
500 inline _LIBCPP_INLINE_VISIBILITY
501 complex<double>::complex(const complex<float>& __c)
502 : __re_(__c.real()), __im_(__c.imag()) {}
505 inline _LIBCPP_INLINE_VISIBILITY
506 complex<double>::complex(const complex<long double>& __c)
507 : __re_(__c.real()), __im_(__c.imag()) {}
510 inline _LIBCPP_INLINE_VISIBILITY
511 complex<long double>::complex(const complex<float>& __c)
512 : __re_(__c.real()), __im_(__c.imag()) {}
515 inline _LIBCPP_INLINE_VISIBILITY
516 complex<long double>::complex(const complex<double>& __c)
517 : __re_(__c.real()), __im_(__c.imag()) {}
522 inline _LIBCPP_INLINE_VISIBILITY
524 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
526 complex<_Tp> __t(__x);
532 inline _LIBCPP_INLINE_VISIBILITY
534 operator+(const complex<_Tp>& __x, const _Tp& __y)
536 complex<_Tp> __t(__x);
542 inline _LIBCPP_INLINE_VISIBILITY
544 operator+(const _Tp& __x, const complex<_Tp>& __y)
546 complex<_Tp> __t(__y);
552 inline _LIBCPP_INLINE_VISIBILITY
554 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
556 complex<_Tp> __t(__x);
562 inline _LIBCPP_INLINE_VISIBILITY
564 operator-(const complex<_Tp>& __x, const _Tp& __y)
566 complex<_Tp> __t(__x);
572 inline _LIBCPP_INLINE_VISIBILITY
574 operator-(const _Tp& __x, const complex<_Tp>& __y)
576 complex<_Tp> __t(-__y);
583 operator*(const complex<_Tp>& __z, const complex<_Tp>& __w)
585 _Tp __a = __z.real();
586 _Tp __b = __z.imag();
587 _Tp __c = __w.real();
588 _Tp __d = __w.imag();
589 _Tp __ac = __a * __c;
590 _Tp __bd = __b * __d;
591 _Tp __ad = __a * __d;
592 _Tp __bc = __b * __c;
593 _Tp __x = __ac - __bd;
594 _Tp __y = __ad + __bc;
595 if (isnan(__x) && isnan(__y))
597 bool __recalc = false;
598 if (isinf(__a) || isinf(__b))
600 __a = copysign(isinf(__a) ? _Tp(1) : _Tp(0), __a);
601 __b = copysign(isinf(__b) ? _Tp(1) : _Tp(0), __b);
603 __c = copysign(_Tp(0), __c);
605 __d = copysign(_Tp(0), __d);
608 if (isinf(__c) || isinf(__d))
610 __c = copysign(isinf(__c) ? _Tp(1) : _Tp(0), __c);
611 __d = copysign(isinf(__d) ? _Tp(1) : _Tp(0), __d);
613 __a = copysign(_Tp(0), __a);
615 __b = copysign(_Tp(0), __b);
618 if (!__recalc && (isinf(__ac) || isinf(__bd) ||
619 isinf(__ad) || isinf(__bc)))
622 __a = copysign(_Tp(0), __a);
624 __b = copysign(_Tp(0), __b);
626 __c = copysign(_Tp(0), __c);
628 __d = copysign(_Tp(0), __d);
633 __x = _Tp(INFINITY) * (__a * __c - __b * __d);
634 __y = _Tp(INFINITY) * (__a * __d + __b * __c);
637 return complex<_Tp>(__x, __y);
641 inline _LIBCPP_INLINE_VISIBILITY
643 operator*(const complex<_Tp>& __x, const _Tp& __y)
645 complex<_Tp> __t(__x);
651 inline _LIBCPP_INLINE_VISIBILITY
653 operator*(const _Tp& __x, const complex<_Tp>& __y)
655 complex<_Tp> __t(__y);
662 operator/(const complex<_Tp>& __z, const complex<_Tp>& __w)
665 _Tp __a = __z.real();
666 _Tp __b = __z.imag();
667 _Tp __c = __w.real();
668 _Tp __d = __w.imag();
669 _Tp __logbw = logb(fmax(fabs(__c), fabs(__d)));
670 if (isfinite(__logbw))
672 __ilogbw = static_cast<int>(__logbw);
673 __c = scalbn(__c, -__ilogbw);
674 __d = scalbn(__d, -__ilogbw);
676 _Tp __denom = __c * __c + __d * __d;
677 _Tp __x = scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
678 _Tp __y = scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
679 if (isnan(__x) && isnan(__y))
681 if ((__denom == _Tp(0)) && (!isnan(__a) || !isnan(__b)))
683 __x = copysign(_Tp(INFINITY), __c) * __a;
684 __y = copysign(_Tp(INFINITY), __c) * __b;
686 else if ((isinf(__a) || isinf(__b)) && isfinite(__c) && isfinite(__d))
688 __a = copysign(isinf(__a) ? _Tp(1) : _Tp(0), __a);
689 __b = copysign(isinf(__b) ? _Tp(1) : _Tp(0), __b);
690 __x = _Tp(INFINITY) * (__a * __c + __b * __d);
691 __y = _Tp(INFINITY) * (__b * __c - __a * __d);
693 else if (isinf(__logbw) && __logbw > _Tp(0) && isfinite(__a) && isfinite(__b))
695 __c = copysign(isinf(__c) ? _Tp(1) : _Tp(0), __c);
696 __d = copysign(isinf(__d) ? _Tp(1) : _Tp(0), __d);
697 __x = _Tp(0) * (__a * __c + __b * __d);
698 __y = _Tp(0) * (__b * __c - __a * __d);
701 return complex<_Tp>(__x, __y);
705 inline _LIBCPP_INLINE_VISIBILITY
707 operator/(const complex<_Tp>& __x, const _Tp& __y)
709 return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
713 inline _LIBCPP_INLINE_VISIBILITY
715 operator/(const _Tp& __x, const complex<_Tp>& __y)
717 complex<_Tp> __t(__x);
723 inline _LIBCPP_INLINE_VISIBILITY
725 operator+(const complex<_Tp>& __x)
731 inline _LIBCPP_INLINE_VISIBILITY
733 operator-(const complex<_Tp>& __x)
735 return complex<_Tp>(-__x.real(), -__x.imag());
739 inline _LIBCPP_INLINE_VISIBILITY
741 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
743 return __x.real() == __y.real() && __x.imag() == __y.imag();
747 inline _LIBCPP_INLINE_VISIBILITY
749 operator==(const complex<_Tp>& __x, const _Tp& __y)
751 return __x.real() == __y && __x.imag() == 0;
755 inline _LIBCPP_INLINE_VISIBILITY
757 operator==(const _Tp& __x, const complex<_Tp>& __y)
759 return __x == __y.real() && 0 == __y.imag();
763 inline _LIBCPP_INLINE_VISIBILITY
765 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
767 return !(__x == __y);
771 inline _LIBCPP_INLINE_VISIBILITY
773 operator!=(const complex<_Tp>& __x, const _Tp& __y)
775 return !(__x == __y);
779 inline _LIBCPP_INLINE_VISIBILITY
781 operator!=(const _Tp& __x, const complex<_Tp>& __y)
783 return !(__x == __y);
791 inline _LIBCPP_INLINE_VISIBILITY
793 real(const complex<_Tp>& __c)
798 inline _LIBCPP_INLINE_VISIBILITY
800 real(long double __re)
805 inline _LIBCPP_INLINE_VISIBILITY
813 inline _LIBCPP_INLINE_VISIBILITY
816 is_integral<_Tp>::value,
824 inline _LIBCPP_INLINE_VISIBILITY
834 inline _LIBCPP_INLINE_VISIBILITY
836 imag(const complex<_Tp>& __c)
841 inline _LIBCPP_INLINE_VISIBILITY
843 imag(long double __re)
848 inline _LIBCPP_INLINE_VISIBILITY
856 inline _LIBCPP_INLINE_VISIBILITY
859 is_integral<_Tp>::value,
867 inline _LIBCPP_INLINE_VISIBILITY
877 inline _LIBCPP_INLINE_VISIBILITY
879 abs(const complex<_Tp>& __c)
881 return hypot(__c.real(), __c.imag());
887 inline _LIBCPP_INLINE_VISIBILITY
889 arg(const complex<_Tp>& __c)
891 return atan2(__c.imag(), __c.real());
894 inline _LIBCPP_INLINE_VISIBILITY
896 arg(long double __re)
898 return atan2l(0.L, __re);
901 inline _LIBCPP_INLINE_VISIBILITY
905 return atan2(0., __re);
909 inline _LIBCPP_INLINE_VISIBILITY
912 is_integral<_Tp>::value,
917 return atan2(0., __re);
920 inline _LIBCPP_INLINE_VISIBILITY
924 return atan2f(0.F, __re);
930 inline _LIBCPP_INLINE_VISIBILITY
932 norm(const complex<_Tp>& __c)
934 if (isinf(__c.real()))
935 return abs(__c.real());
936 if (isinf(__c.imag()))
937 return abs(__c.imag());
938 return __c.real() * __c.real() + __c.imag() * __c.imag();
941 inline _LIBCPP_INLINE_VISIBILITY
943 norm(long double __re)
948 inline _LIBCPP_INLINE_VISIBILITY
956 inline _LIBCPP_INLINE_VISIBILITY
959 is_integral<_Tp>::value,
964 return (double)__re * __re;
967 inline _LIBCPP_INLINE_VISIBILITY
977 inline _LIBCPP_INLINE_VISIBILITY
979 conj(const complex<_Tp>& __c)
981 return complex<_Tp>(__c.real(), -__c.imag());
984 inline _LIBCPP_INLINE_VISIBILITY
986 conj(long double __re)
988 return complex<long double>(__re);
991 inline _LIBCPP_INLINE_VISIBILITY
995 return complex<double>(__re);
999 inline _LIBCPP_INLINE_VISIBILITY
1002 is_integral<_Tp>::value,
1007 return complex<double>(__re);
1010 inline _LIBCPP_INLINE_VISIBILITY
1014 return complex<float>(__re);
1020 inline _LIBCPP_INLINE_VISIBILITY
1022 proj(const complex<_Tp>& __c)
1024 std::complex<_Tp> __r = __c;
1025 if (isinf(__c.real()) || isinf(__c.imag()))
1026 __r = complex<_Tp>(INFINITY, copysign(_Tp(0), __c.imag()));
1030 inline _LIBCPP_INLINE_VISIBILITY
1031 complex<long double>
1032 proj(long double __re)
1036 return complex<long double>(__re);
1039 inline _LIBCPP_INLINE_VISIBILITY
1045 return complex<double>(__re);
1049 inline _LIBCPP_INLINE_VISIBILITY
1052 is_integral<_Tp>::value,
1057 return complex<double>(__re);
1060 inline _LIBCPP_INLINE_VISIBILITY
1066 return complex<float>(__re);
1073 polar(const _Tp& __rho, const _Tp& __theta = _Tp(0))
1075 if (isnan(__rho) || signbit(__rho))
1076 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1080 return complex<_Tp>(__rho, __theta);
1081 return complex<_Tp>(__theta, __theta);
1086 return complex<_Tp>(__rho, _Tp(NAN));
1087 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1089 _Tp __x = __rho * cos(__theta);
1092 _Tp __y = __rho * sin(__theta);
1095 return complex<_Tp>(__x, __y);
1101 inline _LIBCPP_INLINE_VISIBILITY
1103 log(const complex<_Tp>& __x)
1105 return complex<_Tp>(log(abs(__x)), arg(__x));
1111 inline _LIBCPP_INLINE_VISIBILITY
1113 log10(const complex<_Tp>& __x)
1115 return log(__x) / log(_Tp(10));
1122 sqrt(const complex<_Tp>& __x)
1124 if (isinf(__x.imag()))
1125 return complex<_Tp>(_Tp(INFINITY), __x.imag());
1126 if (isinf(__x.real()))
1128 if (__x.real() > _Tp(0))
1129 return complex<_Tp>(__x.real(), isnan(__x.imag()) ? __x.imag() : copysign(_Tp(0), __x.imag()));
1130 return complex<_Tp>(isnan(__x.imag()) ? __x.imag() : _Tp(0), copysign(__x.real(), __x.imag()));
1132 return polar(sqrt(abs(__x)), arg(__x) / _Tp(2));
1139 exp(const complex<_Tp>& __x)
1141 _Tp __i = __x.imag();
1142 if (isinf(__x.real()))
1144 if (__x.real() < _Tp(0))
1149 else if (__i == 0 || !isfinite(__i))
1153 return complex<_Tp>(__x.real(), __i);
1156 else if (isnan(__x.real()) && __x.imag() == 0)
1158 _Tp __e = exp(__x.real());
1159 return complex<_Tp>(__e * cos(__i), __e * sin(__i));
1165 inline _LIBCPP_INLINE_VISIBILITY
1167 pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1169 return exp(__y * log(__x));
1172 template<class _Tp, class _Up>
1173 inline _LIBCPP_INLINE_VISIBILITY
1174 complex<typename __promote<_Tp, _Up>::type>
1175 pow(const complex<_Tp>& __x, const complex<_Up>& __y)
1177 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1178 return _VSTD::pow(result_type(__x), result_type(__y));
1181 template<class _Tp, class _Up>
1182 inline _LIBCPP_INLINE_VISIBILITY
1185 is_arithmetic<_Up>::value,
1186 complex<typename __promote<_Tp, _Up>::type>
1188 pow(const complex<_Tp>& __x, const _Up& __y)
1190 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1191 return _VSTD::pow(result_type(__x), result_type(__y));
1194 template<class _Tp, class _Up>
1195 inline _LIBCPP_INLINE_VISIBILITY
1198 is_arithmetic<_Tp>::value,
1199 complex<typename __promote<_Tp, _Up>::type>
1201 pow(const _Tp& __x, const complex<_Up>& __y)
1203 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
1204 return _VSTD::pow(result_type(__x), result_type(__y));
1211 asinh(const complex<_Tp>& __x)
1213 const _Tp __pi(atan2(+0., -0.));
1214 if (isinf(__x.real()))
1216 if (isnan(__x.imag()))
1218 if (isinf(__x.imag()))
1219 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1220 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1222 if (isnan(__x.real()))
1224 if (isinf(__x.imag()))
1225 return complex<_Tp>(__x.imag(), __x.real());
1226 if (__x.imag() == 0)
1228 return complex<_Tp>(__x.real(), __x.real());
1230 if (isinf(__x.imag()))
1231 return complex<_Tp>(copysign(__x.imag(), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1232 complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) + _Tp(1)));
1233 return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1240 acosh(const complex<_Tp>& __x)
1242 const _Tp __pi(atan2(+0., -0.));
1243 if (isinf(__x.real()))
1245 if (isnan(__x.imag()))
1246 return complex<_Tp>(abs(__x.real()), __x.imag());
1247 if (isinf(__x.imag()))
1249 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1251 return complex<_Tp>(-__x.real(), copysign(__pi * _Tp(0.75), __x.imag()));
1253 return complex<_Tp>(-__x.real(), copysign(__pi, __x.imag()));
1254 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1256 if (isnan(__x.real()))
1258 if (isinf(__x.imag()))
1259 return complex<_Tp>(abs(__x.imag()), __x.real());
1260 return complex<_Tp>(__x.real(), __x.real());
1262 if (isinf(__x.imag()))
1263 return complex<_Tp>(abs(__x.imag()), copysign(__pi/_Tp(2), __x.imag()));
1264 complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) - _Tp(1)));
1265 return complex<_Tp>(copysign(__z.real(), _Tp(0)), copysign(__z.imag(), __x.imag()));
1272 atanh(const complex<_Tp>& __x)
1274 const _Tp __pi(atan2(+0., -0.));
1275 if (isinf(__x.imag()))
1277 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1279 if (isnan(__x.imag()))
1281 if (isinf(__x.real()) || __x.real() == 0)
1282 return complex<_Tp>(copysign(_Tp(0), __x.real()), __x.imag());
1283 return complex<_Tp>(__x.imag(), __x.imag());
1285 if (isnan(__x.real()))
1287 return complex<_Tp>(__x.real(), __x.real());
1289 if (isinf(__x.real()))
1291 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1293 if (abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0))
1295 return complex<_Tp>(copysign(_Tp(INFINITY), __x.real()), copysign(_Tp(0), __x.imag()));
1297 complex<_Tp> __z = log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2);
1298 return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1305 sinh(const complex<_Tp>& __x)
1307 if (isinf(__x.real()) && !isfinite(__x.imag()))
1308 return complex<_Tp>(__x.real(), _Tp(NAN));
1309 if (__x.real() == 0 && !isfinite(__x.imag()))
1310 return complex<_Tp>(__x.real(), _Tp(NAN));
1311 if (__x.imag() == 0 && !isfinite(__x.real()))
1313 return complex<_Tp>(sinh(__x.real()) * cos(__x.imag()), cosh(__x.real()) * sin(__x.imag()));
1320 cosh(const complex<_Tp>& __x)
1322 if (isinf(__x.real()) && !isfinite(__x.imag()))
1323 return complex<_Tp>(abs(__x.real()), _Tp(NAN));
1324 if (__x.real() == 0 && !isfinite(__x.imag()))
1325 return complex<_Tp>(_Tp(NAN), __x.real());
1326 if (__x.real() == 0 && __x.imag() == 0)
1327 return complex<_Tp>(_Tp(1), __x.imag());
1328 if (__x.imag() == 0 && !isfinite(__x.real()))
1329 return complex<_Tp>(abs(__x.real()), __x.imag());
1330 return complex<_Tp>(cosh(__x.real()) * cos(__x.imag()), sinh(__x.real()) * sin(__x.imag()));
1337 tanh(const complex<_Tp>& __x)
1339 if (isinf(__x.real()))
1341 if (!isfinite(__x.imag()))
1342 return complex<_Tp>(_Tp(1), _Tp(0));
1343 return complex<_Tp>(_Tp(1), copysign(_Tp(0), sin(_Tp(2) * __x.imag())));
1345 if (isnan(__x.real()) && __x.imag() == 0)
1347 _Tp __2r(_Tp(2) * __x.real());
1348 _Tp __2i(_Tp(2) * __x.imag());
1349 _Tp __d(cosh(__2r) + cos(__2i));
1350 return complex<_Tp>(sinh(__2r)/__d, sin(__2i)/__d);
1357 asin(const complex<_Tp>& __x)
1359 complex<_Tp> __z = asinh(complex<_Tp>(-__x.imag(), __x.real()));
1360 return complex<_Tp>(__z.imag(), -__z.real());
1367 acos(const complex<_Tp>& __x)
1369 const _Tp __pi(atan2(+0., -0.));
1370 if (isinf(__x.real()))
1372 if (isnan(__x.imag()))
1373 return complex<_Tp>(__x.imag(), __x.real());
1374 if (isinf(__x.imag()))
1376 if (__x.real() < _Tp(0))
1377 return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());
1378 return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag());
1380 if (__x.real() < _Tp(0))
1381 return complex<_Tp>(__pi, signbit(__x.imag()) ? -__x.real() : __x.real());
1382 return complex<_Tp>(_Tp(0), signbit(__x.imag()) ? __x.real() : -__x.real());
1384 if (isnan(__x.real()))
1386 if (isinf(__x.imag()))
1387 return complex<_Tp>(__x.real(), -__x.imag());
1388 return complex<_Tp>(__x.real(), __x.real());
1390 if (isinf(__x.imag()))
1391 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1392 if (__x.real() == 0)
1393 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1394 complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) - _Tp(1)));
1395 if (signbit(__x.imag()))
1396 return complex<_Tp>(abs(__z.imag()), abs(__z.real()));
1397 return complex<_Tp>(abs(__z.imag()), -abs(__z.real()));
1404 atan(const complex<_Tp>& __x)
1406 complex<_Tp> __z = atanh(complex<_Tp>(-__x.imag(), __x.real()));
1407 return complex<_Tp>(__z.imag(), -__z.real());
1414 sin(const complex<_Tp>& __x)
1416 complex<_Tp> __z = sinh(complex<_Tp>(-__x.imag(), __x.real()));
1417 return complex<_Tp>(__z.imag(), -__z.real());
1423 inline _LIBCPP_INLINE_VISIBILITY
1425 cos(const complex<_Tp>& __x)
1427 return cosh(complex<_Tp>(-__x.imag(), __x.real()));
1434 tan(const complex<_Tp>& __x)
1436 complex<_Tp> __z = tanh(complex<_Tp>(-__x.imag(), __x.real()));
1437 return complex<_Tp>(__z.imag(), -__z.real());
1440 template<class _Tp, class _CharT, class _Traits>
1441 basic_istream<_CharT, _Traits>&
1442 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
1447 if (__is.peek() == _CharT('('))
1455 _CharT __c = __is.peek();
1456 if (__c == _CharT(','))
1465 if (__c == _CharT(')'))
1468 __x = complex<_Tp>(__r, __i);
1471 __is.setstate(ios_base::failbit);
1474 __is.setstate(ios_base::failbit);
1476 else if (__c == _CharT(')'))
1479 __x = complex<_Tp>(__r, _Tp(0));
1482 __is.setstate(ios_base::failbit);
1485 __is.setstate(ios_base::failbit);
1492 __x = complex<_Tp>(__r, _Tp(0));
1494 __is.setstate(ios_base::failbit);
1498 __is.setstate(ios_base::failbit);
1502 template<class _Tp, class _CharT, class _Traits>
1503 basic_ostream<_CharT, _Traits>&
1504 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
1506 basic_ostringstream<_CharT, _Traits> __s;
1507 __s.flags(__os.flags());
1508 __s.imbue(__os.getloc());
1509 __s.precision(__os.precision());
1510 __s << '(' << __x.real() << ',' << __x.imag() << ')';
1511 return __os << __s.str();
1514 _LIBCPP_END_NAMESPACE_STD
1516 #endif // _LIBCPP_COMPLEX