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