blob: d20a30c01c2f97e4a4be2488df4b5074e62dcc61 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:161// -*- C++ -*-
2//===--------------------------- complex ----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:165//
Howard Hinnantb64f8b02010-11-16 22:09:026// 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:168//
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:3426 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:1629
Marshall Clowa61e6f82013-07-31 21:02:3430 T real() const; // constexpr in C++14
31 T imag() const; // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:1632
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:4352{
53public:
54 typedef float value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:1655
Howard Hinnant324bb032010-08-22 00:02:4356 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:1659
Howard Hinnant324bb032010-08-22 00:02:4360 constexpr float real() const;
Howard Hinnantbc8d3f92010-05-11 19:42:1661 void real(float);
Howard Hinnant324bb032010-08-22 00:02:4362 constexpr float imag() const;
Howard Hinnantbc8d3f92010-05-11 19:42:1663 void imag(float);
64
Howard Hinnant324bb032010-08-22 00:02:4365 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:1670
Howard Hinnant324bb032010-08-22 00:02:4371 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:1677};
78
79template<>
80class complex<double>
Howard Hinnant324bb032010-08-22 00:02:4381{
82public:
83 typedef double value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:1684
Howard Hinnant324bb032010-08-22 00:02:4385 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:1688
Howard Hinnant324bb032010-08-22 00:02:4389 constexpr double real() const;
Howard Hinnantbc8d3f92010-05-11 19:42:1690 void real(double);
Howard Hinnant324bb032010-08-22 00:02:4391 constexpr double imag() const;
Howard Hinnantbc8d3f92010-05-11 19:42:1692 void imag(double);
93
Howard Hinnant324bb032010-08-22 00:02:4394 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:16100
Howard Hinnant324bb032010-08-22 00:02:43101 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:16107
108template<>
109class complex<long double>
Howard Hinnant324bb032010-08-22 00:02:43110{
111public:
112 typedef long double value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16113
Howard Hinnant324bb032010-08-22 00:02:43114 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:16117
Howard Hinnant324bb032010-08-22 00:02:43118 constexpr long double real() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16119 void real(long double);
Howard Hinnant324bb032010-08-22 00:02:43120 constexpr long double imag() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16121 void imag(long double);
122
Howard Hinnant324bb032010-08-22 00:02:43123 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:16129
Howard Hinnant324bb032010-08-22 00:02:43130 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:16135};
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:34152template<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:16158
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:34168template<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:16173
Marshall Clowa61e6f82013-07-31 21:02:34174template<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:16179
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:48194template<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:16199
Howard Hinnant995676a2010-11-18 17:34:48200template<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:16205
206template<class T> complex<T> polar(const T&, const T& = 0);
207
208// 26.3.8 transcendentals:
209template<class T> complex<T> acos(const complex<T>&);
210template<class T> complex<T> asin(const complex<T>&);
211template<class T> complex<T> atan(const complex<T>&);
212template<class T> complex<T> acosh(const complex<T>&);
213template<class T> complex<T> asinh(const complex<T>&);
214template<class T> complex<T> atanh(const complex<T>&);
215template<class T> complex<T> cos (const complex<T>&);
216template<class T> complex<T> cosh (const complex<T>&);
217template<class T> complex<T> exp (const complex<T>&);
218template<class T> complex<T> log (const complex<T>&);
219template<class T> complex<T> log10(const complex<T>&);
220
221template<class T> complex<T> pow(const complex<T>&, const T&);
222template<class T> complex<T> pow(const complex<T>&, const complex<T>&);
223template<class T> complex<T> pow(const T&, const complex<T>&);
224
225template<class T> complex<T> sin (const complex<T>&);
226template<class T> complex<T> sinh (const complex<T>&);
227template<class T> complex<T> sqrt (const complex<T>&);
228template<class T> complex<T> tan (const complex<T>&);
229template<class T> complex<T> tanh (const complex<T>&);
230
231template<class T, class charT, class traits>
232 basic_istream<charT, traits>&
233 operator>>(basic_istream<charT, traits>& is, complex<T>& x);
234
235template<class T, class charT, class traits>
236 basic_ostream<charT, traits>&
237 operator<<(basic_ostream<charT, traits>& o, const complex<T>& x);
238
239} // std
240
241*/
242
243#include <__config>
244#include <type_traits>
245#include <stdexcept>
246#include <cmath>
247#include <sstream>
248#if defined(_LIBCPP_NO_EXCEPTIONS)
249 #include <cassert>
250#endif
251
Howard Hinnant08e17472011-10-17 20:05:10252#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16253#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10254#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16255
256_LIBCPP_BEGIN_NAMESPACE_STD
257
Howard Hinnant0f678bd2013-08-12 18:38:34258template<class _Tp> class _LIBCPP_TYPE_VIS_ONLY complex;
Howard Hinnantbc8d3f92010-05-11 19:42:16259
260template<class _Tp> complex<_Tp> operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
261template<class _Tp> complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
262
263template<class _Tp>
Howard Hinnant0f678bd2013-08-12 18:38:34264class _LIBCPP_TYPE_VIS_ONLY complex
Howard Hinnantbc8d3f92010-05-11 19:42:16265{
266public:
267 typedef _Tp value_type;
268private:
269 value_type __re_;
270 value_type __im_;
271public:
Marshall Clowa61e6f82013-07-31 21:02:34272 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16273 complex(const value_type& __re = value_type(), const value_type& __im = value_type())
274 : __re_(__re), __im_(__im) {}
Marshall Clowa61e6f82013-07-31 21:02:34275 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16276 complex(const complex<_Xp>& __c)
277 : __re_(__c.real()), __im_(__c.imag()) {}
278
Marshall Clowa61e6f82013-07-31 21:02:34279 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type real() const {return __re_;}
280 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type imag() const {return __im_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16281
282 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
283 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
284
Howard Hinnantae8b16e2012-01-10 15:15:47285 _LIBCPP_INLINE_VISIBILITY complex& operator= (const value_type& __re)
286 {__re_ = __re; __im_ = value_type(); return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16287 _LIBCPP_INLINE_VISIBILITY complex& operator+=(const value_type& __re) {__re_ += __re; return *this;}
288 _LIBCPP_INLINE_VISIBILITY complex& operator-=(const value_type& __re) {__re_ -= __re; return *this;}
289 _LIBCPP_INLINE_VISIBILITY complex& operator*=(const value_type& __re) {__re_ *= __re; __im_ *= __re; return *this;}
290 _LIBCPP_INLINE_VISIBILITY complex& operator/=(const value_type& __re) {__re_ /= __re; __im_ /= __re; return *this;}
291
292 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
293 {
294 __re_ = __c.real();
295 __im_ = __c.imag();
296 return *this;
297 }
298 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
299 {
300 __re_ += __c.real();
301 __im_ += __c.imag();
302 return *this;
303 }
304 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
305 {
306 __re_ -= __c.real();
307 __im_ -= __c.imag();
308 return *this;
309 }
310 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
311 {
Marshall Clowa61e6f82013-07-31 21:02:34312 *this = *this * complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16313 return *this;
314 }
315 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
316 {
Marshall Clowa61e6f82013-07-31 21:02:34317 *this = *this / complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16318 return *this;
319 }
320};
321
Howard Hinnant0f678bd2013-08-12 18:38:34322template<> class _LIBCPP_TYPE_VIS_ONLY complex<double>;
323template<> class _LIBCPP_TYPE_VIS_ONLY complex<long double>;
Howard Hinnantbc8d3f92010-05-11 19:42:16324
325template<>
Howard Hinnant0f678bd2013-08-12 18:38:34326class _LIBCPP_TYPE_VIS_ONLY complex<float>
Howard Hinnant324bb032010-08-22 00:02:43327{
Howard Hinnantbc8d3f92010-05-11 19:42:16328 float __re_;
329 float __im_;
Howard Hinnant324bb032010-08-22 00:02:43330public:
331 typedef float value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16332
Howard Hinnant410f2de2012-07-20 22:18:27333 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(float __re = 0.0f, float __im = 0.0f)
Howard Hinnantbc8d3f92010-05-11 19:42:16334 : __re_(__re), __im_(__im) {}
Evgeniy Stepanov9341a8a2016-04-22 01:04:55335 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant410f2de2012-07-20 22:18:27336 explicit _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
Evgeniy Stepanov9341a8a2016-04-22 01:04:55337 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant410f2de2012-07-20 22:18:27338 explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16339
Howard Hinnant410f2de2012-07-20 22:18:27340 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float real() const {return __re_;}
341 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float imag() const {return __im_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16342
343 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
344 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
345
Howard Hinnantae8b16e2012-01-10 15:15:47346 _LIBCPP_INLINE_VISIBILITY complex& operator= (float __re)
347 {__re_ = __re; __im_ = value_type(); return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16348 _LIBCPP_INLINE_VISIBILITY complex& operator+=(float __re) {__re_ += __re; return *this;}
349 _LIBCPP_INLINE_VISIBILITY complex& operator-=(float __re) {__re_ -= __re; return *this;}
350 _LIBCPP_INLINE_VISIBILITY complex& operator*=(float __re) {__re_ *= __re; __im_ *= __re; return *this;}
351 _LIBCPP_INLINE_VISIBILITY complex& operator/=(float __re) {__re_ /= __re; __im_ /= __re; return *this;}
352
353 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
354 {
355 __re_ = __c.real();
356 __im_ = __c.imag();
357 return *this;
358 }
359 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
360 {
361 __re_ += __c.real();
362 __im_ += __c.imag();
363 return *this;
364 }
365 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
366 {
367 __re_ -= __c.real();
368 __im_ -= __c.imag();
369 return *this;
370 }
371 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
372 {
Marshall Clowa61e6f82013-07-31 21:02:34373 *this = *this * complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16374 return *this;
375 }
376 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
377 {
Marshall Clowa61e6f82013-07-31 21:02:34378 *this = *this / complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16379 return *this;
380 }
381};
382
383template<>
Howard Hinnant0f678bd2013-08-12 18:38:34384class _LIBCPP_TYPE_VIS_ONLY complex<double>
Howard Hinnant324bb032010-08-22 00:02:43385{
Howard Hinnantbc8d3f92010-05-11 19:42:16386 double __re_;
387 double __im_;
Howard Hinnant324bb032010-08-22 00:02:43388public:
389 typedef double value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16390
Howard Hinnant410f2de2012-07-20 22:18:27391 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(double __re = 0.0, double __im = 0.0)
Howard Hinnantbc8d3f92010-05-11 19:42:16392 : __re_(__re), __im_(__im) {}
Evgeniy Stepanov9341a8a2016-04-22 01:04:55393 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant410f2de2012-07-20 22:18:27394 _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
Evgeniy Stepanov9341a8a2016-04-22 01:04:55395 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant410f2de2012-07-20 22:18:27396 explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16397
Howard Hinnant410f2de2012-07-20 22:18:27398 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double real() const {return __re_;}
399 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double imag() const {return __im_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16400
401 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
402 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
403
Howard Hinnantae8b16e2012-01-10 15:15:47404 _LIBCPP_INLINE_VISIBILITY complex& operator= (double __re)
405 {__re_ = __re; __im_ = value_type(); return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16406 _LIBCPP_INLINE_VISIBILITY complex& operator+=(double __re) {__re_ += __re; return *this;}
407 _LIBCPP_INLINE_VISIBILITY complex& operator-=(double __re) {__re_ -= __re; return *this;}
408 _LIBCPP_INLINE_VISIBILITY complex& operator*=(double __re) {__re_ *= __re; __im_ *= __re; return *this;}
409 _LIBCPP_INLINE_VISIBILITY complex& operator/=(double __re) {__re_ /= __re; __im_ /= __re; return *this;}
410
411 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
412 {
413 __re_ = __c.real();
414 __im_ = __c.imag();
415 return *this;
416 }
417 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
418 {
419 __re_ += __c.real();
420 __im_ += __c.imag();
421 return *this;
422 }
423 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
424 {
425 __re_ -= __c.real();
426 __im_ -= __c.imag();
427 return *this;
428 }
429 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
430 {
Marshall Clowa61e6f82013-07-31 21:02:34431 *this = *this * complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16432 return *this;
433 }
434 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
435 {
Marshall Clowa61e6f82013-07-31 21:02:34436 *this = *this / complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16437 return *this;
438 }
Howard Hinnant324bb032010-08-22 00:02:43439};
Howard Hinnantbc8d3f92010-05-11 19:42:16440
441template<>
Howard Hinnant0f678bd2013-08-12 18:38:34442class _LIBCPP_TYPE_VIS_ONLY complex<long double>
Howard Hinnant324bb032010-08-22 00:02:43443{
Howard Hinnantbc8d3f92010-05-11 19:42:16444 long double __re_;
445 long double __im_;
Howard Hinnant324bb032010-08-22 00:02:43446public:
447 typedef long double value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16448
Howard Hinnant410f2de2012-07-20 22:18:27449 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(long double __re = 0.0L, long double __im = 0.0L)
Howard Hinnantbc8d3f92010-05-11 19:42:16450 : __re_(__re), __im_(__im) {}
Evgeniy Stepanov9341a8a2016-04-22 01:04:55451 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant410f2de2012-07-20 22:18:27452 _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
Evgeniy Stepanov9341a8a2016-04-22 01:04:55453 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant410f2de2012-07-20 22:18:27454 _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16455
Howard Hinnant410f2de2012-07-20 22:18:27456 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double real() const {return __re_;}
457 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double imag() const {return __im_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16458
459 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
460 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
461
Howard Hinnantae8b16e2012-01-10 15:15:47462 _LIBCPP_INLINE_VISIBILITY complex& operator= (long double __re)
463 {__re_ = __re; __im_ = value_type(); return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16464 _LIBCPP_INLINE_VISIBILITY complex& operator+=(long double __re) {__re_ += __re; return *this;}
465 _LIBCPP_INLINE_VISIBILITY complex& operator-=(long double __re) {__re_ -= __re; return *this;}
466 _LIBCPP_INLINE_VISIBILITY complex& operator*=(long double __re) {__re_ *= __re; __im_ *= __re; return *this;}
467 _LIBCPP_INLINE_VISIBILITY complex& operator/=(long double __re) {__re_ /= __re; __im_ /= __re; return *this;}
468
469 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
470 {
471 __re_ = __c.real();
472 __im_ = __c.imag();
473 return *this;
474 }
475 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
476 {
477 __re_ += __c.real();
478 __im_ += __c.imag();
479 return *this;
480 }
481 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
482 {
483 __re_ -= __c.real();
484 __im_ -= __c.imag();
485 return *this;
486 }
487 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
488 {
Marshall Clowa61e6f82013-07-31 21:02:34489 *this = *this * complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16490 return *this;
491 }
492 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
493 {
Marshall Clowa61e6f82013-07-31 21:02:34494 *this = *this / complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16495 return *this;
496 }
497};
498
Evgeniy Stepanov9341a8a2016-04-22 01:04:55499inline
Howard Hinnant410f2de2012-07-20 22:18:27500_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16501complex<float>::complex(const complex<double>& __c)
502 : __re_(__c.real()), __im_(__c.imag()) {}
503
Evgeniy Stepanov9341a8a2016-04-22 01:04:55504inline
Howard Hinnant410f2de2012-07-20 22:18:27505_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16506complex<float>::complex(const complex<long double>& __c)
507 : __re_(__c.real()), __im_(__c.imag()) {}
508
Evgeniy Stepanov9341a8a2016-04-22 01:04:55509inline
Howard Hinnant410f2de2012-07-20 22:18:27510_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16511complex<double>::complex(const complex<float>& __c)
512 : __re_(__c.real()), __im_(__c.imag()) {}
513
Evgeniy Stepanov9341a8a2016-04-22 01:04:55514inline
Howard Hinnant410f2de2012-07-20 22:18:27515_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16516complex<double>::complex(const complex<long double>& __c)
517 : __re_(__c.real()), __im_(__c.imag()) {}
518
Evgeniy Stepanov9341a8a2016-04-22 01:04:55519inline
Howard Hinnant410f2de2012-07-20 22:18:27520_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16521complex<long double>::complex(const complex<float>& __c)
522 : __re_(__c.real()), __im_(__c.imag()) {}
523
Evgeniy Stepanov9341a8a2016-04-22 01:04:55524inline
Howard Hinnant410f2de2012-07-20 22:18:27525_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16526complex<long double>::complex(const complex<double>& __c)
527 : __re_(__c.real()), __im_(__c.imag()) {}
528
529// 26.3.6 operators:
530
531template<class _Tp>
532inline _LIBCPP_INLINE_VISIBILITY
533complex<_Tp>
534operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
535{
536 complex<_Tp> __t(__x);
537 __t += __y;
538 return __t;
539}
540
541template<class _Tp>
542inline _LIBCPP_INLINE_VISIBILITY
543complex<_Tp>
544operator+(const complex<_Tp>& __x, const _Tp& __y)
545{
546 complex<_Tp> __t(__x);
547 __t += __y;
548 return __t;
549}
550
551template<class _Tp>
552inline _LIBCPP_INLINE_VISIBILITY
553complex<_Tp>
554operator+(const _Tp& __x, const complex<_Tp>& __y)
555{
556 complex<_Tp> __t(__y);
557 __t += __x;
558 return __t;
559}
560
561template<class _Tp>
562inline _LIBCPP_INLINE_VISIBILITY
563complex<_Tp>
564operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
565{
566 complex<_Tp> __t(__x);
567 __t -= __y;
568 return __t;
569}
570
571template<class _Tp>
572inline _LIBCPP_INLINE_VISIBILITY
573complex<_Tp>
574operator-(const complex<_Tp>& __x, const _Tp& __y)
575{
576 complex<_Tp> __t(__x);
577 __t -= __y;
578 return __t;
579}
580
581template<class _Tp>
582inline _LIBCPP_INLINE_VISIBILITY
583complex<_Tp>
584operator-(const _Tp& __x, const complex<_Tp>& __y)
585{
586 complex<_Tp> __t(-__y);
587 __t += __x;
588 return __t;
589}
590
591template<class _Tp>
592complex<_Tp>
593operator*(const complex<_Tp>& __z, const complex<_Tp>& __w)
594{
595 _Tp __a = __z.real();
596 _Tp __b = __z.imag();
597 _Tp __c = __w.real();
598 _Tp __d = __w.imag();
599 _Tp __ac = __a * __c;
600 _Tp __bd = __b * __d;
601 _Tp __ad = __a * __d;
602 _Tp __bc = __b * __c;
603 _Tp __x = __ac - __bd;
604 _Tp __y = __ad + __bc;
605 if (isnan(__x) && isnan(__y))
606 {
607 bool __recalc = false;
608 if (isinf(__a) || isinf(__b))
609 {
610 __a = copysign(isinf(__a) ? _Tp(1) : _Tp(0), __a);
611 __b = copysign(isinf(__b) ? _Tp(1) : _Tp(0), __b);
612 if (isnan(__c))
613 __c = copysign(_Tp(0), __c);
614 if (isnan(__d))
615 __d = copysign(_Tp(0), __d);
616 __recalc = true;
617 }
618 if (isinf(__c) || isinf(__d))
619 {
620 __c = copysign(isinf(__c) ? _Tp(1) : _Tp(0), __c);
621 __d = copysign(isinf(__d) ? _Tp(1) : _Tp(0), __d);
622 if (isnan(__a))
623 __a = copysign(_Tp(0), __a);
624 if (isnan(__b))
625 __b = copysign(_Tp(0), __b);
626 __recalc = true;
627 }
628 if (!__recalc && (isinf(__ac) || isinf(__bd) ||
629 isinf(__ad) || isinf(__bc)))
630 {
631 if (isnan(__a))
632 __a = copysign(_Tp(0), __a);
633 if (isnan(__b))
634 __b = copysign(_Tp(0), __b);
635 if (isnan(__c))
636 __c = copysign(_Tp(0), __c);
637 if (isnan(__d))
638 __d = copysign(_Tp(0), __d);
639 __recalc = true;
640 }
641 if (__recalc)
642 {
643 __x = _Tp(INFINITY) * (__a * __c - __b * __d);
644 __y = _Tp(INFINITY) * (__a * __d + __b * __c);
645 }
646 }
647 return complex<_Tp>(__x, __y);
648}
649
650template<class _Tp>
651inline _LIBCPP_INLINE_VISIBILITY
652complex<_Tp>
653operator*(const complex<_Tp>& __x, const _Tp& __y)
654{
655 complex<_Tp> __t(__x);
656 __t *= __y;
657 return __t;
658}
659
660template<class _Tp>
661inline _LIBCPP_INLINE_VISIBILITY
662complex<_Tp>
663operator*(const _Tp& __x, const complex<_Tp>& __y)
664{
665 complex<_Tp> __t(__y);
666 __t *= __x;
667 return __t;
668}
669
670template<class _Tp>
671complex<_Tp>
672operator/(const complex<_Tp>& __z, const complex<_Tp>& __w)
673{
674 int __ilogbw = 0;
675 _Tp __a = __z.real();
676 _Tp __b = __z.imag();
677 _Tp __c = __w.real();
678 _Tp __d = __w.imag();
679 _Tp __logbw = logb(fmax(fabs(__c), fabs(__d)));
680 if (isfinite(__logbw))
681 {
682 __ilogbw = static_cast<int>(__logbw);
683 __c = scalbn(__c, -__ilogbw);
684 __d = scalbn(__d, -__ilogbw);
685 }
686 _Tp __denom = __c * __c + __d * __d;
687 _Tp __x = scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
688 _Tp __y = scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
689 if (isnan(__x) && isnan(__y))
690 {
691 if ((__denom == _Tp(0)) && (!isnan(__a) || !isnan(__b)))
692 {
693 __x = copysign(_Tp(INFINITY), __c) * __a;
694 __y = copysign(_Tp(INFINITY), __c) * __b;
695 }
696 else if ((isinf(__a) || isinf(__b)) && isfinite(__c) && isfinite(__d))
697 {
698 __a = copysign(isinf(__a) ? _Tp(1) : _Tp(0), __a);
699 __b = copysign(isinf(__b) ? _Tp(1) : _Tp(0), __b);
700 __x = _Tp(INFINITY) * (__a * __c + __b * __d);
701 __y = _Tp(INFINITY) * (__b * __c - __a * __d);
702 }
703 else if (isinf(__logbw) && __logbw > _Tp(0) && isfinite(__a) && isfinite(__b))
704 {
705 __c = copysign(isinf(__c) ? _Tp(1) : _Tp(0), __c);
706 __d = copysign(isinf(__d) ? _Tp(1) : _Tp(0), __d);
707 __x = _Tp(0) * (__a * __c + __b * __d);
708 __y = _Tp(0) * (__b * __c - __a * __d);
709 }
710 }
711 return complex<_Tp>(__x, __y);
712}
713
714template<class _Tp>
715inline _LIBCPP_INLINE_VISIBILITY
716complex<_Tp>
717operator/(const complex<_Tp>& __x, const _Tp& __y)
718{
719 return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
720}
721
722template<class _Tp>
723inline _LIBCPP_INLINE_VISIBILITY
724complex<_Tp>
725operator/(const _Tp& __x, const complex<_Tp>& __y)
726{
727 complex<_Tp> __t(__x);
728 __t /= __y;
729 return __t;
730}
731
732template<class _Tp>
733inline _LIBCPP_INLINE_VISIBILITY
734complex<_Tp>
735operator+(const complex<_Tp>& __x)
736{
737 return __x;
738}
739
740template<class _Tp>
741inline _LIBCPP_INLINE_VISIBILITY
742complex<_Tp>
743operator-(const complex<_Tp>& __x)
744{
745 return complex<_Tp>(-__x.real(), -__x.imag());
746}
747
748template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34749inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16750bool
751operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
752{
753 return __x.real() == __y.real() && __x.imag() == __y.imag();
754}
755
756template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34757inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16758bool
759operator==(const complex<_Tp>& __x, const _Tp& __y)
760{
761 return __x.real() == __y && __x.imag() == 0;
762}
763
764template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34765inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16766bool
767operator==(const _Tp& __x, const complex<_Tp>& __y)
768{
769 return __x == __y.real() && 0 == __y.imag();
770}
771
772template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34773inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16774bool
775operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
776{
777 return !(__x == __y);
778}
779
780template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34781inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16782bool
783operator!=(const complex<_Tp>& __x, const _Tp& __y)
784{
785 return !(__x == __y);
786}
787
788template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34789inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16790bool
791operator!=(const _Tp& __x, const complex<_Tp>& __y)
792{
793 return !(__x == __y);
794}
795
Howard Hinnantbc8d3f92010-05-11 19:42:16796// 26.3.7 values:
797
Eric Fiselier781fb2a2016-07-20 00:14:10798template <class _Tp, bool = is_integral<_Tp>::value,
799 bool = is_floating_point<_Tp>::value
800 >
801struct __libcpp_complex_overload_traits {};
802
803// Integral Types
804template <class _Tp>
805struct __libcpp_complex_overload_traits<_Tp, true, false>
806{
807 typedef double _ValueType;
808 typedef complex<double> _ComplexType;
809};
810
811// Floating point types
812template <class _Tp>
813struct __libcpp_complex_overload_traits<_Tp, false, true>
814{
815 typedef _Tp _ValueType;
816 typedef complex<_Tp> _ComplexType;
817};
818
Howard Hinnantbc8d3f92010-05-11 19:42:16819// real
820
821template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34822inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16823_Tp
824real(const complex<_Tp>& __c)
825{
826 return __c.real();
827}
828
Eric Fiselier781fb2a2016-07-20 00:14:10829template <class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34830inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Eric Fiselier781fb2a2016-07-20 00:14:10831typename __libcpp_complex_overload_traits<_Tp>::_ValueType
832real(_Tp __re)
Howard Hinnantbc8d3f92010-05-11 19:42:16833{
834 return __re;
835}
836
837// imag
838
839template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34840inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16841_Tp
842imag(const complex<_Tp>& __c)
843{
844 return __c.imag();
845}
846
Eric Fiselier781fb2a2016-07-20 00:14:10847template <class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34848inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Eric Fiselier781fb2a2016-07-20 00:14:10849typename __libcpp_complex_overload_traits<_Tp>::_ValueType
850imag(_Tp __re)
Howard Hinnantbc8d3f92010-05-11 19:42:16851{
852 return 0;
853}
854
855// abs
856
857template<class _Tp>
858inline _LIBCPP_INLINE_VISIBILITY
859_Tp
860abs(const complex<_Tp>& __c)
861{
862 return hypot(__c.real(), __c.imag());
863}
864
865// arg
866
867template<class _Tp>
868inline _LIBCPP_INLINE_VISIBILITY
869_Tp
870arg(const complex<_Tp>& __c)
871{
872 return atan2(__c.imag(), __c.real());
873}
874
Eric Fiselier781fb2a2016-07-20 00:14:10875template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16876inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier781fb2a2016-07-20 00:14:10877typename enable_if<
878 is_same<_Tp, long double>::value,
879 long double
880>::type
881arg(_Tp __re)
Howard Hinnantbc8d3f92010-05-11 19:42:16882{
883 return atan2l(0.L, __re);
884}
885
Howard Hinnantbc8d3f92010-05-11 19:42:16886template<class _Tp>
887inline _LIBCPP_INLINE_VISIBILITY
888typename enable_if
889<
Eric Fiselier781fb2a2016-07-20 00:14:10890 is_integral<_Tp>::value || is_same<_Tp, double>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16891 double
892>::type
893arg(_Tp __re)
894{
895 return atan2(0., __re);
896}
897
Eric Fiselier781fb2a2016-07-20 00:14:10898template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16899inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier781fb2a2016-07-20 00:14:10900typename enable_if<
901 is_same<_Tp, float>::value,
902 float
903>::type
904arg(_Tp __re)
Howard Hinnantbc8d3f92010-05-11 19:42:16905{
906 return atan2f(0.F, __re);
907}
908
909// norm
910
911template<class _Tp>
912inline _LIBCPP_INLINE_VISIBILITY
913_Tp
914norm(const complex<_Tp>& __c)
915{
916 if (isinf(__c.real()))
917 return abs(__c.real());
918 if (isinf(__c.imag()))
919 return abs(__c.imag());
920 return __c.real() * __c.real() + __c.imag() * __c.imag();
921}
922
Eric Fiselier781fb2a2016-07-20 00:14:10923template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16924inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier781fb2a2016-07-20 00:14:10925typename __libcpp_complex_overload_traits<_Tp>::_ValueType
Howard Hinnantbc8d3f92010-05-11 19:42:16926norm(_Tp __re)
927{
Eric Fiselier781fb2a2016-07-20 00:14:10928 typedef typename __libcpp_complex_overload_traits<_Tp>::_ValueType _ValueType;
929 return static_cast<_ValueType>(__re) * __re;
Howard Hinnantbc8d3f92010-05-11 19:42:16930}
931
932// conj
933
934template<class _Tp>
935inline _LIBCPP_INLINE_VISIBILITY
936complex<_Tp>
937conj(const complex<_Tp>& __c)
938{
939 return complex<_Tp>(__c.real(), -__c.imag());
940}
941
Eric Fiselier781fb2a2016-07-20 00:14:10942template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16943inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier781fb2a2016-07-20 00:14:10944typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
Howard Hinnantbc8d3f92010-05-11 19:42:16945conj(_Tp __re)
946{
Eric Fiselier781fb2a2016-07-20 00:14:10947 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
948 return _ComplexType(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16949}
950
Eric Fiselier781fb2a2016-07-20 00:14:10951
Howard Hinnantbc8d3f92010-05-11 19:42:16952
953// proj
954
955template<class _Tp>
956inline _LIBCPP_INLINE_VISIBILITY
957complex<_Tp>
958proj(const complex<_Tp>& __c)
959{
960 std::complex<_Tp> __r = __c;
961 if (isinf(__c.real()) || isinf(__c.imag()))
962 __r = complex<_Tp>(INFINITY, copysign(_Tp(0), __c.imag()));
963 return __r;
964}
965
Eric Fiselier781fb2a2016-07-20 00:14:10966template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16967inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier781fb2a2016-07-20 00:14:10968typename enable_if
969<
970 is_floating_point<_Tp>::value,
971 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
972>::type
973proj(_Tp __re)
Howard Hinnantbc8d3f92010-05-11 19:42:16974{
975 if (isinf(__re))
976 __re = abs(__re);
Eric Fiselier781fb2a2016-07-20 00:14:10977 return complex<_Tp>(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16978}
979
Eric Fiselier781fb2a2016-07-20 00:14:10980template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16981inline _LIBCPP_INLINE_VISIBILITY
982typename enable_if
983<
984 is_integral<_Tp>::value,
Eric Fiselier781fb2a2016-07-20 00:14:10985 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
Howard Hinnantbc8d3f92010-05-11 19:42:16986>::type
987proj(_Tp __re)
988{
Eric Fiselier781fb2a2016-07-20 00:14:10989 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
990 return _ComplexType(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16991}
992
Howard Hinnantbc8d3f92010-05-11 19:42:16993
994// polar
995
996template<class _Tp>
997complex<_Tp>
998polar(const _Tp& __rho, const _Tp& __theta = _Tp(0))
999{
1000 if (isnan(__rho) || signbit(__rho))
1001 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1002 if (isnan(__theta))
1003 {
1004 if (isinf(__rho))
1005 return complex<_Tp>(__rho, __theta);
1006 return complex<_Tp>(__theta, __theta);
1007 }
1008 if (isinf(__theta))
1009 {
1010 if (isinf(__rho))
1011 return complex<_Tp>(__rho, _Tp(NAN));
1012 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1013 }
1014 _Tp __x = __rho * cos(__theta);
1015 if (isnan(__x))
1016 __x = 0;
1017 _Tp __y = __rho * sin(__theta);
1018 if (isnan(__y))
1019 __y = 0;
1020 return complex<_Tp>(__x, __y);
1021}
1022
1023// log
1024
1025template<class _Tp>
1026inline _LIBCPP_INLINE_VISIBILITY
1027complex<_Tp>
1028log(const complex<_Tp>& __x)
1029{
1030 return complex<_Tp>(log(abs(__x)), arg(__x));
1031}
1032
1033// log10
1034
1035template<class _Tp>
1036inline _LIBCPP_INLINE_VISIBILITY
1037complex<_Tp>
1038log10(const complex<_Tp>& __x)
1039{
1040 return log(__x) / log(_Tp(10));
1041}
1042
1043// sqrt
1044
1045template<class _Tp>
1046complex<_Tp>
1047sqrt(const complex<_Tp>& __x)
1048{
1049 if (isinf(__x.imag()))
1050 return complex<_Tp>(_Tp(INFINITY), __x.imag());
1051 if (isinf(__x.real()))
1052 {
1053 if (__x.real() > _Tp(0))
1054 return complex<_Tp>(__x.real(), isnan(__x.imag()) ? __x.imag() : copysign(_Tp(0), __x.imag()));
1055 return complex<_Tp>(isnan(__x.imag()) ? __x.imag() : _Tp(0), copysign(__x.real(), __x.imag()));
1056 }
1057 return polar(sqrt(abs(__x)), arg(__x) / _Tp(2));
1058}
1059
1060// exp
1061
1062template<class _Tp>
1063complex<_Tp>
1064exp(const complex<_Tp>& __x)
1065{
1066 _Tp __i = __x.imag();
1067 if (isinf(__x.real()))
1068 {
1069 if (__x.real() < _Tp(0))
1070 {
1071 if (!isfinite(__i))
1072 __i = _Tp(1);
1073 }
1074 else if (__i == 0 || !isfinite(__i))
1075 {
1076 if (isinf(__i))
1077 __i = _Tp(NAN);
1078 return complex<_Tp>(__x.real(), __i);
1079 }
1080 }
1081 else if (isnan(__x.real()) && __x.imag() == 0)
1082 return __x;
1083 _Tp __e = exp(__x.real());
1084 return complex<_Tp>(__e * cos(__i), __e * sin(__i));
1085}
1086
1087// pow
1088
1089template<class _Tp>
1090inline _LIBCPP_INLINE_VISIBILITY
1091complex<_Tp>
1092pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1093{
1094 return exp(__y * log(__x));
1095}
1096
1097template<class _Tp, class _Up>
1098inline _LIBCPP_INLINE_VISIBILITY
1099complex<typename __promote<_Tp, _Up>::type>
1100pow(const complex<_Tp>& __x, const complex<_Up>& __y)
1101{
1102 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
Howard Hinnant0949eed2011-06-30 21:18:191103 return _VSTD::pow(result_type(__x), result_type(__y));
Howard Hinnantbc8d3f92010-05-11 19:42:161104}
1105
1106template<class _Tp, class _Up>
1107inline _LIBCPP_INLINE_VISIBILITY
1108typename enable_if
1109<
1110 is_arithmetic<_Up>::value,
1111 complex<typename __promote<_Tp, _Up>::type>
1112>::type
1113pow(const complex<_Tp>& __x, const _Up& __y)
1114{
1115 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
Howard Hinnant0949eed2011-06-30 21:18:191116 return _VSTD::pow(result_type(__x), result_type(__y));
Howard Hinnantbc8d3f92010-05-11 19:42:161117}
1118
1119template<class _Tp, class _Up>
1120inline _LIBCPP_INLINE_VISIBILITY
1121typename enable_if
1122<
1123 is_arithmetic<_Tp>::value,
1124 complex<typename __promote<_Tp, _Up>::type>
1125>::type
1126pow(const _Tp& __x, const complex<_Up>& __y)
1127{
1128 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
Howard Hinnant0949eed2011-06-30 21:18:191129 return _VSTD::pow(result_type(__x), result_type(__y));
Howard Hinnantbc8d3f92010-05-11 19:42:161130}
1131
1132// asinh
1133
1134template<class _Tp>
1135complex<_Tp>
1136asinh(const complex<_Tp>& __x)
1137{
1138 const _Tp __pi(atan2(+0., -0.));
1139 if (isinf(__x.real()))
1140 {
1141 if (isnan(__x.imag()))
1142 return __x;
1143 if (isinf(__x.imag()))
1144 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1145 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1146 }
1147 if (isnan(__x.real()))
1148 {
1149 if (isinf(__x.imag()))
1150 return complex<_Tp>(__x.imag(), __x.real());
1151 if (__x.imag() == 0)
1152 return __x;
1153 return complex<_Tp>(__x.real(), __x.real());
1154 }
1155 if (isinf(__x.imag()))
1156 return complex<_Tp>(copysign(__x.imag(), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1157 complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) + _Tp(1)));
1158 return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1159}
1160
1161// acosh
1162
1163template<class _Tp>
1164complex<_Tp>
1165acosh(const complex<_Tp>& __x)
1166{
1167 const _Tp __pi(atan2(+0., -0.));
1168 if (isinf(__x.real()))
1169 {
1170 if (isnan(__x.imag()))
1171 return complex<_Tp>(abs(__x.real()), __x.imag());
1172 if (isinf(__x.imag()))
Howard Hinnant0919dba2012-11-06 21:55:441173 {
Howard Hinnantbc8d3f92010-05-11 19:42:161174 if (__x.real() > 0)
1175 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1176 else
1177 return complex<_Tp>(-__x.real(), copysign(__pi * _Tp(0.75), __x.imag()));
Howard Hinnant0919dba2012-11-06 21:55:441178 }
Howard Hinnantbc8d3f92010-05-11 19:42:161179 if (__x.real() < 0)
1180 return complex<_Tp>(-__x.real(), copysign(__pi, __x.imag()));
1181 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1182 }
1183 if (isnan(__x.real()))
1184 {
1185 if (isinf(__x.imag()))
1186 return complex<_Tp>(abs(__x.imag()), __x.real());
1187 return complex<_Tp>(__x.real(), __x.real());
1188 }
1189 if (isinf(__x.imag()))
1190 return complex<_Tp>(abs(__x.imag()), copysign(__pi/_Tp(2), __x.imag()));
1191 complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) - _Tp(1)));
1192 return complex<_Tp>(copysign(__z.real(), _Tp(0)), copysign(__z.imag(), __x.imag()));
1193}
1194
1195// atanh
1196
1197template<class _Tp>
1198complex<_Tp>
1199atanh(const complex<_Tp>& __x)
1200{
1201 const _Tp __pi(atan2(+0., -0.));
1202 if (isinf(__x.imag()))
1203 {
1204 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1205 }
1206 if (isnan(__x.imag()))
1207 {
1208 if (isinf(__x.real()) || __x.real() == 0)
1209 return complex<_Tp>(copysign(_Tp(0), __x.real()), __x.imag());
1210 return complex<_Tp>(__x.imag(), __x.imag());
1211 }
1212 if (isnan(__x.real()))
1213 {
1214 return complex<_Tp>(__x.real(), __x.real());
1215 }
1216 if (isinf(__x.real()))
1217 {
1218 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1219 }
1220 if (abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0))
1221 {
1222 return complex<_Tp>(copysign(_Tp(INFINITY), __x.real()), copysign(_Tp(0), __x.imag()));
1223 }
1224 complex<_Tp> __z = log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2);
1225 return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1226}
1227
1228// sinh
1229
1230template<class _Tp>
1231complex<_Tp>
1232sinh(const complex<_Tp>& __x)
1233{
1234 if (isinf(__x.real()) && !isfinite(__x.imag()))
1235 return complex<_Tp>(__x.real(), _Tp(NAN));
1236 if (__x.real() == 0 && !isfinite(__x.imag()))
1237 return complex<_Tp>(__x.real(), _Tp(NAN));
1238 if (__x.imag() == 0 && !isfinite(__x.real()))
1239 return __x;
1240 return complex<_Tp>(sinh(__x.real()) * cos(__x.imag()), cosh(__x.real()) * sin(__x.imag()));
1241}
1242
1243// cosh
1244
1245template<class _Tp>
1246complex<_Tp>
1247cosh(const complex<_Tp>& __x)
1248{
1249 if (isinf(__x.real()) && !isfinite(__x.imag()))
1250 return complex<_Tp>(abs(__x.real()), _Tp(NAN));
1251 if (__x.real() == 0 && !isfinite(__x.imag()))
1252 return complex<_Tp>(_Tp(NAN), __x.real());
1253 if (__x.real() == 0 && __x.imag() == 0)
1254 return complex<_Tp>(_Tp(1), __x.imag());
1255 if (__x.imag() == 0 && !isfinite(__x.real()))
1256 return complex<_Tp>(abs(__x.real()), __x.imag());
1257 return complex<_Tp>(cosh(__x.real()) * cos(__x.imag()), sinh(__x.real()) * sin(__x.imag()));
1258}
1259
1260// tanh
1261
1262template<class _Tp>
1263complex<_Tp>
1264tanh(const complex<_Tp>& __x)
1265{
1266 if (isinf(__x.real()))
1267 {
1268 if (!isfinite(__x.imag()))
1269 return complex<_Tp>(_Tp(1), _Tp(0));
1270 return complex<_Tp>(_Tp(1), copysign(_Tp(0), sin(_Tp(2) * __x.imag())));
1271 }
1272 if (isnan(__x.real()) && __x.imag() == 0)
1273 return __x;
1274 _Tp __2r(_Tp(2) * __x.real());
1275 _Tp __2i(_Tp(2) * __x.imag());
1276 _Tp __d(cosh(__2r) + cos(__2i));
Howard Hinnant2d3f4ee2012-09-19 23:51:471277 _Tp __2rsh(sinh(__2r));
1278 if (isinf(__2rsh) && isinf(__d))
1279 return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1),
1280 __2i > _Tp(0) ? _Tp(0) : _Tp(-0.));
1281 return complex<_Tp>(__2rsh/__d, sin(__2i)/__d);
Howard Hinnantbc8d3f92010-05-11 19:42:161282}
1283
1284// asin
1285
1286template<class _Tp>
1287complex<_Tp>
1288asin(const complex<_Tp>& __x)
1289{
1290 complex<_Tp> __z = asinh(complex<_Tp>(-__x.imag(), __x.real()));
1291 return complex<_Tp>(__z.imag(), -__z.real());
1292}
1293
1294// acos
1295
1296template<class _Tp>
1297complex<_Tp>
1298acos(const complex<_Tp>& __x)
1299{
1300 const _Tp __pi(atan2(+0., -0.));
1301 if (isinf(__x.real()))
1302 {
1303 if (isnan(__x.imag()))
1304 return complex<_Tp>(__x.imag(), __x.real());
1305 if (isinf(__x.imag()))
1306 {
1307 if (__x.real() < _Tp(0))
1308 return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());
1309 return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag());
1310 }
1311 if (__x.real() < _Tp(0))
1312 return complex<_Tp>(__pi, signbit(__x.imag()) ? -__x.real() : __x.real());
1313 return complex<_Tp>(_Tp(0), signbit(__x.imag()) ? __x.real() : -__x.real());
1314 }
1315 if (isnan(__x.real()))
1316 {
1317 if (isinf(__x.imag()))
1318 return complex<_Tp>(__x.real(), -__x.imag());
1319 return complex<_Tp>(__x.real(), __x.real());
1320 }
1321 if (isinf(__x.imag()))
1322 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
Marshall Clowa2778112016-04-04 16:08:541323 if (__x.real() == 0 && (__x.imag() == 0 || isnan(__x.imag())))
Howard Hinnantbc8d3f92010-05-11 19:42:161324 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
1325 complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) - _Tp(1)));
1326 if (signbit(__x.imag()))
1327 return complex<_Tp>(abs(__z.imag()), abs(__z.real()));
1328 return complex<_Tp>(abs(__z.imag()), -abs(__z.real()));
1329}
1330
1331// atan
1332
1333template<class _Tp>
1334complex<_Tp>
1335atan(const complex<_Tp>& __x)
1336{
1337 complex<_Tp> __z = atanh(complex<_Tp>(-__x.imag(), __x.real()));
1338 return complex<_Tp>(__z.imag(), -__z.real());
1339}
1340
1341// sin
1342
1343template<class _Tp>
1344complex<_Tp>
1345sin(const complex<_Tp>& __x)
1346{
1347 complex<_Tp> __z = sinh(complex<_Tp>(-__x.imag(), __x.real()));
1348 return complex<_Tp>(__z.imag(), -__z.real());
1349}
1350
1351// cos
1352
1353template<class _Tp>
1354inline _LIBCPP_INLINE_VISIBILITY
1355complex<_Tp>
1356cos(const complex<_Tp>& __x)
1357{
1358 return cosh(complex<_Tp>(-__x.imag(), __x.real()));
1359}
1360
1361// tan
1362
1363template<class _Tp>
1364complex<_Tp>
1365tan(const complex<_Tp>& __x)
1366{
1367 complex<_Tp> __z = tanh(complex<_Tp>(-__x.imag(), __x.real()));
1368 return complex<_Tp>(__z.imag(), -__z.real());
1369}
1370
1371template<class _Tp, class _CharT, class _Traits>
1372basic_istream<_CharT, _Traits>&
1373operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
1374{
1375 if (__is.good())
1376 {
1377 ws(__is);
1378 if (__is.peek() == _CharT('('))
1379 {
1380 __is.get();
1381 _Tp __r;
1382 __is >> __r;
1383 if (!__is.fail())
1384 {
1385 ws(__is);
1386 _CharT __c = __is.peek();
1387 if (__c == _CharT(','))
1388 {
1389 __is.get();
1390 _Tp __i;
1391 __is >> __i;
1392 if (!__is.fail())
1393 {
1394 ws(__is);
1395 __c = __is.peek();
1396 if (__c == _CharT(')'))
1397 {
1398 __is.get();
1399 __x = complex<_Tp>(__r, __i);
1400 }
1401 else
1402 __is.setstate(ios_base::failbit);
1403 }
1404 else
1405 __is.setstate(ios_base::failbit);
1406 }
1407 else if (__c == _CharT(')'))
1408 {
1409 __is.get();
1410 __x = complex<_Tp>(__r, _Tp(0));
1411 }
1412 else
1413 __is.setstate(ios_base::failbit);
1414 }
1415 else
1416 __is.setstate(ios_base::failbit);
1417 }
1418 else
1419 {
1420 _Tp __r;
1421 __is >> __r;
1422 if (!__is.fail())
1423 __x = complex<_Tp>(__r, _Tp(0));
1424 else
1425 __is.setstate(ios_base::failbit);
1426 }
1427 }
1428 else
1429 __is.setstate(ios_base::failbit);
1430 return __is;
1431}
1432
1433template<class _Tp, class _CharT, class _Traits>
1434basic_ostream<_CharT, _Traits>&
1435operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
1436{
1437 basic_ostringstream<_CharT, _Traits> __s;
1438 __s.flags(__os.flags());
1439 __s.imbue(__os.getloc());
1440 __s.precision(__os.precision());
1441 __s << '(' << __x.real() << ',' << __x.imag() << ')';
1442 return __os << __s.str();
1443}
1444
Marshall Clow320c80f2013-10-05 21:19:491445#if _LIBCPP_STD_VER > 11
1446// Literal suffix for complex number literals [complex.literals]
1447inline namespace literals
1448{
1449 inline namespace complex_literals
1450 {
1451 constexpr complex<long double> operator""il(long double __im)
1452 {
1453 return { 0.0l, __im };
1454 }
1455
1456 constexpr complex<long double> operator""il(unsigned long long __im)
1457 {
1458 return { 0.0l, static_cast<long double>(__im) };
1459 }
1460
1461
1462 constexpr complex<double> operator""i(long double __im)
1463 {
1464 return { 0.0, static_cast<double>(__im) };
1465 }
1466
1467 constexpr complex<double> operator""i(unsigned long long __im)
1468 {
1469 return { 0.0, static_cast<double>(__im) };
1470 }
1471
1472
1473 constexpr complex<float> operator""if(long double __im)
1474 {
1475 return { 0.0f, static_cast<float>(__im) };
1476 }
1477
1478 constexpr complex<float> operator""if(unsigned long long __im)
1479 {
1480 return { 0.0f, static_cast<float>(__im) };
1481 }
1482 }
1483}
1484#endif
1485
Howard Hinnantbc8d3f92010-05-11 19:42:161486_LIBCPP_END_NAMESPACE_STD
1487
1488#endif // _LIBCPP_COMPLEX