blob: 8cf6a946d711f7cf4e1347cb070e82800e3d701b [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- complex ----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_COMPLEX
12#define _LIBCPP_COMPLEX
13
14/*
15 complex synopsis
16
17namespace std
18{
19
20template<class T>
21class complex
22{
23public:
24 typedef T value_type;
25
Marshall Clowa61e6f82013-07-31 21:02:34 +000026 complex(const T& re = T(), const T& im = T()); // constexpr in C++14
27 complex(const complex&); // constexpr in C++14
28 template<class X> complex(const complex<X>&); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000029
Marshall Clowa61e6f82013-07-31 21:02:34 +000030 T real() const; // constexpr in C++14
31 T imag() const; // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000032
33 void real(T);
34 void imag(T);
35
36 complex<T>& operator= (const T&);
37 complex<T>& operator+=(const T&);
38 complex<T>& operator-=(const T&);
39 complex<T>& operator*=(const T&);
40 complex<T>& operator/=(const T&);
41
42 complex& operator=(const complex&);
43 template<class X> complex<T>& operator= (const complex<X>&);
44 template<class X> complex<T>& operator+=(const complex<X>&);
45 template<class X> complex<T>& operator-=(const complex<X>&);
46 template<class X> complex<T>& operator*=(const complex<X>&);
47 template<class X> complex<T>& operator/=(const complex<X>&);
48};
49
50template<>
51class complex<float>
Howard Hinnant324bb032010-08-22 00:02:43 +000052{
53public:
54 typedef float value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000055
Howard Hinnant324bb032010-08-22 00:02:43 +000056 constexpr complex(float re = 0.0f, float im = 0.0f);
57 explicit constexpr complex(const complex<double>&);
58 explicit constexpr complex(const complex<long double>&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000059
Howard Hinnant324bb032010-08-22 00:02:43 +000060 constexpr float real() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000061 void real(float);
Howard Hinnant324bb032010-08-22 00:02:43 +000062 constexpr float imag() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000063 void imag(float);
64
Howard Hinnant324bb032010-08-22 00:02:43 +000065 complex<float>& operator= (float);
66 complex<float>& operator+=(float);
67 complex<float>& operator-=(float);
68 complex<float>& operator*=(float);
69 complex<float>& operator/=(float);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000070
Howard Hinnant324bb032010-08-22 00:02:43 +000071 complex<float>& operator=(const complex<float>&);
72 template<class X> complex<float>& operator= (const complex<X>&);
73 template<class X> complex<float>& operator+=(const complex<X>&);
74 template<class X> complex<float>& operator-=(const complex<X>&);
75 template<class X> complex<float>& operator*=(const complex<X>&);
76 template<class X> complex<float>& operator/=(const complex<X>&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000077};
78
79template<>
80class complex<double>
Howard Hinnant324bb032010-08-22 00:02:43 +000081{
82public:
83 typedef double value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000084
Howard Hinnant324bb032010-08-22 00:02:43 +000085 constexpr complex(double re = 0.0, double im = 0.0);
86 constexpr complex(const complex<float>&);
87 explicit constexpr complex(const complex<long double>&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000088
Howard Hinnant324bb032010-08-22 00:02:43 +000089 constexpr double real() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000090 void real(double);
Howard Hinnant324bb032010-08-22 00:02:43 +000091 constexpr double imag() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000092 void imag(double);
93
Howard Hinnant324bb032010-08-22 00:02:43 +000094 complex<double>& operator= (double);
95 complex<double>& operator+=(double);
96 complex<double>& operator-=(double);
97 complex<double>& operator*=(double);
98 complex<double>& operator/=(double);
99 complex<double>& operator=(const complex<double>&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000100
Howard Hinnant324bb032010-08-22 00:02:43 +0000101 template<class X> complex<double>& operator= (const complex<X>&);
102 template<class X> complex<double>& operator+=(const complex<X>&);
103 template<class X> complex<double>& operator-=(const complex<X>&);
104 template<class X> complex<double>& operator*=(const complex<X>&);
105 template<class X> complex<double>& operator/=(const complex<X>&);
106};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000107
108template<>
109class complex<long double>
Howard Hinnant324bb032010-08-22 00:02:43 +0000110{
111public:
112 typedef long double value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000113
Howard Hinnant324bb032010-08-22 00:02:43 +0000114 constexpr complex(long double re = 0.0L, long double im = 0.0L);
115 constexpr complex(const complex<float>&);
116 constexpr complex(const complex<double>&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000117
Howard Hinnant324bb032010-08-22 00:02:43 +0000118 constexpr long double real() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000119 void real(long double);
Howard Hinnant324bb032010-08-22 00:02:43 +0000120 constexpr long double imag() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000121 void imag(long double);
122
Howard Hinnant324bb032010-08-22 00:02:43 +0000123 complex<long double>& operator=(const complex<long double>&);
124 complex<long double>& operator= (long double);
125 complex<long double>& operator+=(long double);
126 complex<long double>& operator-=(long double);
127 complex<long double>& operator*=(long double);
128 complex<long double>& operator/=(long double);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000129
Howard Hinnant324bb032010-08-22 00:02:43 +0000130 template<class X> complex<long double>& operator= (const complex<X>&);
131 template<class X> complex<long double>& operator+=(const complex<X>&);
132 template<class X> complex<long double>& operator-=(const complex<X>&);
133 template<class X> complex<long double>& operator*=(const complex<X>&);
134 template<class X> complex<long double>& operator/=(const complex<X>&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000135};
136
137// 26.3.6 operators:
138template<class T> complex<T> operator+(const complex<T>&, const complex<T>&);
139template<class T> complex<T> operator+(const complex<T>&, const T&);
140template<class T> complex<T> operator+(const T&, const complex<T>&);
141template<class T> complex<T> operator-(const complex<T>&, const complex<T>&);
142template<class T> complex<T> operator-(const complex<T>&, const T&);
143template<class T> complex<T> operator-(const T&, const complex<T>&);
144template<class T> complex<T> operator*(const complex<T>&, const complex<T>&);
145template<class T> complex<T> operator*(const complex<T>&, const T&);
146template<class T> complex<T> operator*(const T&, const complex<T>&);
147template<class T> complex<T> operator/(const complex<T>&, const complex<T>&);
148template<class T> complex<T> operator/(const complex<T>&, const T&);
149template<class T> complex<T> operator/(const T&, const complex<T>&);
150template<class T> complex<T> operator+(const complex<T>&);
151template<class T> complex<T> operator-(const complex<T>&);
Marshall Clowa61e6f82013-07-31 21:02:34 +0000152template<class T> bool operator==(const complex<T>&, const complex<T>&); // constexpr in C++14
153template<class T> bool operator==(const complex<T>&, const T&); // constexpr in C++14
154template<class T> bool operator==(const T&, const complex<T>&); // constexpr in C++14
155template<class T> bool operator!=(const complex<T>&, const complex<T>&); // constexpr in C++14
156template<class T> bool operator!=(const complex<T>&, const T&); // constexpr in C++14
157template<class T> bool operator!=(const T&, const complex<T>&); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000158
159template<class T, class charT, class traits>
160 basic_istream<charT, traits>&
161 operator>>(basic_istream<charT, traits>&, complex<T>&);
162template<class T, class charT, class traits>
163 basic_ostream<charT, traits>&
164 operator<<(basic_ostream<charT, traits>&, const complex<T>&);
165
166// 26.3.7 values:
167
Marshall Clowa61e6f82013-07-31 21:02:34 +0000168template<class T> T real(const complex<T>&); // constexpr in C++14
169 long double real(long double); // constexpr in C++14
170 double real(double); // constexpr in C++14
171template<Integral T> double real(T); // constexpr in C++14
172 float real(float); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000173
Marshall Clowa61e6f82013-07-31 21:02:34 +0000174template<class T> T imag(const complex<T>&); // constexpr in C++14
175 long double imag(long double); // constexpr in C++14
176 double imag(double); // constexpr in C++14
177template<Integral T> double imag(T); // constexpr in C++14
178 float imag(float); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000179
180template<class T> T abs(const complex<T>&);
181
182template<class T> T arg(const complex<T>&);
183 long double arg(long double);
184 double arg(double);
185template<Integral T> double arg(T);
186 float arg(float);
187
188template<class T> T norm(const complex<T>&);
189 long double norm(long double);
190 double norm(double);
191template<Integral T> double norm(T);
192 float norm(float);
193
Howard Hinnant995676a2010-11-18 17:34:48 +0000194template<class T> complex<T> conj(const complex<T>&);
195 complex<long double> conj(long double);
196 complex<double> conj(double);
197template<Integral T> complex<double> conj(T);
198 complex<float> conj(float);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000199
Howard Hinnant995676a2010-11-18 17:34:48 +0000200template<class T> complex<T> proj(const complex<T>&);
201 complex<long double> proj(long double);
202 complex<double> proj(double);
203template<Integral T> complex<double> proj(T);
204 complex<float> proj(float);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000205
Marshall Clow37e4c9b2018-01-31 21:42:39 +0000206template<class T> complex<T> polar(const T&, const T& = T());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000207
208// 26.3.8 transcendentals:
209template<class T> complex<T> acos(const complex<T>&);
210template<class T> complex<T> asin(const complex<T>&);
211template<class T> complex<T> atan(const complex<T>&);
212template<class T> complex<T> acosh(const complex<T>&);
213template<class T> complex<T> asinh(const complex<T>&);
214template<class T> complex<T> atanh(const complex<T>&);
215template<class T> complex<T> cos (const complex<T>&);
216template<class T> complex<T> cosh (const complex<T>&);
217template<class T> complex<T> exp (const complex<T>&);
218template<class T> complex<T> log (const complex<T>&);
219template<class T> complex<T> log10(const complex<T>&);
220
221template<class T> complex<T> pow(const complex<T>&, const T&);
222template<class T> complex<T> pow(const complex<T>&, const complex<T>&);
223template<class T> complex<T> pow(const T&, const complex<T>&);
224
225template<class T> complex<T> sin (const complex<T>&);
226template<class T> complex<T> sinh (const complex<T>&);
227template<class T> complex<T> sqrt (const complex<T>&);
228template<class T> complex<T> tan (const complex<T>&);
229template<class T> complex<T> tanh (const complex<T>&);
230
231template<class T, class charT, class traits>
232 basic_istream<charT, traits>&
233 operator>>(basic_istream<charT, traits>& is, complex<T>& x);
234
235template<class T, class charT, class traits>
236 basic_ostream<charT, traits>&
237 operator<<(basic_ostream<charT, traits>& o, const complex<T>& x);
238
239} // std
240
241*/
242
243#include <__config>
244#include <type_traits>
245#include <stdexcept>
246#include <cmath>
247#include <sstream>
Marshall Clowe3973fd2018-09-12 19:41:40 +0000248#include <version>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000249
Howard Hinnant08e17472011-10-17 20:05:10 +0000250#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000251#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000252#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000253
254_LIBCPP_BEGIN_NAMESPACE_STD
255
Eric Fiselierc3589a82017-01-04 23:56:00 +0000256template<class _Tp> class _LIBCPP_TEMPLATE_VIS complex;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000257
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>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000262class _LIBCPP_TEMPLATE_VIS complex
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000263{
264public:
265 typedef _Tp value_type;
266private:
267 value_type __re_;
268 value_type __im_;
269public:
Marshall Clowa61e6f82013-07-31 21:02:34 +0000270 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000271 complex(const value_type& __re = value_type(), const value_type& __im = value_type())
272 : __re_(__re), __im_(__im) {}
Marshall Clowa61e6f82013-07-31 21:02:34 +0000273 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000274 complex(const complex<_Xp>& __c)
275 : __re_(__c.real()), __im_(__c.imag()) {}
276
Marshall Clowa61e6f82013-07-31 21:02:34 +0000277 _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_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000279
280 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
281 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
282
Howard Hinnantae8b16e2012-01-10 15:15:47 +0000283 _LIBCPP_INLINE_VISIBILITY complex& operator= (const value_type& __re)
284 {__re_ = __re; __im_ = value_type(); return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000285 _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 {
Marshall Clowa61e6f82013-07-31 21:02:34 +0000310 *this = *this * complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000311 return *this;
312 }
313 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
314 {
Marshall Clowa61e6f82013-07-31 21:02:34 +0000315 *this = *this / complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000316 return *this;
317 }
318};
319
Eric Fiselierc3589a82017-01-04 23:56:00 +0000320template<> class _LIBCPP_TEMPLATE_VIS complex<double>;
321template<> class _LIBCPP_TEMPLATE_VIS complex<long double>;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000322
323template<>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000324class _LIBCPP_TEMPLATE_VIS complex<float>
Howard Hinnant324bb032010-08-22 00:02:43 +0000325{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000326 float __re_;
327 float __im_;
Howard Hinnant324bb032010-08-22 00:02:43 +0000328public:
329 typedef float value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000330
Howard Hinnant410f2de2012-07-20 22:18:27 +0000331 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(float __re = 0.0f, float __im = 0.0f)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000332 : __re_(__re), __im_(__im) {}
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000333 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant410f2de2012-07-20 22:18:27 +0000334 explicit _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000335 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant410f2de2012-07-20 22:18:27 +0000336 explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000337
Howard Hinnant410f2de2012-07-20 22:18:27 +0000338 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float real() const {return __re_;}
339 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float imag() const {return __im_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000340
341 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
342 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
343
Howard Hinnantae8b16e2012-01-10 15:15:47 +0000344 _LIBCPP_INLINE_VISIBILITY complex& operator= (float __re)
345 {__re_ = __re; __im_ = value_type(); return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000346 _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 {
Marshall Clowa61e6f82013-07-31 21:02:34 +0000371 *this = *this * complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000372 return *this;
373 }
374 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
375 {
Marshall Clowa61e6f82013-07-31 21:02:34 +0000376 *this = *this / complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000377 return *this;
378 }
379};
380
381template<>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000382class _LIBCPP_TEMPLATE_VIS complex<double>
Howard Hinnant324bb032010-08-22 00:02:43 +0000383{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000384 double __re_;
385 double __im_;
Howard Hinnant324bb032010-08-22 00:02:43 +0000386public:
387 typedef double value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000388
Howard Hinnant410f2de2012-07-20 22:18:27 +0000389 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(double __re = 0.0, double __im = 0.0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000390 : __re_(__re), __im_(__im) {}
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000391 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant410f2de2012-07-20 22:18:27 +0000392 _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant410f2de2012-07-20 22:18:27 +0000394 explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000395
Howard Hinnant410f2de2012-07-20 22:18:27 +0000396 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double real() const {return __re_;}
397 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double imag() const {return __im_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000398
399 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
400 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
401
Howard Hinnantae8b16e2012-01-10 15:15:47 +0000402 _LIBCPP_INLINE_VISIBILITY complex& operator= (double __re)
403 {__re_ = __re; __im_ = value_type(); return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000404 _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 {
Marshall Clowa61e6f82013-07-31 21:02:34 +0000429 *this = *this * complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000430 return *this;
431 }
432 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
433 {
Marshall Clowa61e6f82013-07-31 21:02:34 +0000434 *this = *this / complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000435 return *this;
436 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000437};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000438
439template<>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000440class _LIBCPP_TEMPLATE_VIS complex<long double>
Howard Hinnant324bb032010-08-22 00:02:43 +0000441{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000442 long double __re_;
443 long double __im_;
Howard Hinnant324bb032010-08-22 00:02:43 +0000444public:
445 typedef long double value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000446
Howard Hinnant410f2de2012-07-20 22:18:27 +0000447 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(long double __re = 0.0L, long double __im = 0.0L)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000448 : __re_(__re), __im_(__im) {}
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000449 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant410f2de2012-07-20 22:18:27 +0000450 _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000451 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant410f2de2012-07-20 22:18:27 +0000452 _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000453
Howard Hinnant410f2de2012-07-20 22:18:27 +0000454 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double real() const {return __re_;}
455 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double imag() const {return __im_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000456
457 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
458 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
459
Howard Hinnantae8b16e2012-01-10 15:15:47 +0000460 _LIBCPP_INLINE_VISIBILITY complex& operator= (long double __re)
461 {__re_ = __re; __im_ = value_type(); return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000462 _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 {
Marshall Clowa61e6f82013-07-31 21:02:34 +0000487 *this = *this * complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000488 return *this;
489 }
490 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
491 {
Marshall Clowa61e6f82013-07-31 21:02:34 +0000492 *this = *this / complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000493 return *this;
494 }
495};
496
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000497inline
Howard Hinnant410f2de2012-07-20 22:18:27 +0000498_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000499complex<float>::complex(const complex<double>& __c)
500 : __re_(__c.real()), __im_(__c.imag()) {}
501
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000502inline
Howard Hinnant410f2de2012-07-20 22:18:27 +0000503_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000504complex<float>::complex(const complex<long double>& __c)
505 : __re_(__c.real()), __im_(__c.imag()) {}
506
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000507inline
Howard Hinnant410f2de2012-07-20 22:18:27 +0000508_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000509complex<double>::complex(const complex<float>& __c)
510 : __re_(__c.real()), __im_(__c.imag()) {}
511
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000512inline
Howard Hinnant410f2de2012-07-20 22:18:27 +0000513_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000514complex<double>::complex(const complex<long double>& __c)
515 : __re_(__c.real()), __im_(__c.imag()) {}
516
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000517inline
Howard Hinnant410f2de2012-07-20 22:18:27 +0000518_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000519complex<long double>::complex(const complex<float>& __c)
520 : __re_(__c.real()), __im_(__c.imag()) {}
521
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000522inline
Howard Hinnant410f2de2012-07-20 22:18:27 +0000523_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000524complex<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;
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +0000603 if (__libcpp_isnan_or_builtin(__x) && __libcpp_isnan_or_builtin(__y))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000604 {
605 bool __recalc = false;
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +0000606 if (__libcpp_isinf_or_builtin(__a) || __libcpp_isinf_or_builtin(__b))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000607 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +0000608 __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))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000611 __c = copysign(_Tp(0), __c);
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +0000612 if (__libcpp_isnan_or_builtin(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000613 __d = copysign(_Tp(0), __d);
614 __recalc = true;
615 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +0000616 if (__libcpp_isinf_or_builtin(__c) || __libcpp_isinf_or_builtin(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000617 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +0000618 __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))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000621 __a = copysign(_Tp(0), __a);
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +0000622 if (__libcpp_isnan_or_builtin(__b))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000623 __b = copysign(_Tp(0), __b);
624 __recalc = true;
625 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +0000626 if (!__recalc && (__libcpp_isinf_or_builtin(__ac) || __libcpp_isinf_or_builtin(__bd) ||
627 __libcpp_isinf_or_builtin(__ad) || __libcpp_isinf_or_builtin(__bc)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000628 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +0000629 if (__libcpp_isnan_or_builtin(__a))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000630 __a = copysign(_Tp(0), __a);
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +0000631 if (__libcpp_isnan_or_builtin(__b))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000632 __b = copysign(_Tp(0), __b);
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +0000633 if (__libcpp_isnan_or_builtin(__c))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000634 __c = copysign(_Tp(0), __c);
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +0000635 if (__libcpp_isnan_or_builtin(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000636 __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)));
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +0000678 if (__libcpp_isfinite_or_builtin(__logbw))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000679 {
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);
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +0000687 if (__libcpp_isnan_or_builtin(__x) && __libcpp_isnan_or_builtin(__y))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000688 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +0000689 if ((__denom == _Tp(0)) && (!__libcpp_isnan_or_builtin(__a) || !__libcpp_isnan_or_builtin(__b)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000690 {
691 __x = copysign(_Tp(INFINITY), __c) * __a;
692 __y = copysign(_Tp(INFINITY), __c) * __b;
693 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +0000694 else if ((__libcpp_isinf_or_builtin(__a) || __libcpp_isinf_or_builtin(__b)) && __libcpp_isfinite_or_builtin(__c) && __libcpp_isfinite_or_builtin(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000695 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +0000696 __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);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000698 __x = _Tp(INFINITY) * (__a * __c + __b * __d);
699 __y = _Tp(INFINITY) * (__b * __c - __a * __d);
700 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +0000701 else if (__libcpp_isinf_or_builtin(__logbw) && __logbw > _Tp(0) && __libcpp_isfinite_or_builtin(__a) && __libcpp_isfinite_or_builtin(__b))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000702 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +0000703 __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);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000705 __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>
Marshall Clowa61e6f82013-07-31 21:02:34 +0000747inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000748bool
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>
Marshall Clowa61e6f82013-07-31 21:02:34 +0000755inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000756bool
757operator==(const complex<_Tp>& __x, const _Tp& __y)
758{
759 return __x.real() == __y && __x.imag() == 0;
760}
761
762template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34 +0000763inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000764bool
765operator==(const _Tp& __x, const complex<_Tp>& __y)
766{
767 return __x == __y.real() && 0 == __y.imag();
768}
769
770template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34 +0000771inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000772bool
773operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
774{
775 return !(__x == __y);
776}
777
778template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34 +0000779inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000780bool
781operator!=(const complex<_Tp>& __x, const _Tp& __y)
782{
783 return !(__x == __y);
784}
785
786template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34 +0000787inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000788bool
789operator!=(const _Tp& __x, const complex<_Tp>& __y)
790{
791 return !(__x == __y);
792}
793
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000794// 26.3.7 values:
795
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000796template <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
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000817// real
818
819template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34 +0000820inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000821_Tp
822real(const complex<_Tp>& __c)
823{
824 return __c.real();
825}
826
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000827template <class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34 +0000828inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000829typename __libcpp_complex_overload_traits<_Tp>::_ValueType
830real(_Tp __re)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000831{
832 return __re;
833}
834
835// imag
836
837template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34 +0000838inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000839_Tp
840imag(const complex<_Tp>& __c)
841{
842 return __c.imag();
843}
844
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000845template <class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34 +0000846inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000847typename __libcpp_complex_overload_traits<_Tp>::_ValueType
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +0000848imag(_Tp)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000849{
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
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000873template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000874inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000875typename enable_if<
876 is_same<_Tp, long double>::value,
877 long double
878>::type
879arg(_Tp __re)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000880{
881 return atan2l(0.L, __re);
882}
883
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000884template<class _Tp>
885inline _LIBCPP_INLINE_VISIBILITY
886typename enable_if
887<
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000888 is_integral<_Tp>::value || is_same<_Tp, double>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000889 double
890>::type
891arg(_Tp __re)
892{
893 return atan2(0., __re);
894}
895
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000896template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000897inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000898typename enable_if<
899 is_same<_Tp, float>::value,
900 float
901>::type
902arg(_Tp __re)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000903{
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{
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +0000914 if (__libcpp_isinf_or_builtin(__c.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000915 return abs(__c.real());
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +0000916 if (__libcpp_isinf_or_builtin(__c.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000917 return abs(__c.imag());
918 return __c.real() * __c.real() + __c.imag() * __c.imag();
919}
920
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000921template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000922inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000923typename __libcpp_complex_overload_traits<_Tp>::_ValueType
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000924norm(_Tp __re)
925{
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000926 typedef typename __libcpp_complex_overload_traits<_Tp>::_ValueType _ValueType;
927 return static_cast<_ValueType>(__re) * __re;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000928}
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
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000940template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000941inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000942typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000943conj(_Tp __re)
944{
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000945 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
946 return _ComplexType(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000947}
948
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000949
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000950
951// proj
952
953template<class _Tp>
954inline _LIBCPP_INLINE_VISIBILITY
955complex<_Tp>
956proj(const complex<_Tp>& __c)
957{
958 std::complex<_Tp> __r = __c;
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +0000959 if (__libcpp_isinf_or_builtin(__c.real()) || __libcpp_isinf_or_builtin(__c.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000960 __r = complex<_Tp>(INFINITY, copysign(_Tp(0), __c.imag()));
961 return __r;
962}
963
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000964template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000965inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000966typename enable_if
967<
968 is_floating_point<_Tp>::value,
969 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
970>::type
971proj(_Tp __re)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000972{
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +0000973 if (__libcpp_isinf_or_builtin(__re))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000974 __re = abs(__re);
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000975 return complex<_Tp>(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000976}
977
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000978template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000979inline _LIBCPP_INLINE_VISIBILITY
980typename enable_if
981<
982 is_integral<_Tp>::value,
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000983 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000984>::type
985proj(_Tp __re)
986{
Eric Fiselier781fb2a2016-07-20 00:14:10 +0000987 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
988 return _ComplexType(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000989}
990
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000991// polar
992
993template<class _Tp>
994complex<_Tp>
Marshall Clow37e4c9b2018-01-31 21:42:39 +0000995polar(const _Tp& __rho, const _Tp& __theta = _Tp())
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000996{
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +0000997 if (__libcpp_isnan_or_builtin(__rho) || signbit(__rho))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000998 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +0000999 if (__libcpp_isnan_or_builtin(__theta))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001000 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001001 if (__libcpp_isinf_or_builtin(__rho))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001002 return complex<_Tp>(__rho, __theta);
1003 return complex<_Tp>(__theta, __theta);
1004 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001005 if (__libcpp_isinf_or_builtin(__theta))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001006 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001007 if (__libcpp_isinf_or_builtin(__rho))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001008 return complex<_Tp>(__rho, _Tp(NAN));
1009 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1010 }
1011 _Tp __x = __rho * cos(__theta);
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001012 if (__libcpp_isnan_or_builtin(__x))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001013 __x = 0;
1014 _Tp __y = __rho * sin(__theta);
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001015 if (__libcpp_isnan_or_builtin(__y))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001016 __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{
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001046 if (__libcpp_isinf_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001047 return complex<_Tp>(_Tp(INFINITY), __x.imag());
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001048 if (__libcpp_isinf_or_builtin(__x.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001049 {
1050 if (__x.real() > _Tp(0))
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001051 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()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001053 }
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();
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001064 if (__libcpp_isinf_or_builtin(__x.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001065 {
1066 if (__x.real() < _Tp(0))
1067 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001068 if (!__libcpp_isfinite_or_builtin(__i))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001069 __i = _Tp(1);
1070 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001071 else if (__i == 0 || !__libcpp_isfinite_or_builtin(__i))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001072 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001073 if (__libcpp_isinf_or_builtin(__i))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001074 __i = _Tp(NAN);
1075 return complex<_Tp>(__x.real(), __i);
1076 }
1077 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001078 else if (__libcpp_isnan_or_builtin(__x.real()) && __x.imag() == 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001079 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;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001100 return _VSTD::pow(result_type(__x), result_type(__y));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001101}
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;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001113 return _VSTD::pow(result_type(__x), result_type(__y));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001114}
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;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001126 return _VSTD::pow(result_type(__x), result_type(__y));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001127}
1128
Mikhail Maltsevc4658ab2018-02-19 15:41:36 +00001129// __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
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001140// asinh
1141
1142template<class _Tp>
1143complex<_Tp>
1144asinh(const complex<_Tp>& __x)
1145{
1146 const _Tp __pi(atan2(+0., -0.));
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001147 if (__libcpp_isinf_or_builtin(__x.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001148 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001149 if (__libcpp_isnan_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001150 return __x;
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001151 if (__libcpp_isinf_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001152 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1153 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1154 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001155 if (__libcpp_isnan_or_builtin(__x.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001156 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001157 if (__libcpp_isinf_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001158 return complex<_Tp>(__x.imag(), __x.real());
1159 if (__x.imag() == 0)
1160 return __x;
1161 return complex<_Tp>(__x.real(), __x.real());
1162 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001163 if (__libcpp_isinf_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001164 return complex<_Tp>(copysign(__x.imag(), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
Mikhail Maltsevc4658ab2018-02-19 15:41:36 +00001165 complex<_Tp> __z = log(__x + sqrt(__sqr(__x) + _Tp(1)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001166 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.));
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001176 if (__libcpp_isinf_or_builtin(__x.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001177 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001178 if (__libcpp_isnan_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001179 return complex<_Tp>(abs(__x.real()), __x.imag());
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001180 if (__libcpp_isinf_or_builtin(__x.imag()))
Howard Hinnant0919dba2012-11-06 21:55:44 +00001181 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001182 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()));
Howard Hinnant0919dba2012-11-06 21:55:44 +00001186 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001187 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 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001191 if (__libcpp_isnan_or_builtin(__x.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001192 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001193 if (__libcpp_isinf_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001194 return complex<_Tp>(abs(__x.imag()), __x.real());
1195 return complex<_Tp>(__x.real(), __x.real());
1196 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001197 if (__libcpp_isinf_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001198 return complex<_Tp>(abs(__x.imag()), copysign(__pi/_Tp(2), __x.imag()));
Mikhail Maltsevc4658ab2018-02-19 15:41:36 +00001199 complex<_Tp> __z = log(__x + sqrt(__sqr(__x) - _Tp(1)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001200 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.));
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001210 if (__libcpp_isinf_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001211 {
1212 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1213 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001214 if (__libcpp_isnan_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001215 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001216 if (__libcpp_isinf_or_builtin(__x.real()) || __x.real() == 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001217 return complex<_Tp>(copysign(_Tp(0), __x.real()), __x.imag());
1218 return complex<_Tp>(__x.imag(), __x.imag());
1219 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001220 if (__libcpp_isnan_or_builtin(__x.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001221 {
1222 return complex<_Tp>(__x.real(), __x.real());
1223 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001224 if (__libcpp_isinf_or_builtin(__x.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001225 {
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{
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001242 if (__libcpp_isinf_or_builtin(__x.real()) && !__libcpp_isfinite_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001243 return complex<_Tp>(__x.real(), _Tp(NAN));
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001244 if (__x.real() == 0 && !__libcpp_isfinite_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001245 return complex<_Tp>(__x.real(), _Tp(NAN));
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001246 if (__x.imag() == 0 && !__libcpp_isfinite_or_builtin(__x.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001247 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{
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001257 if (__libcpp_isinf_or_builtin(__x.real()) && !__libcpp_isfinite_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001258 return complex<_Tp>(abs(__x.real()), _Tp(NAN));
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001259 if (__x.real() == 0 && !__libcpp_isfinite_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001260 return complex<_Tp>(_Tp(NAN), __x.real());
1261 if (__x.real() == 0 && __x.imag() == 0)
1262 return complex<_Tp>(_Tp(1), __x.imag());
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001263 if (__x.imag() == 0 && !__libcpp_isfinite_or_builtin(__x.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001264 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{
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001274 if (__libcpp_isinf_or_builtin(__x.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001275 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001276 if (!__libcpp_isfinite_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001277 return complex<_Tp>(_Tp(1), _Tp(0));
1278 return complex<_Tp>(_Tp(1), copysign(_Tp(0), sin(_Tp(2) * __x.imag())));
1279 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001280 if (__libcpp_isnan_or_builtin(__x.real()) && __x.imag() == 0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001281 return __x;
1282 _Tp __2r(_Tp(2) * __x.real());
1283 _Tp __2i(_Tp(2) * __x.imag());
1284 _Tp __d(cosh(__2r) + cos(__2i));
Howard Hinnant2d3f4ee2012-09-19 23:51:47 +00001285 _Tp __2rsh(sinh(__2r));
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001286 if (__libcpp_isinf_or_builtin(__2rsh) && __libcpp_isinf_or_builtin(__d))
Howard Hinnant2d3f4ee2012-09-19 23:51:47 +00001287 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);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001290}
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.));
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001309 if (__libcpp_isinf_or_builtin(__x.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001310 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001311 if (__libcpp_isnan_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001312 return complex<_Tp>(__x.imag(), __x.real());
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001313 if (__libcpp_isinf_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001314 {
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 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001323 if (__libcpp_isnan_or_builtin(__x.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001324 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001325 if (__libcpp_isinf_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001326 return complex<_Tp>(__x.real(), -__x.imag());
1327 return complex<_Tp>(__x.real(), __x.real());
1328 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36 +00001329 if (__libcpp_isinf_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001330 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
Marshall Clowa2778112016-04-04 16:08:54 +00001331 if (__x.real() == 0 && (__x.imag() == 0 || isnan(__x.imag())))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001332 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
Mikhail Maltsevc4658ab2018-02-19 15:41:36 +00001333 complex<_Tp> __z = log(__x + sqrt(__sqr(__x) - _Tp(1)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001334 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(ios_base::failbit);
1411 }
1412 else
1413 __is.setstate(ios_base::failbit);
1414 }
1415 else if (__c == _CharT(')'))
1416 {
1417 __is.get();
1418 __x = complex<_Tp>(__r, _Tp(0));
1419 }
1420 else
1421 __is.setstate(ios_base::failbit);
1422 }
1423 else
1424 __is.setstate(ios_base::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(ios_base::failbit);
1434 }
1435 }
1436 else
1437 __is.setstate(ios_base::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
Marshall Clow320c80f2013-10-05 21:19:49 +00001453#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
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001494_LIBCPP_END_NAMESPACE_STD
1495
1496#endif // _LIBCPP_COMPLEX