blob: c168406befbdd967b3d4c4f8ff7760cd1ab909bf [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:161// -*- C++ -*-
2//===--------------------------- complex ----------------------------------===//
3//
Chandler Carruth7c3769d2019-01-19 10:56:404// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantbc8d3f92010-05-11 19:42:167//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_COMPLEX
11#define _LIBCPP_COMPLEX
12
13/*
14 complex synopsis
15
16namespace std
17{
18
19template<class T>
20class complex
21{
22public:
23 typedef T value_type;
24
Marshall Clowa61e6f82013-07-31 21:02:3425 complex(const T& re = T(), const T& im = T()); // constexpr in C++14
26 complex(const complex&); // constexpr in C++14
27 template<class X> complex(const complex<X>&); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:1628
Marshall Clowa61e6f82013-07-31 21:02:3429 T real() const; // constexpr in C++14
30 T imag() const; // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:1631
32 void real(T);
33 void imag(T);
34
35 complex<T>& operator= (const T&);
36 complex<T>& operator+=(const T&);
37 complex<T>& operator-=(const T&);
38 complex<T>& operator*=(const T&);
39 complex<T>& operator/=(const T&);
40
41 complex& operator=(const complex&);
42 template<class X> complex<T>& operator= (const complex<X>&);
43 template<class X> complex<T>& operator+=(const complex<X>&);
44 template<class X> complex<T>& operator-=(const complex<X>&);
45 template<class X> complex<T>& operator*=(const complex<X>&);
46 template<class X> complex<T>& operator/=(const complex<X>&);
47};
48
49template<>
50class complex<float>
Howard Hinnant324bb032010-08-22 00:02:4351{
52public:
53 typedef float value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:1654
Howard Hinnant324bb032010-08-22 00:02:4355 constexpr complex(float re = 0.0f, float im = 0.0f);
56 explicit constexpr complex(const complex<double>&);
57 explicit constexpr complex(const complex<long double>&);
Howard Hinnantbc8d3f92010-05-11 19:42:1658
Howard Hinnant324bb032010-08-22 00:02:4359 constexpr float real() const;
Howard Hinnantbc8d3f92010-05-11 19:42:1660 void real(float);
Howard Hinnant324bb032010-08-22 00:02:4361 constexpr float imag() const;
Howard Hinnantbc8d3f92010-05-11 19:42:1662 void imag(float);
63
Howard Hinnant324bb032010-08-22 00:02:4364 complex<float>& operator= (float);
65 complex<float>& operator+=(float);
66 complex<float>& operator-=(float);
67 complex<float>& operator*=(float);
68 complex<float>& operator/=(float);
Howard Hinnantbc8d3f92010-05-11 19:42:1669
Howard Hinnant324bb032010-08-22 00:02:4370 complex<float>& operator=(const complex<float>&);
71 template<class X> complex<float>& operator= (const complex<X>&);
72 template<class X> complex<float>& operator+=(const complex<X>&);
73 template<class X> complex<float>& operator-=(const complex<X>&);
74 template<class X> complex<float>& operator*=(const complex<X>&);
75 template<class X> complex<float>& operator/=(const complex<X>&);
Howard Hinnantbc8d3f92010-05-11 19:42:1676};
77
78template<>
79class complex<double>
Howard Hinnant324bb032010-08-22 00:02:4380{
81public:
82 typedef double value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:1683
Howard Hinnant324bb032010-08-22 00:02:4384 constexpr complex(double re = 0.0, double im = 0.0);
85 constexpr complex(const complex<float>&);
86 explicit constexpr complex(const complex<long double>&);
Howard Hinnantbc8d3f92010-05-11 19:42:1687
Howard Hinnant324bb032010-08-22 00:02:4388 constexpr double real() const;
Howard Hinnantbc8d3f92010-05-11 19:42:1689 void real(double);
Howard Hinnant324bb032010-08-22 00:02:4390 constexpr double imag() const;
Howard Hinnantbc8d3f92010-05-11 19:42:1691 void imag(double);
92
Howard Hinnant324bb032010-08-22 00:02:4393 complex<double>& operator= (double);
94 complex<double>& operator+=(double);
95 complex<double>& operator-=(double);
96 complex<double>& operator*=(double);
97 complex<double>& operator/=(double);
98 complex<double>& operator=(const complex<double>&);
Howard Hinnantbc8d3f92010-05-11 19:42:1699
Howard Hinnant324bb032010-08-22 00:02:43100 template<class X> complex<double>& operator= (const complex<X>&);
101 template<class X> complex<double>& operator+=(const complex<X>&);
102 template<class X> complex<double>& operator-=(const complex<X>&);
103 template<class X> complex<double>& operator*=(const complex<X>&);
104 template<class X> complex<double>& operator/=(const complex<X>&);
105};
Howard Hinnantbc8d3f92010-05-11 19:42:16106
107template<>
108class complex<long double>
Howard Hinnant324bb032010-08-22 00:02:43109{
110public:
111 typedef long double value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16112
Howard Hinnant324bb032010-08-22 00:02:43113 constexpr complex(long double re = 0.0L, long double im = 0.0L);
114 constexpr complex(const complex<float>&);
115 constexpr complex(const complex<double>&);
Howard Hinnantbc8d3f92010-05-11 19:42:16116
Howard Hinnant324bb032010-08-22 00:02:43117 constexpr long double real() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16118 void real(long double);
Howard Hinnant324bb032010-08-22 00:02:43119 constexpr long double imag() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16120 void imag(long double);
121
Howard Hinnant324bb032010-08-22 00:02:43122 complex<long double>& operator=(const complex<long double>&);
123 complex<long double>& operator= (long double);
124 complex<long double>& operator+=(long double);
125 complex<long double>& operator-=(long double);
126 complex<long double>& operator*=(long double);
127 complex<long double>& operator/=(long double);
Howard Hinnantbc8d3f92010-05-11 19:42:16128
Howard Hinnant324bb032010-08-22 00:02:43129 template<class X> complex<long double>& operator= (const complex<X>&);
130 template<class X> complex<long double>& operator+=(const complex<X>&);
131 template<class X> complex<long double>& operator-=(const complex<X>&);
132 template<class X> complex<long double>& operator*=(const complex<X>&);
133 template<class X> complex<long double>& operator/=(const complex<X>&);
Howard Hinnantbc8d3f92010-05-11 19:42:16134};
135
136// 26.3.6 operators:
137template<class T> complex<T> operator+(const complex<T>&, const complex<T>&);
138template<class T> complex<T> operator+(const complex<T>&, const T&);
139template<class T> complex<T> operator+(const T&, const complex<T>&);
140template<class T> complex<T> operator-(const complex<T>&, const complex<T>&);
141template<class T> complex<T> operator-(const complex<T>&, const T&);
142template<class T> complex<T> operator-(const T&, const complex<T>&);
143template<class T> complex<T> operator*(const complex<T>&, const complex<T>&);
144template<class T> complex<T> operator*(const complex<T>&, const T&);
145template<class T> complex<T> operator*(const T&, const complex<T>&);
146template<class T> complex<T> operator/(const complex<T>&, const complex<T>&);
147template<class T> complex<T> operator/(const complex<T>&, const T&);
148template<class T> complex<T> operator/(const T&, const complex<T>&);
149template<class T> complex<T> operator+(const complex<T>&);
150template<class T> complex<T> operator-(const complex<T>&);
Marshall Clowa61e6f82013-07-31 21:02:34151template<class T> bool operator==(const complex<T>&, const complex<T>&); // constexpr in C++14
152template<class T> bool operator==(const complex<T>&, const T&); // constexpr in C++14
153template<class T> bool operator==(const T&, const complex<T>&); // constexpr in C++14
154template<class T> bool operator!=(const complex<T>&, const complex<T>&); // constexpr in C++14
155template<class T> bool operator!=(const complex<T>&, const T&); // constexpr in C++14
156template<class T> bool operator!=(const T&, const complex<T>&); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16157
158template<class T, class charT, class traits>
159 basic_istream<charT, traits>&
160 operator>>(basic_istream<charT, traits>&, complex<T>&);
161template<class T, class charT, class traits>
162 basic_ostream<charT, traits>&
163 operator<<(basic_ostream<charT, traits>&, const complex<T>&);
164
165// 26.3.7 values:
166
Marshall Clowa61e6f82013-07-31 21:02:34167template<class T> T real(const complex<T>&); // constexpr in C++14
168 long double real(long double); // constexpr in C++14
169 double real(double); // constexpr in C++14
170template<Integral T> double real(T); // constexpr in C++14
171 float real(float); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16172
Marshall Clowa61e6f82013-07-31 21:02:34173template<class T> T imag(const complex<T>&); // constexpr in C++14
174 long double imag(long double); // constexpr in C++14
175 double imag(double); // constexpr in C++14
176template<Integral T> double imag(T); // constexpr in C++14
177 float imag(float); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16178
179template<class T> T abs(const complex<T>&);
180
181template<class T> T arg(const complex<T>&);
182 long double arg(long double);
183 double arg(double);
184template<Integral T> double arg(T);
185 float arg(float);
186
187template<class T> T norm(const complex<T>&);
188 long double norm(long double);
189 double norm(double);
190template<Integral T> double norm(T);
191 float norm(float);
192
Howard Hinnant995676a2010-11-18 17:34:48193template<class T> complex<T> conj(const complex<T>&);
194 complex<long double> conj(long double);
195 complex<double> conj(double);
196template<Integral T> complex<double> conj(T);
197 complex<float> conj(float);
Howard Hinnantbc8d3f92010-05-11 19:42:16198
Howard Hinnant995676a2010-11-18 17:34:48199template<class T> complex<T> proj(const complex<T>&);
200 complex<long double> proj(long double);
201 complex<double> proj(double);
202template<Integral T> complex<double> proj(T);
203 complex<float> proj(float);
Howard Hinnantbc8d3f92010-05-11 19:42:16204
Marshall Clow37e4c9b2018-01-31 21:42:39205template<class T> complex<T> polar(const T&, const T& = T());
Howard Hinnantbc8d3f92010-05-11 19:42:16206
207// 26.3.8 transcendentals:
208template<class T> complex<T> acos(const complex<T>&);
209template<class T> complex<T> asin(const complex<T>&);
210template<class T> complex<T> atan(const complex<T>&);
211template<class T> complex<T> acosh(const complex<T>&);
212template<class T> complex<T> asinh(const complex<T>&);
213template<class T> complex<T> atanh(const complex<T>&);
214template<class T> complex<T> cos (const complex<T>&);
215template<class T> complex<T> cosh (const complex<T>&);
216template<class T> complex<T> exp (const complex<T>&);
217template<class T> complex<T> log (const complex<T>&);
218template<class T> complex<T> log10(const complex<T>&);
219
220template<class T> complex<T> pow(const complex<T>&, const T&);
221template<class T> complex<T> pow(const complex<T>&, const complex<T>&);
222template<class T> complex<T> pow(const T&, const complex<T>&);
223
224template<class T> complex<T> sin (const complex<T>&);
225template<class T> complex<T> sinh (const complex<T>&);
226template<class T> complex<T> sqrt (const complex<T>&);
227template<class T> complex<T> tan (const complex<T>&);
228template<class T> complex<T> tanh (const complex<T>&);
229
230template<class T, class charT, class traits>
231 basic_istream<charT, traits>&
232 operator>>(basic_istream<charT, traits>& is, complex<T>& x);
233
234template<class T, class charT, class traits>
235 basic_ostream<charT, traits>&
236 operator<<(basic_ostream<charT, traits>& o, const complex<T>& x);
237
238} // std
239
240*/
241
242#include <__config>
243#include <type_traits>
244#include <stdexcept>
245#include <cmath>
246#include <sstream>
Marshall Clowe3973fd2018-09-12 19:41:40247#include <version>
Howard Hinnantbc8d3f92010-05-11 19:42:16248
Howard Hinnant08e17472011-10-17 20:05:10249#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16250#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10251#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16252
253_LIBCPP_BEGIN_NAMESPACE_STD
254
Eric Fiselierc3589a82017-01-04 23:56:00255template<class _Tp> class _LIBCPP_TEMPLATE_VIS complex;
Howard Hinnantbc8d3f92010-05-11 19:42:16256
257template<class _Tp> complex<_Tp> operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
258template<class _Tp> complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
259
260template<class _Tp>
Eric Fiselierc3589a82017-01-04 23:56:00261class _LIBCPP_TEMPLATE_VIS complex
Howard Hinnantbc8d3f92010-05-11 19:42:16262{
263public:
264 typedef _Tp value_type;
265private:
266 value_type __re_;
267 value_type __im_;
268public:
Marshall Clowa61e6f82013-07-31 21:02:34269 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16270 complex(const value_type& __re = value_type(), const value_type& __im = value_type())
271 : __re_(__re), __im_(__im) {}
Marshall Clowa61e6f82013-07-31 21:02:34272 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16273 complex(const complex<_Xp>& __c)
274 : __re_(__c.real()), __im_(__c.imag()) {}
275
Marshall Clowa61e6f82013-07-31 21:02:34276 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type real() const {return __re_;}
277 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type imag() const {return __im_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16278
279 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
280 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
281
Howard Hinnantae8b16e2012-01-10 15:15:47282 _LIBCPP_INLINE_VISIBILITY complex& operator= (const value_type& __re)
283 {__re_ = __re; __im_ = value_type(); return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16284 _LIBCPP_INLINE_VISIBILITY complex& operator+=(const value_type& __re) {__re_ += __re; return *this;}
285 _LIBCPP_INLINE_VISIBILITY complex& operator-=(const value_type& __re) {__re_ -= __re; return *this;}
286 _LIBCPP_INLINE_VISIBILITY complex& operator*=(const value_type& __re) {__re_ *= __re; __im_ *= __re; return *this;}
287 _LIBCPP_INLINE_VISIBILITY complex& operator/=(const value_type& __re) {__re_ /= __re; __im_ /= __re; return *this;}
288
289 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
290 {
291 __re_ = __c.real();
292 __im_ = __c.imag();
293 return *this;
294 }
295 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
296 {
297 __re_ += __c.real();
298 __im_ += __c.imag();
299 return *this;
300 }
301 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
302 {
303 __re_ -= __c.real();
304 __im_ -= __c.imag();
305 return *this;
306 }
307 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
308 {
Marshall Clowa61e6f82013-07-31 21:02:34309 *this = *this * complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16310 return *this;
311 }
312 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
313 {
Marshall Clowa61e6f82013-07-31 21:02:34314 *this = *this / complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16315 return *this;
316 }
317};
318
Eric Fiselierc3589a82017-01-04 23:56:00319template<> class _LIBCPP_TEMPLATE_VIS complex<double>;
320template<> class _LIBCPP_TEMPLATE_VIS complex<long double>;
Howard Hinnantbc8d3f92010-05-11 19:42:16321
322template<>
Eric Fiselierc3589a82017-01-04 23:56:00323class _LIBCPP_TEMPLATE_VIS complex<float>
Howard Hinnant324bb032010-08-22 00:02:43324{
Howard Hinnantbc8d3f92010-05-11 19:42:16325 float __re_;
326 float __im_;
Howard Hinnant324bb032010-08-22 00:02:43327public:
328 typedef float value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16329
Howard Hinnant410f2de2012-07-20 22:18:27330 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(float __re = 0.0f, float __im = 0.0f)
Howard Hinnantbc8d3f92010-05-11 19:42:16331 : __re_(__re), __im_(__im) {}
Evgeniy Stepanov9341a8a2016-04-22 01:04:55332 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant410f2de2012-07-20 22:18:27333 explicit _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
Evgeniy Stepanov9341a8a2016-04-22 01:04:55334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant410f2de2012-07-20 22:18:27335 explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16336
Howard Hinnant410f2de2012-07-20 22:18:27337 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float real() const {return __re_;}
338 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float imag() const {return __im_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16339
340 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
341 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
342
Howard Hinnantae8b16e2012-01-10 15:15:47343 _LIBCPP_INLINE_VISIBILITY complex& operator= (float __re)
344 {__re_ = __re; __im_ = value_type(); return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16345 _LIBCPP_INLINE_VISIBILITY complex& operator+=(float __re) {__re_ += __re; return *this;}
346 _LIBCPP_INLINE_VISIBILITY complex& operator-=(float __re) {__re_ -= __re; return *this;}
347 _LIBCPP_INLINE_VISIBILITY complex& operator*=(float __re) {__re_ *= __re; __im_ *= __re; return *this;}
348 _LIBCPP_INLINE_VISIBILITY complex& operator/=(float __re) {__re_ /= __re; __im_ /= __re; return *this;}
349
350 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
351 {
352 __re_ = __c.real();
353 __im_ = __c.imag();
354 return *this;
355 }
356 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
357 {
358 __re_ += __c.real();
359 __im_ += __c.imag();
360 return *this;
361 }
362 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
363 {
364 __re_ -= __c.real();
365 __im_ -= __c.imag();
366 return *this;
367 }
368 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
369 {
Marshall Clowa61e6f82013-07-31 21:02:34370 *this = *this * complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16371 return *this;
372 }
373 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
374 {
Marshall Clowa61e6f82013-07-31 21:02:34375 *this = *this / complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16376 return *this;
377 }
378};
379
380template<>
Eric Fiselierc3589a82017-01-04 23:56:00381class _LIBCPP_TEMPLATE_VIS complex<double>
Howard Hinnant324bb032010-08-22 00:02:43382{
Howard Hinnantbc8d3f92010-05-11 19:42:16383 double __re_;
384 double __im_;
Howard Hinnant324bb032010-08-22 00:02:43385public:
386 typedef double value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16387
Howard Hinnant410f2de2012-07-20 22:18:27388 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(double __re = 0.0, double __im = 0.0)
Howard Hinnantbc8d3f92010-05-11 19:42:16389 : __re_(__re), __im_(__im) {}
Evgeniy Stepanov9341a8a2016-04-22 01:04:55390 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant410f2de2012-07-20 22:18:27391 _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
Evgeniy Stepanov9341a8a2016-04-22 01:04:55392 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant410f2de2012-07-20 22:18:27393 explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16394
Howard Hinnant410f2de2012-07-20 22:18:27395 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double real() const {return __re_;}
396 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double imag() const {return __im_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16397
398 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
399 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
400
Howard Hinnantae8b16e2012-01-10 15:15:47401 _LIBCPP_INLINE_VISIBILITY complex& operator= (double __re)
402 {__re_ = __re; __im_ = value_type(); return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16403 _LIBCPP_INLINE_VISIBILITY complex& operator+=(double __re) {__re_ += __re; return *this;}
404 _LIBCPP_INLINE_VISIBILITY complex& operator-=(double __re) {__re_ -= __re; return *this;}
405 _LIBCPP_INLINE_VISIBILITY complex& operator*=(double __re) {__re_ *= __re; __im_ *= __re; return *this;}
406 _LIBCPP_INLINE_VISIBILITY complex& operator/=(double __re) {__re_ /= __re; __im_ /= __re; return *this;}
407
408 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
409 {
410 __re_ = __c.real();
411 __im_ = __c.imag();
412 return *this;
413 }
414 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
415 {
416 __re_ += __c.real();
417 __im_ += __c.imag();
418 return *this;
419 }
420 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
421 {
422 __re_ -= __c.real();
423 __im_ -= __c.imag();
424 return *this;
425 }
426 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
427 {
Marshall Clowa61e6f82013-07-31 21:02:34428 *this = *this * complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16429 return *this;
430 }
431 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
432 {
Marshall Clowa61e6f82013-07-31 21:02:34433 *this = *this / complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16434 return *this;
435 }
Howard Hinnant324bb032010-08-22 00:02:43436};
Howard Hinnantbc8d3f92010-05-11 19:42:16437
438template<>
Eric Fiselierc3589a82017-01-04 23:56:00439class _LIBCPP_TEMPLATE_VIS complex<long double>
Howard Hinnant324bb032010-08-22 00:02:43440{
Howard Hinnantbc8d3f92010-05-11 19:42:16441 long double __re_;
442 long double __im_;
Howard Hinnant324bb032010-08-22 00:02:43443public:
444 typedef long double value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16445
Howard Hinnant410f2de2012-07-20 22:18:27446 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(long double __re = 0.0L, long double __im = 0.0L)
Howard Hinnantbc8d3f92010-05-11 19:42:16447 : __re_(__re), __im_(__im) {}
Evgeniy Stepanov9341a8a2016-04-22 01:04:55448 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant410f2de2012-07-20 22:18:27449 _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
Evgeniy Stepanov9341a8a2016-04-22 01:04:55450 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant410f2de2012-07-20 22:18:27451 _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
Howard Hinnantbc8d3f92010-05-11 19:42:16452
Howard Hinnant410f2de2012-07-20 22:18:27453 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double real() const {return __re_;}
454 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double imag() const {return __im_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16455
456 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
457 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
458
Howard Hinnantae8b16e2012-01-10 15:15:47459 _LIBCPP_INLINE_VISIBILITY complex& operator= (long double __re)
460 {__re_ = __re; __im_ = value_type(); return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16461 _LIBCPP_INLINE_VISIBILITY complex& operator+=(long double __re) {__re_ += __re; return *this;}
462 _LIBCPP_INLINE_VISIBILITY complex& operator-=(long double __re) {__re_ -= __re; return *this;}
463 _LIBCPP_INLINE_VISIBILITY complex& operator*=(long double __re) {__re_ *= __re; __im_ *= __re; return *this;}
464 _LIBCPP_INLINE_VISIBILITY complex& operator/=(long double __re) {__re_ /= __re; __im_ /= __re; return *this;}
465
466 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
467 {
468 __re_ = __c.real();
469 __im_ = __c.imag();
470 return *this;
471 }
472 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
473 {
474 __re_ += __c.real();
475 __im_ += __c.imag();
476 return *this;
477 }
478 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
479 {
480 __re_ -= __c.real();
481 __im_ -= __c.imag();
482 return *this;
483 }
484 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
485 {
Marshall Clowa61e6f82013-07-31 21:02:34486 *this = *this * complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16487 return *this;
488 }
489 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
490 {
Marshall Clowa61e6f82013-07-31 21:02:34491 *this = *this / complex(__c.real(), __c.imag());
Howard Hinnantbc8d3f92010-05-11 19:42:16492 return *this;
493 }
494};
495
Evgeniy Stepanov9341a8a2016-04-22 01:04:55496inline
Howard Hinnant410f2de2012-07-20 22:18:27497_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16498complex<float>::complex(const complex<double>& __c)
499 : __re_(__c.real()), __im_(__c.imag()) {}
500
Evgeniy Stepanov9341a8a2016-04-22 01:04:55501inline
Howard Hinnant410f2de2012-07-20 22:18:27502_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16503complex<float>::complex(const complex<long double>& __c)
504 : __re_(__c.real()), __im_(__c.imag()) {}
505
Evgeniy Stepanov9341a8a2016-04-22 01:04:55506inline
Howard Hinnant410f2de2012-07-20 22:18:27507_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16508complex<double>::complex(const complex<float>& __c)
509 : __re_(__c.real()), __im_(__c.imag()) {}
510
Evgeniy Stepanov9341a8a2016-04-22 01:04:55511inline
Howard Hinnant410f2de2012-07-20 22:18:27512_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16513complex<double>::complex(const complex<long double>& __c)
514 : __re_(__c.real()), __im_(__c.imag()) {}
515
Evgeniy Stepanov9341a8a2016-04-22 01:04:55516inline
Howard Hinnant410f2de2012-07-20 22:18:27517_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16518complex<long double>::complex(const complex<float>& __c)
519 : __re_(__c.real()), __im_(__c.imag()) {}
520
Evgeniy Stepanov9341a8a2016-04-22 01:04:55521inline
Howard Hinnant410f2de2012-07-20 22:18:27522_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16523complex<long double>::complex(const complex<double>& __c)
524 : __re_(__c.real()), __im_(__c.imag()) {}
525
526// 26.3.6 operators:
527
528template<class _Tp>
529inline _LIBCPP_INLINE_VISIBILITY
530complex<_Tp>
531operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
532{
533 complex<_Tp> __t(__x);
534 __t += __y;
535 return __t;
536}
537
538template<class _Tp>
539inline _LIBCPP_INLINE_VISIBILITY
540complex<_Tp>
541operator+(const complex<_Tp>& __x, const _Tp& __y)
542{
543 complex<_Tp> __t(__x);
544 __t += __y;
545 return __t;
546}
547
548template<class _Tp>
549inline _LIBCPP_INLINE_VISIBILITY
550complex<_Tp>
551operator+(const _Tp& __x, const complex<_Tp>& __y)
552{
553 complex<_Tp> __t(__y);
554 __t += __x;
555 return __t;
556}
557
558template<class _Tp>
559inline _LIBCPP_INLINE_VISIBILITY
560complex<_Tp>
561operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
562{
563 complex<_Tp> __t(__x);
564 __t -= __y;
565 return __t;
566}
567
568template<class _Tp>
569inline _LIBCPP_INLINE_VISIBILITY
570complex<_Tp>
571operator-(const complex<_Tp>& __x, const _Tp& __y)
572{
573 complex<_Tp> __t(__x);
574 __t -= __y;
575 return __t;
576}
577
578template<class _Tp>
579inline _LIBCPP_INLINE_VISIBILITY
580complex<_Tp>
581operator-(const _Tp& __x, const complex<_Tp>& __y)
582{
583 complex<_Tp> __t(-__y);
584 __t += __x;
585 return __t;
586}
587
588template<class _Tp>
589complex<_Tp>
590operator*(const complex<_Tp>& __z, const complex<_Tp>& __w)
591{
592 _Tp __a = __z.real();
593 _Tp __b = __z.imag();
594 _Tp __c = __w.real();
595 _Tp __d = __w.imag();
596 _Tp __ac = __a * __c;
597 _Tp __bd = __b * __d;
598 _Tp __ad = __a * __d;
599 _Tp __bc = __b * __c;
600 _Tp __x = __ac - __bd;
601 _Tp __y = __ad + __bc;
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36602 if (__libcpp_isnan_or_builtin(__x) && __libcpp_isnan_or_builtin(__y))
Howard Hinnantbc8d3f92010-05-11 19:42:16603 {
604 bool __recalc = false;
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36605 if (__libcpp_isinf_or_builtin(__a) || __libcpp_isinf_or_builtin(__b))
Howard Hinnantbc8d3f92010-05-11 19:42:16606 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36607 __a = copysign(__libcpp_isinf_or_builtin(__a) ? _Tp(1) : _Tp(0), __a);
608 __b = copysign(__libcpp_isinf_or_builtin(__b) ? _Tp(1) : _Tp(0), __b);
609 if (__libcpp_isnan_or_builtin(__c))
Howard Hinnantbc8d3f92010-05-11 19:42:16610 __c = copysign(_Tp(0), __c);
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36611 if (__libcpp_isnan_or_builtin(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16612 __d = copysign(_Tp(0), __d);
613 __recalc = true;
614 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36615 if (__libcpp_isinf_or_builtin(__c) || __libcpp_isinf_or_builtin(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16616 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36617 __c = copysign(__libcpp_isinf_or_builtin(__c) ? _Tp(1) : _Tp(0), __c);
618 __d = copysign(__libcpp_isinf_or_builtin(__d) ? _Tp(1) : _Tp(0), __d);
619 if (__libcpp_isnan_or_builtin(__a))
Howard Hinnantbc8d3f92010-05-11 19:42:16620 __a = copysign(_Tp(0), __a);
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36621 if (__libcpp_isnan_or_builtin(__b))
Howard Hinnantbc8d3f92010-05-11 19:42:16622 __b = copysign(_Tp(0), __b);
623 __recalc = true;
624 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36625 if (!__recalc && (__libcpp_isinf_or_builtin(__ac) || __libcpp_isinf_or_builtin(__bd) ||
626 __libcpp_isinf_or_builtin(__ad) || __libcpp_isinf_or_builtin(__bc)))
Howard Hinnantbc8d3f92010-05-11 19:42:16627 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36628 if (__libcpp_isnan_or_builtin(__a))
Howard Hinnantbc8d3f92010-05-11 19:42:16629 __a = copysign(_Tp(0), __a);
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36630 if (__libcpp_isnan_or_builtin(__b))
Howard Hinnantbc8d3f92010-05-11 19:42:16631 __b = copysign(_Tp(0), __b);
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36632 if (__libcpp_isnan_or_builtin(__c))
Howard Hinnantbc8d3f92010-05-11 19:42:16633 __c = copysign(_Tp(0), __c);
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36634 if (__libcpp_isnan_or_builtin(__d))
Howard Hinnantbc8d3f92010-05-11 19:42:16635 __d = copysign(_Tp(0), __d);
636 __recalc = true;
637 }
638 if (__recalc)
639 {
640 __x = _Tp(INFINITY) * (__a * __c - __b * __d);
641 __y = _Tp(INFINITY) * (__a * __d + __b * __c);
642 }
643 }
644 return complex<_Tp>(__x, __y);
645}
646
647template<class _Tp>
648inline _LIBCPP_INLINE_VISIBILITY
649complex<_Tp>
650operator*(const complex<_Tp>& __x, const _Tp& __y)
651{
652 complex<_Tp> __t(__x);
653 __t *= __y;
654 return __t;
655}
656
657template<class _Tp>
658inline _LIBCPP_INLINE_VISIBILITY
659complex<_Tp>
660operator*(const _Tp& __x, const complex<_Tp>& __y)
661{
662 complex<_Tp> __t(__y);
663 __t *= __x;
664 return __t;
665}
666
667template<class _Tp>
668complex<_Tp>
669operator/(const complex<_Tp>& __z, const complex<_Tp>& __w)
670{
671 int __ilogbw = 0;
672 _Tp __a = __z.real();
673 _Tp __b = __z.imag();
674 _Tp __c = __w.real();
675 _Tp __d = __w.imag();
676 _Tp __logbw = logb(fmax(fabs(__c), fabs(__d)));
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36677 if (__libcpp_isfinite_or_builtin(__logbw))
Howard Hinnantbc8d3f92010-05-11 19:42:16678 {
679 __ilogbw = static_cast<int>(__logbw);
680 __c = scalbn(__c, -__ilogbw);
681 __d = scalbn(__d, -__ilogbw);
682 }
683 _Tp __denom = __c * __c + __d * __d;
684 _Tp __x = scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
685 _Tp __y = scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36686 if (__libcpp_isnan_or_builtin(__x) && __libcpp_isnan_or_builtin(__y))
Howard Hinnantbc8d3f92010-05-11 19:42:16687 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36688 if ((__denom == _Tp(0)) && (!__libcpp_isnan_or_builtin(__a) || !__libcpp_isnan_or_builtin(__b)))
Howard Hinnantbc8d3f92010-05-11 19:42:16689 {
690 __x = copysign(_Tp(INFINITY), __c) * __a;
691 __y = copysign(_Tp(INFINITY), __c) * __b;
692 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36693 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:16694 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36695 __a = copysign(__libcpp_isinf_or_builtin(__a) ? _Tp(1) : _Tp(0), __a);
696 __b = copysign(__libcpp_isinf_or_builtin(__b) ? _Tp(1) : _Tp(0), __b);
Howard Hinnantbc8d3f92010-05-11 19:42:16697 __x = _Tp(INFINITY) * (__a * __c + __b * __d);
698 __y = _Tp(INFINITY) * (__b * __c - __a * __d);
699 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36700 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:16701 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36702 __c = copysign(__libcpp_isinf_or_builtin(__c) ? _Tp(1) : _Tp(0), __c);
703 __d = copysign(__libcpp_isinf_or_builtin(__d) ? _Tp(1) : _Tp(0), __d);
Howard Hinnantbc8d3f92010-05-11 19:42:16704 __x = _Tp(0) * (__a * __c + __b * __d);
705 __y = _Tp(0) * (__b * __c - __a * __d);
706 }
707 }
708 return complex<_Tp>(__x, __y);
709}
710
711template<class _Tp>
712inline _LIBCPP_INLINE_VISIBILITY
713complex<_Tp>
714operator/(const complex<_Tp>& __x, const _Tp& __y)
715{
716 return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
717}
718
719template<class _Tp>
720inline _LIBCPP_INLINE_VISIBILITY
721complex<_Tp>
722operator/(const _Tp& __x, const complex<_Tp>& __y)
723{
724 complex<_Tp> __t(__x);
725 __t /= __y;
726 return __t;
727}
728
729template<class _Tp>
730inline _LIBCPP_INLINE_VISIBILITY
731complex<_Tp>
732operator+(const complex<_Tp>& __x)
733{
734 return __x;
735}
736
737template<class _Tp>
738inline _LIBCPP_INLINE_VISIBILITY
739complex<_Tp>
740operator-(const complex<_Tp>& __x)
741{
742 return complex<_Tp>(-__x.real(), -__x.imag());
743}
744
745template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34746inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16747bool
748operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
749{
750 return __x.real() == __y.real() && __x.imag() == __y.imag();
751}
752
753template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34754inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16755bool
756operator==(const complex<_Tp>& __x, const _Tp& __y)
757{
758 return __x.real() == __y && __x.imag() == 0;
759}
760
761template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34762inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16763bool
764operator==(const _Tp& __x, const complex<_Tp>& __y)
765{
766 return __x == __y.real() && 0 == __y.imag();
767}
768
769template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34770inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16771bool
772operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
773{
774 return !(__x == __y);
775}
776
777template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34778inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16779bool
780operator!=(const complex<_Tp>& __x, const _Tp& __y)
781{
782 return !(__x == __y);
783}
784
785template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34786inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16787bool
788operator!=(const _Tp& __x, const complex<_Tp>& __y)
789{
790 return !(__x == __y);
791}
792
Howard Hinnantbc8d3f92010-05-11 19:42:16793// 26.3.7 values:
794
Eric Fiselier781fb2a2016-07-20 00:14:10795template <class _Tp, bool = is_integral<_Tp>::value,
796 bool = is_floating_point<_Tp>::value
797 >
798struct __libcpp_complex_overload_traits {};
799
800// Integral Types
801template <class _Tp>
802struct __libcpp_complex_overload_traits<_Tp, true, false>
803{
804 typedef double _ValueType;
805 typedef complex<double> _ComplexType;
806};
807
808// Floating point types
809template <class _Tp>
810struct __libcpp_complex_overload_traits<_Tp, false, true>
811{
812 typedef _Tp _ValueType;
813 typedef complex<_Tp> _ComplexType;
814};
815
Howard Hinnantbc8d3f92010-05-11 19:42:16816// real
817
818template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34819inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16820_Tp
821real(const complex<_Tp>& __c)
822{
823 return __c.real();
824}
825
Eric Fiselier781fb2a2016-07-20 00:14:10826template <class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34827inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Eric Fiselier781fb2a2016-07-20 00:14:10828typename __libcpp_complex_overload_traits<_Tp>::_ValueType
829real(_Tp __re)
Howard Hinnantbc8d3f92010-05-11 19:42:16830{
831 return __re;
832}
833
834// imag
835
836template<class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34837inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16838_Tp
839imag(const complex<_Tp>& __c)
840{
841 return __c.imag();
842}
843
Eric Fiselier781fb2a2016-07-20 00:14:10844template <class _Tp>
Marshall Clowa61e6f82013-07-31 21:02:34845inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Eric Fiselier781fb2a2016-07-20 00:14:10846typename __libcpp_complex_overload_traits<_Tp>::_ValueType
Eric Fiselier0e5ebbc2016-12-23 23:37:52847imag(_Tp)
Howard Hinnantbc8d3f92010-05-11 19:42:16848{
849 return 0;
850}
851
852// abs
853
854template<class _Tp>
855inline _LIBCPP_INLINE_VISIBILITY
856_Tp
857abs(const complex<_Tp>& __c)
858{
859 return hypot(__c.real(), __c.imag());
860}
861
862// arg
863
864template<class _Tp>
865inline _LIBCPP_INLINE_VISIBILITY
866_Tp
867arg(const complex<_Tp>& __c)
868{
869 return atan2(__c.imag(), __c.real());
870}
871
Eric Fiselier781fb2a2016-07-20 00:14:10872template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16873inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier781fb2a2016-07-20 00:14:10874typename enable_if<
875 is_same<_Tp, long double>::value,
876 long double
877>::type
878arg(_Tp __re)
Howard Hinnantbc8d3f92010-05-11 19:42:16879{
880 return atan2l(0.L, __re);
881}
882
Howard Hinnantbc8d3f92010-05-11 19:42:16883template<class _Tp>
884inline _LIBCPP_INLINE_VISIBILITY
885typename enable_if
886<
Eric Fiselier781fb2a2016-07-20 00:14:10887 is_integral<_Tp>::value || is_same<_Tp, double>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16888 double
889>::type
890arg(_Tp __re)
891{
892 return atan2(0., __re);
893}
894
Eric Fiselier781fb2a2016-07-20 00:14:10895template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16896inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier781fb2a2016-07-20 00:14:10897typename enable_if<
898 is_same<_Tp, float>::value,
899 float
900>::type
901arg(_Tp __re)
Howard Hinnantbc8d3f92010-05-11 19:42:16902{
903 return atan2f(0.F, __re);
904}
905
906// norm
907
908template<class _Tp>
909inline _LIBCPP_INLINE_VISIBILITY
910_Tp
911norm(const complex<_Tp>& __c)
912{
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36913 if (__libcpp_isinf_or_builtin(__c.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:16914 return abs(__c.real());
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36915 if (__libcpp_isinf_or_builtin(__c.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:16916 return abs(__c.imag());
917 return __c.real() * __c.real() + __c.imag() * __c.imag();
918}
919
Eric Fiselier781fb2a2016-07-20 00:14:10920template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16921inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier781fb2a2016-07-20 00:14:10922typename __libcpp_complex_overload_traits<_Tp>::_ValueType
Howard Hinnantbc8d3f92010-05-11 19:42:16923norm(_Tp __re)
924{
Eric Fiselier781fb2a2016-07-20 00:14:10925 typedef typename __libcpp_complex_overload_traits<_Tp>::_ValueType _ValueType;
926 return static_cast<_ValueType>(__re) * __re;
Howard Hinnantbc8d3f92010-05-11 19:42:16927}
928
929// conj
930
931template<class _Tp>
932inline _LIBCPP_INLINE_VISIBILITY
933complex<_Tp>
934conj(const complex<_Tp>& __c)
935{
936 return complex<_Tp>(__c.real(), -__c.imag());
937}
938
Eric Fiselier781fb2a2016-07-20 00:14:10939template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16940inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier781fb2a2016-07-20 00:14:10941typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
Howard Hinnantbc8d3f92010-05-11 19:42:16942conj(_Tp __re)
943{
Eric Fiselier781fb2a2016-07-20 00:14:10944 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
945 return _ComplexType(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16946}
947
Eric Fiselier781fb2a2016-07-20 00:14:10948
Howard Hinnantbc8d3f92010-05-11 19:42:16949
950// proj
951
952template<class _Tp>
953inline _LIBCPP_INLINE_VISIBILITY
954complex<_Tp>
955proj(const complex<_Tp>& __c)
956{
957 std::complex<_Tp> __r = __c;
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36958 if (__libcpp_isinf_or_builtin(__c.real()) || __libcpp_isinf_or_builtin(__c.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:16959 __r = complex<_Tp>(INFINITY, copysign(_Tp(0), __c.imag()));
960 return __r;
961}
962
Eric Fiselier781fb2a2016-07-20 00:14:10963template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16964inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier781fb2a2016-07-20 00:14:10965typename enable_if
966<
967 is_floating_point<_Tp>::value,
968 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
969>::type
970proj(_Tp __re)
Howard Hinnantbc8d3f92010-05-11 19:42:16971{
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36972 if (__libcpp_isinf_or_builtin(__re))
Howard Hinnantbc8d3f92010-05-11 19:42:16973 __re = abs(__re);
Eric Fiselier781fb2a2016-07-20 00:14:10974 return complex<_Tp>(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16975}
976
Eric Fiselier781fb2a2016-07-20 00:14:10977template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16978inline _LIBCPP_INLINE_VISIBILITY
979typename enable_if
980<
981 is_integral<_Tp>::value,
Eric Fiselier781fb2a2016-07-20 00:14:10982 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
Howard Hinnantbc8d3f92010-05-11 19:42:16983>::type
984proj(_Tp __re)
985{
Eric Fiselier781fb2a2016-07-20 00:14:10986 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
987 return _ComplexType(__re);
Howard Hinnantbc8d3f92010-05-11 19:42:16988}
989
Howard Hinnantbc8d3f92010-05-11 19:42:16990// polar
991
992template<class _Tp>
993complex<_Tp>
Marshall Clow37e4c9b2018-01-31 21:42:39994polar(const _Tp& __rho, const _Tp& __theta = _Tp())
Howard Hinnantbc8d3f92010-05-11 19:42:16995{
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36996 if (__libcpp_isnan_or_builtin(__rho) || signbit(__rho))
Howard Hinnantbc8d3f92010-05-11 19:42:16997 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:36998 if (__libcpp_isnan_or_builtin(__theta))
Howard Hinnantbc8d3f92010-05-11 19:42:16999 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361000 if (__libcpp_isinf_or_builtin(__rho))
Howard Hinnantbc8d3f92010-05-11 19:42:161001 return complex<_Tp>(__rho, __theta);
1002 return complex<_Tp>(__theta, __theta);
1003 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361004 if (__libcpp_isinf_or_builtin(__theta))
Howard Hinnantbc8d3f92010-05-11 19:42:161005 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361006 if (__libcpp_isinf_or_builtin(__rho))
Howard Hinnantbc8d3f92010-05-11 19:42:161007 return complex<_Tp>(__rho, _Tp(NAN));
1008 return complex<_Tp>(_Tp(NAN), _Tp(NAN));
1009 }
1010 _Tp __x = __rho * cos(__theta);
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361011 if (__libcpp_isnan_or_builtin(__x))
Howard Hinnantbc8d3f92010-05-11 19:42:161012 __x = 0;
1013 _Tp __y = __rho * sin(__theta);
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361014 if (__libcpp_isnan_or_builtin(__y))
Howard Hinnantbc8d3f92010-05-11 19:42:161015 __y = 0;
1016 return complex<_Tp>(__x, __y);
1017}
1018
1019// log
1020
1021template<class _Tp>
1022inline _LIBCPP_INLINE_VISIBILITY
1023complex<_Tp>
1024log(const complex<_Tp>& __x)
1025{
1026 return complex<_Tp>(log(abs(__x)), arg(__x));
1027}
1028
1029// log10
1030
1031template<class _Tp>
1032inline _LIBCPP_INLINE_VISIBILITY
1033complex<_Tp>
1034log10(const complex<_Tp>& __x)
1035{
1036 return log(__x) / log(_Tp(10));
1037}
1038
1039// sqrt
1040
1041template<class _Tp>
1042complex<_Tp>
1043sqrt(const complex<_Tp>& __x)
1044{
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361045 if (__libcpp_isinf_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:161046 return complex<_Tp>(_Tp(INFINITY), __x.imag());
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361047 if (__libcpp_isinf_or_builtin(__x.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:161048 {
1049 if (__x.real() > _Tp(0))
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361050 return complex<_Tp>(__x.real(), __libcpp_isnan_or_builtin(__x.imag()) ? __x.imag() : copysign(_Tp(0), __x.imag()));
1051 return complex<_Tp>(__libcpp_isnan_or_builtin(__x.imag()) ? __x.imag() : _Tp(0), copysign(__x.real(), __x.imag()));
Howard Hinnantbc8d3f92010-05-11 19:42:161052 }
1053 return polar(sqrt(abs(__x)), arg(__x) / _Tp(2));
1054}
1055
1056// exp
1057
1058template<class _Tp>
1059complex<_Tp>
1060exp(const complex<_Tp>& __x)
1061{
1062 _Tp __i = __x.imag();
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361063 if (__libcpp_isinf_or_builtin(__x.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:161064 {
1065 if (__x.real() < _Tp(0))
1066 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361067 if (!__libcpp_isfinite_or_builtin(__i))
Howard Hinnantbc8d3f92010-05-11 19:42:161068 __i = _Tp(1);
1069 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361070 else if (__i == 0 || !__libcpp_isfinite_or_builtin(__i))
Howard Hinnantbc8d3f92010-05-11 19:42:161071 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361072 if (__libcpp_isinf_or_builtin(__i))
Howard Hinnantbc8d3f92010-05-11 19:42:161073 __i = _Tp(NAN);
1074 return complex<_Tp>(__x.real(), __i);
1075 }
1076 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361077 else if (__libcpp_isnan_or_builtin(__x.real()) && __x.imag() == 0)
Howard Hinnantbc8d3f92010-05-11 19:42:161078 return __x;
1079 _Tp __e = exp(__x.real());
1080 return complex<_Tp>(__e * cos(__i), __e * sin(__i));
1081}
1082
1083// pow
1084
1085template<class _Tp>
1086inline _LIBCPP_INLINE_VISIBILITY
1087complex<_Tp>
1088pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
1089{
1090 return exp(__y * log(__x));
1091}
1092
1093template<class _Tp, class _Up>
1094inline _LIBCPP_INLINE_VISIBILITY
1095complex<typename __promote<_Tp, _Up>::type>
1096pow(const complex<_Tp>& __x, const complex<_Up>& __y)
1097{
1098 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
Howard Hinnant0949eed2011-06-30 21:18:191099 return _VSTD::pow(result_type(__x), result_type(__y));
Howard Hinnantbc8d3f92010-05-11 19:42:161100}
1101
1102template<class _Tp, class _Up>
1103inline _LIBCPP_INLINE_VISIBILITY
1104typename enable_if
1105<
1106 is_arithmetic<_Up>::value,
1107 complex<typename __promote<_Tp, _Up>::type>
1108>::type
1109pow(const complex<_Tp>& __x, const _Up& __y)
1110{
1111 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
Howard Hinnant0949eed2011-06-30 21:18:191112 return _VSTD::pow(result_type(__x), result_type(__y));
Howard Hinnantbc8d3f92010-05-11 19:42:161113}
1114
1115template<class _Tp, class _Up>
1116inline _LIBCPP_INLINE_VISIBILITY
1117typename enable_if
1118<
1119 is_arithmetic<_Tp>::value,
1120 complex<typename __promote<_Tp, _Up>::type>
1121>::type
1122pow(const _Tp& __x, const complex<_Up>& __y)
1123{
1124 typedef complex<typename __promote<_Tp, _Up>::type> result_type;
Howard Hinnant0949eed2011-06-30 21:18:191125 return _VSTD::pow(result_type(__x), result_type(__y));
Howard Hinnantbc8d3f92010-05-11 19:42:161126}
1127
Mikhail Maltsevc4658ab2018-02-19 15:41:361128// __sqr, computes pow(x, 2)
1129
1130template<class _Tp>
1131inline _LIBCPP_INLINE_VISIBILITY
1132complex<_Tp>
1133__sqr(const complex<_Tp>& __x)
1134{
1135 return complex<_Tp>((__x.real() - __x.imag()) * (__x.real() + __x.imag()),
1136 _Tp(2) * __x.real() * __x.imag());
1137}
1138
Howard Hinnantbc8d3f92010-05-11 19:42:161139// asinh
1140
1141template<class _Tp>
1142complex<_Tp>
1143asinh(const complex<_Tp>& __x)
1144{
1145 const _Tp __pi(atan2(+0., -0.));
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361146 if (__libcpp_isinf_or_builtin(__x.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:161147 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361148 if (__libcpp_isnan_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:161149 return __x;
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361150 if (__libcpp_isinf_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:161151 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1152 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1153 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361154 if (__libcpp_isnan_or_builtin(__x.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:161155 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361156 if (__libcpp_isinf_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:161157 return complex<_Tp>(__x.imag(), __x.real());
1158 if (__x.imag() == 0)
1159 return __x;
1160 return complex<_Tp>(__x.real(), __x.real());
1161 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361162 if (__libcpp_isinf_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:161163 return complex<_Tp>(copysign(__x.imag(), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
Mikhail Maltsevc4658ab2018-02-19 15:41:361164 complex<_Tp> __z = log(__x + sqrt(__sqr(__x) + _Tp(1)));
Howard Hinnantbc8d3f92010-05-11 19:42:161165 return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1166}
1167
1168// acosh
1169
1170template<class _Tp>
1171complex<_Tp>
1172acosh(const complex<_Tp>& __x)
1173{
1174 const _Tp __pi(atan2(+0., -0.));
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361175 if (__libcpp_isinf_or_builtin(__x.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:161176 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361177 if (__libcpp_isnan_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:161178 return complex<_Tp>(abs(__x.real()), __x.imag());
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361179 if (__libcpp_isinf_or_builtin(__x.imag()))
Howard Hinnant0919dba2012-11-06 21:55:441180 {
Howard Hinnantbc8d3f92010-05-11 19:42:161181 if (__x.real() > 0)
1182 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
1183 else
1184 return complex<_Tp>(-__x.real(), copysign(__pi * _Tp(0.75), __x.imag()));
Howard Hinnant0919dba2012-11-06 21:55:441185 }
Howard Hinnantbc8d3f92010-05-11 19:42:161186 if (__x.real() < 0)
1187 return complex<_Tp>(-__x.real(), copysign(__pi, __x.imag()));
1188 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
1189 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361190 if (__libcpp_isnan_or_builtin(__x.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:161191 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361192 if (__libcpp_isinf_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:161193 return complex<_Tp>(abs(__x.imag()), __x.real());
1194 return complex<_Tp>(__x.real(), __x.real());
1195 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361196 if (__libcpp_isinf_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:161197 return complex<_Tp>(abs(__x.imag()), copysign(__pi/_Tp(2), __x.imag()));
Mikhail Maltsevc4658ab2018-02-19 15:41:361198 complex<_Tp> __z = log(__x + sqrt(__sqr(__x) - _Tp(1)));
Howard Hinnantbc8d3f92010-05-11 19:42:161199 return complex<_Tp>(copysign(__z.real(), _Tp(0)), copysign(__z.imag(), __x.imag()));
1200}
1201
1202// atanh
1203
1204template<class _Tp>
1205complex<_Tp>
1206atanh(const complex<_Tp>& __x)
1207{
1208 const _Tp __pi(atan2(+0., -0.));
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361209 if (__libcpp_isinf_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:161210 {
1211 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1212 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361213 if (__libcpp_isnan_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:161214 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361215 if (__libcpp_isinf_or_builtin(__x.real()) || __x.real() == 0)
Howard Hinnantbc8d3f92010-05-11 19:42:161216 return complex<_Tp>(copysign(_Tp(0), __x.real()), __x.imag());
1217 return complex<_Tp>(__x.imag(), __x.imag());
1218 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361219 if (__libcpp_isnan_or_builtin(__x.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:161220 {
1221 return complex<_Tp>(__x.real(), __x.real());
1222 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361223 if (__libcpp_isinf_or_builtin(__x.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:161224 {
1225 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
1226 }
1227 if (abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0))
1228 {
1229 return complex<_Tp>(copysign(_Tp(INFINITY), __x.real()), copysign(_Tp(0), __x.imag()));
1230 }
1231 complex<_Tp> __z = log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2);
1232 return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
1233}
1234
1235// sinh
1236
1237template<class _Tp>
1238complex<_Tp>
1239sinh(const complex<_Tp>& __x)
1240{
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361241 if (__libcpp_isinf_or_builtin(__x.real()) && !__libcpp_isfinite_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:161242 return complex<_Tp>(__x.real(), _Tp(NAN));
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361243 if (__x.real() == 0 && !__libcpp_isfinite_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:161244 return complex<_Tp>(__x.real(), _Tp(NAN));
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361245 if (__x.imag() == 0 && !__libcpp_isfinite_or_builtin(__x.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:161246 return __x;
1247 return complex<_Tp>(sinh(__x.real()) * cos(__x.imag()), cosh(__x.real()) * sin(__x.imag()));
1248}
1249
1250// cosh
1251
1252template<class _Tp>
1253complex<_Tp>
1254cosh(const complex<_Tp>& __x)
1255{
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361256 if (__libcpp_isinf_or_builtin(__x.real()) && !__libcpp_isfinite_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:161257 return complex<_Tp>(abs(__x.real()), _Tp(NAN));
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361258 if (__x.real() == 0 && !__libcpp_isfinite_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:161259 return complex<_Tp>(_Tp(NAN), __x.real());
1260 if (__x.real() == 0 && __x.imag() == 0)
1261 return complex<_Tp>(_Tp(1), __x.imag());
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361262 if (__x.imag() == 0 && !__libcpp_isfinite_or_builtin(__x.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:161263 return complex<_Tp>(abs(__x.real()), __x.imag());
1264 return complex<_Tp>(cosh(__x.real()) * cos(__x.imag()), sinh(__x.real()) * sin(__x.imag()));
1265}
1266
1267// tanh
1268
1269template<class _Tp>
1270complex<_Tp>
1271tanh(const complex<_Tp>& __x)
1272{
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361273 if (__libcpp_isinf_or_builtin(__x.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:161274 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361275 if (!__libcpp_isfinite_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:161276 return complex<_Tp>(_Tp(1), _Tp(0));
1277 return complex<_Tp>(_Tp(1), copysign(_Tp(0), sin(_Tp(2) * __x.imag())));
1278 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361279 if (__libcpp_isnan_or_builtin(__x.real()) && __x.imag() == 0)
Howard Hinnantbc8d3f92010-05-11 19:42:161280 return __x;
1281 _Tp __2r(_Tp(2) * __x.real());
1282 _Tp __2i(_Tp(2) * __x.imag());
1283 _Tp __d(cosh(__2r) + cos(__2i));
Howard Hinnant2d3f4ee2012-09-19 23:51:471284 _Tp __2rsh(sinh(__2r));
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361285 if (__libcpp_isinf_or_builtin(__2rsh) && __libcpp_isinf_or_builtin(__d))
Howard Hinnant2d3f4ee2012-09-19 23:51:471286 return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1),
1287 __2i > _Tp(0) ? _Tp(0) : _Tp(-0.));
1288 return complex<_Tp>(__2rsh/__d, sin(__2i)/__d);
Howard Hinnantbc8d3f92010-05-11 19:42:161289}
1290
1291// asin
1292
1293template<class _Tp>
1294complex<_Tp>
1295asin(const complex<_Tp>& __x)
1296{
1297 complex<_Tp> __z = asinh(complex<_Tp>(-__x.imag(), __x.real()));
1298 return complex<_Tp>(__z.imag(), -__z.real());
1299}
1300
1301// acos
1302
1303template<class _Tp>
1304complex<_Tp>
1305acos(const complex<_Tp>& __x)
1306{
1307 const _Tp __pi(atan2(+0., -0.));
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361308 if (__libcpp_isinf_or_builtin(__x.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:161309 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361310 if (__libcpp_isnan_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:161311 return complex<_Tp>(__x.imag(), __x.real());
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361312 if (__libcpp_isinf_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:161313 {
1314 if (__x.real() < _Tp(0))
1315 return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());
1316 return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag());
1317 }
1318 if (__x.real() < _Tp(0))
1319 return complex<_Tp>(__pi, signbit(__x.imag()) ? -__x.real() : __x.real());
1320 return complex<_Tp>(_Tp(0), signbit(__x.imag()) ? __x.real() : -__x.real());
1321 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361322 if (__libcpp_isnan_or_builtin(__x.real()))
Howard Hinnantbc8d3f92010-05-11 19:42:161323 {
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361324 if (__libcpp_isinf_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:161325 return complex<_Tp>(__x.real(), -__x.imag());
1326 return complex<_Tp>(__x.real(), __x.real());
1327 }
Duncan P. N. Exon Smithe452f6a2017-07-07 05:13:361328 if (__libcpp_isinf_or_builtin(__x.imag()))
Howard Hinnantbc8d3f92010-05-11 19:42:161329 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
Marshall Clowa2778112016-04-04 16:08:541330 if (__x.real() == 0 && (__x.imag() == 0 || isnan(__x.imag())))
Howard Hinnantbc8d3f92010-05-11 19:42:161331 return complex<_Tp>(__pi/_Tp(2), -__x.imag());
Mikhail Maltsevc4658ab2018-02-19 15:41:361332 complex<_Tp> __z = log(__x + sqrt(__sqr(__x) - _Tp(1)));
Howard Hinnantbc8d3f92010-05-11 19:42:161333 if (signbit(__x.imag()))
1334 return complex<_Tp>(abs(__z.imag()), abs(__z.real()));
1335 return complex<_Tp>(abs(__z.imag()), -abs(__z.real()));
1336}
1337
1338// atan
1339
1340template<class _Tp>
1341complex<_Tp>
1342atan(const complex<_Tp>& __x)
1343{
1344 complex<_Tp> __z = atanh(complex<_Tp>(-__x.imag(), __x.real()));
1345 return complex<_Tp>(__z.imag(), -__z.real());
1346}
1347
1348// sin
1349
1350template<class _Tp>
1351complex<_Tp>
1352sin(const complex<_Tp>& __x)
1353{
1354 complex<_Tp> __z = sinh(complex<_Tp>(-__x.imag(), __x.real()));
1355 return complex<_Tp>(__z.imag(), -__z.real());
1356}
1357
1358// cos
1359
1360template<class _Tp>
1361inline _LIBCPP_INLINE_VISIBILITY
1362complex<_Tp>
1363cos(const complex<_Tp>& __x)
1364{
1365 return cosh(complex<_Tp>(-__x.imag(), __x.real()));
1366}
1367
1368// tan
1369
1370template<class _Tp>
1371complex<_Tp>
1372tan(const complex<_Tp>& __x)
1373{
1374 complex<_Tp> __z = tanh(complex<_Tp>(-__x.imag(), __x.real()));
1375 return complex<_Tp>(__z.imag(), -__z.real());
1376}
1377
1378template<class _Tp, class _CharT, class _Traits>
1379basic_istream<_CharT, _Traits>&
1380operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
1381{
1382 if (__is.good())
1383 {
1384 ws(__is);
1385 if (__is.peek() == _CharT('('))
1386 {
1387 __is.get();
1388 _Tp __r;
1389 __is >> __r;
1390 if (!__is.fail())
1391 {
1392 ws(__is);
1393 _CharT __c = __is.peek();
1394 if (__c == _CharT(','))
1395 {
1396 __is.get();
1397 _Tp __i;
1398 __is >> __i;
1399 if (!__is.fail())
1400 {
1401 ws(__is);
1402 __c = __is.peek();
1403 if (__c == _CharT(')'))
1404 {
1405 __is.get();
1406 __x = complex<_Tp>(__r, __i);
1407 }
1408 else
1409 __is.setstate(ios_base::failbit);
1410 }
1411 else
1412 __is.setstate(ios_base::failbit);
1413 }
1414 else if (__c == _CharT(')'))
1415 {
1416 __is.get();
1417 __x = complex<_Tp>(__r, _Tp(0));
1418 }
1419 else
1420 __is.setstate(ios_base::failbit);
1421 }
1422 else
1423 __is.setstate(ios_base::failbit);
1424 }
1425 else
1426 {
1427 _Tp __r;
1428 __is >> __r;
1429 if (!__is.fail())
1430 __x = complex<_Tp>(__r, _Tp(0));
1431 else
1432 __is.setstate(ios_base::failbit);
1433 }
1434 }
1435 else
1436 __is.setstate(ios_base::failbit);
1437 return __is;
1438}
1439
1440template<class _Tp, class _CharT, class _Traits>
1441basic_ostream<_CharT, _Traits>&
1442operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
1443{
1444 basic_ostringstream<_CharT, _Traits> __s;
1445 __s.flags(__os.flags());
1446 __s.imbue(__os.getloc());
1447 __s.precision(__os.precision());
1448 __s << '(' << __x.real() << ',' << __x.imag() << ')';
1449 return __os << __s.str();
1450}
1451
Louis Dionnec30e2d92019-05-29 16:01:361452#if _LIBCPP_STD_VER > 11
Marshall Clow320c80f2013-10-05 21:19:491453// Literal suffix for complex number literals [complex.literals]
1454inline namespace literals
Louis Dionnec30e2d92019-05-29 16:01:361455{
Marshall Clow320c80f2013-10-05 21:19:491456 inline namespace complex_literals
1457 {
1458 constexpr complex<long double> operator""il(long double __im)
1459 {
1460 return { 0.0l, __im };
1461 }
1462
1463 constexpr complex<long double> operator""il(unsigned long long __im)
1464 {
1465 return { 0.0l, static_cast<long double>(__im) };
1466 }
1467
1468
1469 constexpr complex<double> operator""i(long double __im)
1470 {
1471 return { 0.0, static_cast<double>(__im) };
1472 }
1473
1474 constexpr complex<double> operator""i(unsigned long long __im)
1475 {
1476 return { 0.0, static_cast<double>(__im) };
1477 }
1478
1479
1480 constexpr complex<float> operator""if(long double __im)
1481 {
1482 return { 0.0f, static_cast<float>(__im) };
1483 }
1484
1485 constexpr complex<float> operator""if(unsigned long long __im)
1486 {
1487 return { 0.0f, static_cast<float>(__im) };
1488 }
1489 }
1490}
1491#endif
1492
Howard Hinnantbc8d3f92010-05-11 19:42:161493_LIBCPP_END_NAMESPACE_STD
1494
1495#endif // _LIBCPP_COMPLEX