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