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