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