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 // [complex.tuple], tuple interface 231 template<class T> struct tuple_size; // Since C++26 232 template<size_t I, class T> struct tuple_element; // Since C++26 233 template<class T> struct tuple_size<complex<T>>; // Since C++26 234 template<size_t I, class T> struct tuple_element<I, complex<T>>; // Since C++26 235 template<size_t I, class T> 236 constexpr T& get(complex<T>&) noexcept; // Since C++26 237 template<size_t I, class T> 238 constexpr T&& get(complex<T>&&) noexcept; // Since C++26 239 template<size_t I, class T> 240 constexpr const T& get(const complex<T>&) noexcept; // Since C++26 241 template<size_t I, class T> 242 constexpr const T&& get(const complex<T>&&) noexcept; // Since C++26 243 244 // [complex.literals], complex literals 245 inline namespace literals { 246 inline namespace complex_literals { 247 constexpr complex<long double> operator""il(long double); // Since C++14 248 constexpr complex<long double> operator""il(unsigned long long); // Since C++14 249 constexpr complex<double> operator""i(long double); // Since C++14 250 constexpr complex<double> operator""i(unsigned long long); // Since C++14 251 constexpr complex<float> operator""if(long double); // Since C++14 252 constexpr complex<float> operator""if(unsigned long long); // Since C++14 253 } 254 } 255} // std 256 257*/ 258 259#include <__config> 260#include <__fwd/complex.h> 261#include <__fwd/tuple.h> 262#include <__tuple/tuple_element.h> 263#include <__tuple/tuple_size.h> 264#include <__type_traits/conditional.h> 265#include <__utility/move.h> 266#include <cmath> 267#include <version> 268 269#if !defined(_LIBCPP_HAS_NO_LOCALIZATION) 270# include <sstream> // for std::basic_ostringstream 271#endif 272 273#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 274# pragma GCC system_header 275#endif 276 277_LIBCPP_PUSH_MACROS 278#include <__undef_macros> 279 280_LIBCPP_BEGIN_NAMESPACE_STD 281 282template <class _Tp> 283class _LIBCPP_TEMPLATE_VIS complex; 284 285template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> = 0> 286_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> 287operator*(const complex<_Tp>& __z, const complex<_Tp>& __w); 288 289template <class _Tp, __enable_if_t<!is_floating_point<_Tp>::value, int> = 0> 290_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> 291operator*(const complex<_Tp>& __z, const complex<_Tp>& __w); 292 293template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> = 0> 294_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> 295operator/(const complex<_Tp>& __x, const complex<_Tp>& __y); 296 297template <class _Tp, __enable_if_t<!is_floating_point<_Tp>::value, int> = 0> 298_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> 299operator/(const complex<_Tp>& __x, const complex<_Tp>& __y); 300 301template <class _Tp> 302class _LIBCPP_TEMPLATE_VIS complex { 303public: 304 typedef _Tp value_type; 305 306private: 307 value_type __re_; 308 value_type __im_; 309 310public: 311 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 312 complex(const value_type& __re = value_type(), const value_type& __im = value_type()) 313 : __re_(__re), __im_(__im) {} 314 template <class _Xp> 315 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 complex(const complex<_Xp>& __c) 316 : __re_(__c.real()), __im_(__c.imag()) {} 317 318 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 value_type real() const { return __re_; } 319 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 value_type imag() const { return __im_; } 320 321 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) { __re_ = __re; } 322 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) { __im_ = __im; } 323 324 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(const value_type& __re) { 325 __re_ = __re; 326 __im_ = value_type(); 327 return *this; 328 } 329 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const value_type& __re) { 330 __re_ += __re; 331 return *this; 332 } 333 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const value_type& __re) { 334 __re_ -= __re; 335 return *this; 336 } 337 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const value_type& __re) { 338 __re_ *= __re; 339 __im_ *= __re; 340 return *this; 341 } 342 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const value_type& __re) { 343 __re_ /= __re; 344 __im_ /= __re; 345 return *this; 346 } 347 348 template <class _Xp> 349 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(const complex<_Xp>& __c) { 350 __re_ = __c.real(); 351 __im_ = __c.imag(); 352 return *this; 353 } 354 template <class _Xp> 355 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c) { 356 __re_ += __c.real(); 357 __im_ += __c.imag(); 358 return *this; 359 } 360 template <class _Xp> 361 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c) { 362 __re_ -= __c.real(); 363 __im_ -= __c.imag(); 364 return *this; 365 } 366 template <class _Xp> 367 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c) { 368 *this = *this * complex(__c.real(), __c.imag()); 369 return *this; 370 } 371 template <class _Xp> 372 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c) { 373 *this = *this / complex(__c.real(), __c.imag()); 374 return *this; 375 } 376 377#if _LIBCPP_STD_VER >= 26 378 template <size_t _Ip, class _Xp> 379 friend _LIBCPP_HIDE_FROM_ABI constexpr _Xp& get(complex<_Xp>&) noexcept; 380 381 template <size_t _Ip, class _Xp> 382 friend _LIBCPP_HIDE_FROM_ABI constexpr _Xp&& get(complex<_Xp>&&) noexcept; 383 384 template <size_t _Ip, class _Xp> 385 friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp& get(const complex<_Xp>&) noexcept; 386 387 template <size_t _Ip, class _Xp> 388 friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp&& get(const complex<_Xp>&&) noexcept; 389#endif 390}; 391 392template <> 393class _LIBCPP_TEMPLATE_VIS complex<double>; 394template <> 395class _LIBCPP_TEMPLATE_VIS complex<long double>; 396 397struct __from_builtin_tag {}; 398 399template <class _Tp> 400using __complex_t = 401 __conditional_t<is_same<_Tp, float>::value, 402 _Complex float, 403 __conditional_t<is_same<_Tp, double>::value, _Complex double, _Complex long double> >; 404 405template <class _Tp> 406_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __complex_t<_Tp> __make_complex(_Tp __re, _Tp __im) { 407#if __has_builtin(__builtin_complex) 408 return __builtin_complex(__re, __im); 409#else 410 return __complex_t<_Tp>{__re, __im}; 411#endif 412} 413 414template <> 415class _LIBCPP_TEMPLATE_VIS complex<float> { 416 float __re_; 417 float __im_; 418 419public: 420 typedef float value_type; 421 422 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(float __re = 0.0f, float __im = 0.0f) : __re_(__re), __im_(__im) {} 423 424 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(__from_builtin_tag, _Complex float __v) 425 : __re_(__real__ __v), __im_(__imag__ __v) {} 426 427 _LIBCPP_HIDE_FROM_ABI explicit _LIBCPP_CONSTEXPR complex(const complex<double>& __c); 428 _LIBCPP_HIDE_FROM_ABI explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c); 429 430 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR float real() const { return __re_; } 431 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR float imag() const { return __im_; } 432 433 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) { __re_ = __re; } 434 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) { __im_ = __im; } 435 436 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Complex float __builtin() const { return std::__make_complex(__re_, __im_); } 437 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __builtin(_Complex float __f) { 438 __re_ = __real__ __f; 439 __im_ = __imag__ __f; 440 } 441 442 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(float __re) { 443 __re_ = __re; 444 __im_ = value_type(); 445 return *this; 446 } 447 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(float __re) { 448 __re_ += __re; 449 return *this; 450 } 451 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(float __re) { 452 __re_ -= __re; 453 return *this; 454 } 455 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(float __re) { 456 __re_ *= __re; 457 __im_ *= __re; 458 return *this; 459 } 460 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(float __re) { 461 __re_ /= __re; 462 __im_ /= __re; 463 return *this; 464 } 465 466 template <class _Xp> 467 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(const complex<_Xp>& __c) { 468 __re_ = __c.real(); 469 __im_ = __c.imag(); 470 return *this; 471 } 472 template <class _Xp> 473 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c) { 474 __re_ += __c.real(); 475 __im_ += __c.imag(); 476 return *this; 477 } 478 template <class _Xp> 479 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c) { 480 __re_ -= __c.real(); 481 __im_ -= __c.imag(); 482 return *this; 483 } 484 template <class _Xp> 485 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c) { 486 *this = *this * complex(__c.real(), __c.imag()); 487 return *this; 488 } 489 template <class _Xp> 490 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c) { 491 *this = *this / complex(__c.real(), __c.imag()); 492 return *this; 493 } 494 495#if _LIBCPP_STD_VER >= 26 496 template <size_t _Ip, class _Xp> 497 friend _LIBCPP_HIDE_FROM_ABI constexpr _Xp& get(complex<_Xp>&) noexcept; 498 499 template <size_t _Ip, class _Xp> 500 friend _LIBCPP_HIDE_FROM_ABI constexpr _Xp&& get(complex<_Xp>&&) noexcept; 501 502 template <size_t _Ip, class _Xp> 503 friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp& get(const complex<_Xp>&) noexcept; 504 505 template <size_t _Ip, class _Xp> 506 friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp&& get(const complex<_Xp>&&) noexcept; 507#endif 508}; 509 510template <> 511class _LIBCPP_TEMPLATE_VIS complex<double> { 512 double __re_; 513 double __im_; 514 515public: 516 typedef double value_type; 517 518 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(double __re = 0.0, double __im = 0.0) : __re_(__re), __im_(__im) {} 519 520 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(__from_builtin_tag, _Complex double __v) 521 : __re_(__real__ __v), __im_(__imag__ __v) {} 522 523 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(const complex<float>& __c); 524 _LIBCPP_HIDE_FROM_ABI explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c); 525 526 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR double real() const { return __re_; } 527 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR double imag() const { return __im_; } 528 529 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) { __re_ = __re; } 530 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) { __im_ = __im; } 531 532 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Complex double __builtin() const { 533 return std::__make_complex(__re_, __im_); 534 } 535 536 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __builtin(_Complex double __f) { 537 __re_ = __real__ __f; 538 __im_ = __imag__ __f; 539 } 540 541 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(double __re) { 542 __re_ = __re; 543 __im_ = value_type(); 544 return *this; 545 } 546 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(double __re) { 547 __re_ += __re; 548 return *this; 549 } 550 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(double __re) { 551 __re_ -= __re; 552 return *this; 553 } 554 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(double __re) { 555 __re_ *= __re; 556 __im_ *= __re; 557 return *this; 558 } 559 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(double __re) { 560 __re_ /= __re; 561 __im_ /= __re; 562 return *this; 563 } 564 565 template <class _Xp> 566 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(const complex<_Xp>& __c) { 567 __re_ = __c.real(); 568 __im_ = __c.imag(); 569 return *this; 570 } 571 template <class _Xp> 572 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c) { 573 __re_ += __c.real(); 574 __im_ += __c.imag(); 575 return *this; 576 } 577 template <class _Xp> 578 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c) { 579 __re_ -= __c.real(); 580 __im_ -= __c.imag(); 581 return *this; 582 } 583 template <class _Xp> 584 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c) { 585 *this = *this * complex(__c.real(), __c.imag()); 586 return *this; 587 } 588 template <class _Xp> 589 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c) { 590 *this = *this / complex(__c.real(), __c.imag()); 591 return *this; 592 } 593 594#if _LIBCPP_STD_VER >= 26 595 template <size_t _Ip, class _Xp> 596 friend _LIBCPP_HIDE_FROM_ABI constexpr _Xp& get(complex<_Xp>&) noexcept; 597 598 template <size_t _Ip, class _Xp> 599 friend _LIBCPP_HIDE_FROM_ABI constexpr _Xp&& get(complex<_Xp>&&) noexcept; 600 601 template <size_t _Ip, class _Xp> 602 friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp& get(const complex<_Xp>&) noexcept; 603 604 template <size_t _Ip, class _Xp> 605 friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp&& get(const complex<_Xp>&&) noexcept; 606#endif 607}; 608 609template <> 610class _LIBCPP_TEMPLATE_VIS complex<long double> { 611 long double __re_; 612 long double __im_; 613 614public: 615 typedef long double value_type; 616 617 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(long double __re = 0.0L, long double __im = 0.0L) 618 : __re_(__re), __im_(__im) {} 619 620 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(__from_builtin_tag, _Complex long double __v) 621 : __re_(__real__ __v), __im_(__imag__ __v) {} 622 623 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(const complex<float>& __c); 624 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR complex(const complex<double>& __c); 625 626 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR long double real() const { return __re_; } 627 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR long double imag() const { return __im_; } 628 629 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) { __re_ = __re; } 630 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) { __im_ = __im; } 631 632 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Complex long double __builtin() const { 633 return std::__make_complex(__re_, __im_); 634 } 635 636 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __builtin(_Complex long double __f) { 637 __re_ = __real__ __f; 638 __im_ = __imag__ __f; 639 } 640 641 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(long double __re) { 642 __re_ = __re; 643 __im_ = value_type(); 644 return *this; 645 } 646 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(long double __re) { 647 __re_ += __re; 648 return *this; 649 } 650 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(long double __re) { 651 __re_ -= __re; 652 return *this; 653 } 654 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(long double __re) { 655 __re_ *= __re; 656 __im_ *= __re; 657 return *this; 658 } 659 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(long double __re) { 660 __re_ /= __re; 661 __im_ /= __re; 662 return *this; 663 } 664 665 template <class _Xp> 666 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator=(const complex<_Xp>& __c) { 667 __re_ = __c.real(); 668 __im_ = __c.imag(); 669 return *this; 670 } 671 template <class _Xp> 672 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c) { 673 __re_ += __c.real(); 674 __im_ += __c.imag(); 675 return *this; 676 } 677 template <class _Xp> 678 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c) { 679 __re_ -= __c.real(); 680 __im_ -= __c.imag(); 681 return *this; 682 } 683 template <class _Xp> 684 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c) { 685 *this = *this * complex(__c.real(), __c.imag()); 686 return *this; 687 } 688 template <class _Xp> 689 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c) { 690 *this = *this / complex(__c.real(), __c.imag()); 691 return *this; 692 } 693 694#if _LIBCPP_STD_VER >= 26 695 template <size_t _Ip, class _Xp> 696 friend _LIBCPP_HIDE_FROM_ABI constexpr _Xp& get(complex<_Xp>&) noexcept; 697 698 template <size_t _Ip, class _Xp> 699 friend _LIBCPP_HIDE_FROM_ABI constexpr _Xp&& get(complex<_Xp>&&) noexcept; 700 701 template <size_t _Ip, class _Xp> 702 friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp& get(const complex<_Xp>&) noexcept; 703 704 template <size_t _Ip, class _Xp> 705 friend _LIBCPP_HIDE_FROM_ABI constexpr const _Xp&& get(const complex<_Xp>&&) noexcept; 706#endif 707}; 708 709inline _LIBCPP_CONSTEXPR complex<float>::complex(const complex<double>& __c) : __re_(__c.real()), __im_(__c.imag()) {} 710 711inline _LIBCPP_CONSTEXPR complex<float>::complex(const complex<long double>& __c) 712 : __re_(__c.real()), __im_(__c.imag()) {} 713 714inline _LIBCPP_CONSTEXPR complex<double>::complex(const complex<float>& __c) : __re_(__c.real()), __im_(__c.imag()) {} 715 716inline _LIBCPP_CONSTEXPR complex<double>::complex(const complex<long double>& __c) 717 : __re_(__c.real()), __im_(__c.imag()) {} 718 719inline _LIBCPP_CONSTEXPR complex<long double>::complex(const complex<float>& __c) 720 : __re_(__c.real()), __im_(__c.imag()) {} 721 722inline _LIBCPP_CONSTEXPR complex<long double>::complex(const complex<double>& __c) 723 : __re_(__c.real()), __im_(__c.imag()) {} 724 725// 26.3.6 operators: 726 727template <class _Tp> 728inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> 729operator+(const complex<_Tp>& __x, const complex<_Tp>& __y) { 730 complex<_Tp> __t(__x); 731 __t += __y; 732 return __t; 733} 734 735template <class _Tp> 736inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> 737operator+(const complex<_Tp>& __x, const _Tp& __y) { 738 complex<_Tp> __t(__x); 739 __t += __y; 740 return __t; 741} 742 743template <class _Tp> 744inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> 745operator+(const _Tp& __x, const complex<_Tp>& __y) { 746 complex<_Tp> __t(__y); 747 __t += __x; 748 return __t; 749} 750 751template <class _Tp> 752inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> 753operator-(const complex<_Tp>& __x, const complex<_Tp>& __y) { 754 complex<_Tp> __t(__x); 755 __t -= __y; 756 return __t; 757} 758 759template <class _Tp> 760inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> 761operator-(const complex<_Tp>& __x, const _Tp& __y) { 762 complex<_Tp> __t(__x); 763 __t -= __y; 764 return __t; 765} 766 767template <class _Tp> 768inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> 769operator-(const _Tp& __x, const complex<_Tp>& __y) { 770 complex<_Tp> __t(-__y); 771 __t += __x; 772 return __t; 773} 774 775template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> > 776_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> 777operator*(const complex<_Tp>& __lhs, const complex<_Tp>& __rhs) { 778 return complex<_Tp>(__from_builtin_tag(), __lhs.__builtin() * __rhs.__builtin()); 779} 780 781template <class _Tp, __enable_if_t<!is_floating_point<_Tp>::value, int> > 782_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> 783operator*(const complex<_Tp>& __z, const complex<_Tp>& __w) { 784 _Tp __a = __z.real(); 785 _Tp __b = __z.imag(); 786 _Tp __c = __w.real(); 787 _Tp __d = __w.imag(); 788 789 return complex<_Tp>((__a * __c) - (__b * __d), (__a * __d) + (__b * __c)); 790} 791 792template <class _Tp> 793inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> 794operator*(const complex<_Tp>& __x, const _Tp& __y) { 795 complex<_Tp> __t(__x); 796 __t *= __y; 797 return __t; 798} 799 800template <class _Tp> 801inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> 802operator*(const _Tp& __x, const complex<_Tp>& __y) { 803 complex<_Tp> __t(__y); 804 __t *= __x; 805 return __t; 806} 807 808template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> > 809_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> 810operator/(const complex<_Tp>& __lhs, const complex<_Tp>& __rhs) { 811 return complex<_Tp>(__from_builtin_tag(), __lhs.__builtin() / __rhs.__builtin()); 812} 813 814template <class _Tp, __enable_if_t<!is_floating_point<_Tp>::value, int> > 815_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> 816operator/(const complex<_Tp>& __z, const complex<_Tp>& __w) { 817 _Tp __a = __z.real(); 818 _Tp __b = __z.imag(); 819 _Tp __c = __w.real(); 820 _Tp __d = __w.imag(); 821 822 _Tp __denom = __c * __c + __d * __d; 823 return complex<_Tp>((__a * __c + __b * __d) / __denom, (__b * __c - __a * __d) / __denom); 824} 825 826template <class _Tp> 827inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> 828operator/(const complex<_Tp>& __x, const _Tp& __y) { 829 return complex<_Tp>(__x.real() / __y, __x.imag() / __y); 830} 831 832template <class _Tp> 833inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> 834operator/(const _Tp& __x, const complex<_Tp>& __y) { 835 complex<_Tp> __t(__x); 836 __t /= __y; 837 return __t; 838} 839 840template <class _Tp> 841inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> operator+(const complex<_Tp>& __x) { 842 return __x; 843} 844 845template <class _Tp> 846inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> operator-(const complex<_Tp>& __x) { 847 return complex<_Tp>(-__x.real(), -__x.imag()); 848} 849 850template <class _Tp> 851inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool 852operator==(const complex<_Tp>& __x, const complex<_Tp>& __y) { 853 return __x.real() == __y.real() && __x.imag() == __y.imag(); 854} 855 856template <class _Tp> 857inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator==(const complex<_Tp>& __x, const _Tp& __y) { 858 return __x.real() == __y && __x.imag() == 0; 859} 860 861#if _LIBCPP_STD_VER <= 17 862 863template <class _Tp> 864inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator==(const _Tp& __x, const complex<_Tp>& __y) { 865 return __x == __y.real() && 0 == __y.imag(); 866} 867 868template <class _Tp> 869inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool 870operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y) { 871 return !(__x == __y); 872} 873 874template <class _Tp> 875inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator!=(const complex<_Tp>& __x, const _Tp& __y) { 876 return !(__x == __y); 877} 878 879template <class _Tp> 880inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool operator!=(const _Tp& __x, const complex<_Tp>& __y) { 881 return !(__x == __y); 882} 883 884#endif 885 886// 26.3.7 values: 887 888template <class _Tp, bool = is_integral<_Tp>::value, bool = is_floating_point<_Tp>::value > 889struct __libcpp_complex_overload_traits {}; 890 891// Integral Types 892template <class _Tp> 893struct __libcpp_complex_overload_traits<_Tp, true, false> { 894 typedef double _ValueType; 895 typedef complex<double> _ComplexType; 896}; 897 898// Floating point types 899template <class _Tp> 900struct __libcpp_complex_overload_traits<_Tp, false, true> { 901 typedef _Tp _ValueType; 902 typedef complex<_Tp> _ComplexType; 903}; 904 905// real 906 907template <class _Tp> 908inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp real(const complex<_Tp>& __c) { 909 return __c.real(); 910} 911 912template <class _Tp> 913inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename __libcpp_complex_overload_traits<_Tp>::_ValueType 914real(_Tp __re) { 915 return __re; 916} 917 918// imag 919 920template <class _Tp> 921inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp imag(const complex<_Tp>& __c) { 922 return __c.imag(); 923} 924 925template <class _Tp> 926inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename __libcpp_complex_overload_traits<_Tp>::_ValueType 927imag(_Tp) { 928 return 0; 929} 930 931// abs 932 933template <class _Tp> 934inline _LIBCPP_HIDE_FROM_ABI _Tp abs(const complex<_Tp>& __c) { 935 return std::hypot(__c.real(), __c.imag()); 936} 937 938// arg 939 940template <class _Tp> 941inline _LIBCPP_HIDE_FROM_ABI _Tp arg(const complex<_Tp>& __c) { 942 return std::atan2(__c.imag(), __c.real()); 943} 944 945template <class _Tp, __enable_if_t<is_same<_Tp, long double>::value, int> = 0> 946inline _LIBCPP_HIDE_FROM_ABI long double arg(_Tp __re) { 947 return std::atan2l(0.L, __re); 948} 949 950template <class _Tp, __enable_if_t<is_integral<_Tp>::value || is_same<_Tp, double>::value, int> = 0> 951inline _LIBCPP_HIDE_FROM_ABI double arg(_Tp __re) { 952 return std::atan2(0., __re); 953} 954 955template <class _Tp, __enable_if_t<is_same<_Tp, float>::value, int> = 0> 956inline _LIBCPP_HIDE_FROM_ABI float arg(_Tp __re) { 957 return std::atan2f(0.F, __re); 958} 959 960// norm 961 962template <class _Tp> 963inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp norm(const complex<_Tp>& __c) { 964 if (std::__constexpr_isinf(__c.real())) 965 return std::abs(__c.real()); 966 if (std::__constexpr_isinf(__c.imag())) 967 return std::abs(__c.imag()); 968 return __c.real() * __c.real() + __c.imag() * __c.imag(); 969} 970 971template <class _Tp> 972inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __libcpp_complex_overload_traits<_Tp>::_ValueType 973norm(_Tp __re) { 974 typedef typename __libcpp_complex_overload_traits<_Tp>::_ValueType _ValueType; 975 return static_cast<_ValueType>(__re) * __re; 976} 977 978// conj 979 980template <class _Tp> 981inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> conj(const complex<_Tp>& __c) { 982 return complex<_Tp>(__c.real(), -__c.imag()); 983} 984 985template <class _Tp> 986inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType 987conj(_Tp __re) { 988 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType; 989 return _ComplexType(__re); 990} 991 992// proj 993 994template <class _Tp> 995inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> proj(const complex<_Tp>& __c) { 996 complex<_Tp> __r = __c; 997 if (std::__constexpr_isinf(__c.real()) || std::__constexpr_isinf(__c.imag())) 998 __r = complex<_Tp>(INFINITY, std::copysign(_Tp(0), __c.imag())); 999 return __r; 1000} 1001 1002template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> = 0> 1003inline _LIBCPP_HIDE_FROM_ABI typename __libcpp_complex_overload_traits<_Tp>::_ComplexType proj(_Tp __re) { 1004 if (std::__constexpr_isinf(__re)) 1005 __re = std::abs(__re); 1006 return complex<_Tp>(__re); 1007} 1008 1009template <class _Tp, __enable_if_t<is_integral<_Tp>::value, int> = 0> 1010inline _LIBCPP_HIDE_FROM_ABI typename __libcpp_complex_overload_traits<_Tp>::_ComplexType proj(_Tp __re) { 1011 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType; 1012 return _ComplexType(__re); 1013} 1014 1015// polar 1016 1017template <class _Tp> 1018_LIBCPP_HIDE_FROM_ABI complex<_Tp> polar(const _Tp& __rho, const _Tp& __theta = _Tp()) { 1019 if (std::__constexpr_isnan(__rho) || std::signbit(__rho)) 1020 return complex<_Tp>(_Tp(NAN), _Tp(NAN)); 1021 if (std::__constexpr_isnan(__theta)) { 1022 if (std::__constexpr_isinf(__rho)) 1023 return complex<_Tp>(__rho, __theta); 1024 return complex<_Tp>(__theta, __theta); 1025 } 1026 if (std::__constexpr_isinf(__theta)) { 1027 if (std::__constexpr_isinf(__rho)) 1028 return complex<_Tp>(__rho, _Tp(NAN)); 1029 return complex<_Tp>(_Tp(NAN), _Tp(NAN)); 1030 } 1031 _Tp __x = __rho * std::cos(__theta); 1032 if (std::__constexpr_isnan(__x)) 1033 __x = 0; 1034 _Tp __y = __rho * std::sin(__theta); 1035 if (std::__constexpr_isnan(__y)) 1036 __y = 0; 1037 return complex<_Tp>(__x, __y); 1038} 1039 1040// log 1041 1042template <class _Tp> 1043inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> log(const complex<_Tp>& __x) { 1044 return complex<_Tp>(std::log(std::abs(__x)), std::arg(__x)); 1045} 1046 1047// log10 1048 1049template <class _Tp> 1050inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> log10(const complex<_Tp>& __x) { 1051 return std::log(__x) / std::log(_Tp(10)); 1052} 1053 1054// sqrt 1055 1056template <class _Tp> 1057_LIBCPP_HIDE_FROM_ABI complex<_Tp> sqrt(const complex<_Tp>& __x) { 1058 if (std::__constexpr_isinf(__x.imag())) 1059 return complex<_Tp>(_Tp(INFINITY), __x.imag()); 1060 if (std::__constexpr_isinf(__x.real())) { 1061 if (__x.real() > _Tp(0)) 1062 return complex<_Tp>( 1063 __x.real(), std::__constexpr_isnan(__x.imag()) ? __x.imag() : std::copysign(_Tp(0), __x.imag())); 1064 return complex<_Tp>( 1065 std::__constexpr_isnan(__x.imag()) ? __x.imag() : _Tp(0), std::copysign(__x.real(), __x.imag())); 1066 } 1067 return std::polar(std::sqrt(std::abs(__x)), std::arg(__x) / _Tp(2)); 1068} 1069 1070// exp 1071 1072template <class _Tp> 1073_LIBCPP_HIDE_FROM_ABI complex<_Tp> exp(const complex<_Tp>& __x) { 1074 _Tp __i = __x.imag(); 1075 if (__i == 0) { 1076 return complex<_Tp>(std::exp(__x.real()), std::copysign(_Tp(0), __x.imag())); 1077 } 1078 if (std::__constexpr_isinf(__x.real())) { 1079 if (__x.real() < _Tp(0)) { 1080 if (!std::__constexpr_isfinite(__i)) 1081 __i = _Tp(1); 1082 } else if (__i == 0 || !std::__constexpr_isfinite(__i)) { 1083 if (std::__constexpr_isinf(__i)) 1084 __i = _Tp(NAN); 1085 return complex<_Tp>(__x.real(), __i); 1086 } 1087 } 1088 _Tp __e = std::exp(__x.real()); 1089 return complex<_Tp>(__e * std::cos(__i), __e * std::sin(__i)); 1090} 1091 1092// pow 1093 1094template <class _Tp> 1095inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> pow(const complex<_Tp>& __x, const complex<_Tp>& __y) { 1096 return std::exp(__y * std::log(__x)); 1097} 1098 1099template <class _Tp, class _Up> 1100inline _LIBCPP_HIDE_FROM_ABI complex<typename __promote<_Tp, _Up>::type> 1101pow(const complex<_Tp>& __x, const complex<_Up>& __y) { 1102 typedef complex<typename __promote<_Tp, _Up>::type> result_type; 1103 return std::pow(result_type(__x), result_type(__y)); 1104} 1105 1106template <class _Tp, class _Up, __enable_if_t<is_arithmetic<_Up>::value, int> = 0> 1107inline _LIBCPP_HIDE_FROM_ABI complex<typename __promote<_Tp, _Up>::type> pow(const complex<_Tp>& __x, const _Up& __y) { 1108 typedef complex<typename __promote<_Tp, _Up>::type> result_type; 1109 return std::pow(result_type(__x), result_type(__y)); 1110} 1111 1112template <class _Tp, class _Up, __enable_if_t<is_arithmetic<_Tp>::value, int> = 0> 1113inline _LIBCPP_HIDE_FROM_ABI complex<typename __promote<_Tp, _Up>::type> pow(const _Tp& __x, const complex<_Up>& __y) { 1114 typedef complex<typename __promote<_Tp, _Up>::type> result_type; 1115 return std::pow(result_type(__x), result_type(__y)); 1116} 1117 1118// __sqr, computes pow(x, 2) 1119 1120template <class _Tp> 1121inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> __sqr(const complex<_Tp>& __x) { 1122 return complex<_Tp>((__x.real() - __x.imag()) * (__x.real() + __x.imag()), _Tp(2) * __x.real() * __x.imag()); 1123} 1124 1125// asinh 1126 1127template <class _Tp> 1128_LIBCPP_HIDE_FROM_ABI complex<_Tp> asinh(const complex<_Tp>& __x) { 1129 const _Tp __pi(atan2(+0., -0.)); 1130 if (std::__constexpr_isinf(__x.real())) { 1131 if (std::__constexpr_isnan(__x.imag())) 1132 return __x; 1133 if (std::__constexpr_isinf(__x.imag())) 1134 return complex<_Tp>(__x.real(), std::copysign(__pi * _Tp(0.25), __x.imag())); 1135 return complex<_Tp>(__x.real(), std::copysign(_Tp(0), __x.imag())); 1136 } 1137 if (std::__constexpr_isnan(__x.real())) { 1138 if (std::__constexpr_isinf(__x.imag())) 1139 return complex<_Tp>(__x.imag(), __x.real()); 1140 if (__x.imag() == 0) 1141 return __x; 1142 return complex<_Tp>(__x.real(), __x.real()); 1143 } 1144 if (std::__constexpr_isinf(__x.imag())) 1145 return complex<_Tp>(std::copysign(__x.imag(), __x.real()), std::copysign(__pi / _Tp(2), __x.imag())); 1146 complex<_Tp> __z = std::log(__x + std::sqrt(std::__sqr(__x) + _Tp(1))); 1147 return complex<_Tp>(std::copysign(__z.real(), __x.real()), std::copysign(__z.imag(), __x.imag())); 1148} 1149 1150// acosh 1151 1152template <class _Tp> 1153_LIBCPP_HIDE_FROM_ABI complex<_Tp> acosh(const complex<_Tp>& __x) { 1154 const _Tp __pi(atan2(+0., -0.)); 1155 if (std::__constexpr_isinf(__x.real())) { 1156 if (std::__constexpr_isnan(__x.imag())) 1157 return complex<_Tp>(std::abs(__x.real()), __x.imag()); 1158 if (std::__constexpr_isinf(__x.imag())) { 1159 if (__x.real() > 0) 1160 return complex<_Tp>(__x.real(), std::copysign(__pi * _Tp(0.25), __x.imag())); 1161 else 1162 return complex<_Tp>(-__x.real(), std::copysign(__pi * _Tp(0.75), __x.imag())); 1163 } 1164 if (__x.real() < 0) 1165 return complex<_Tp>(-__x.real(), std::copysign(__pi, __x.imag())); 1166 return complex<_Tp>(__x.real(), std::copysign(_Tp(0), __x.imag())); 1167 } 1168 if (std::__constexpr_isnan(__x.real())) { 1169 if (std::__constexpr_isinf(__x.imag())) 1170 return complex<_Tp>(std::abs(__x.imag()), __x.real()); 1171 return complex<_Tp>(__x.real(), __x.real()); 1172 } 1173 if (std::__constexpr_isinf(__x.imag())) 1174 return complex<_Tp>(std::abs(__x.imag()), std::copysign(__pi / _Tp(2), __x.imag())); 1175 complex<_Tp> __z = std::log(__x + std::sqrt(std::__sqr(__x) - _Tp(1))); 1176 return complex<_Tp>(std::copysign(__z.real(), _Tp(0)), std::copysign(__z.imag(), __x.imag())); 1177} 1178 1179// atanh 1180 1181template <class _Tp> 1182_LIBCPP_HIDE_FROM_ABI complex<_Tp> atanh(const complex<_Tp>& __x) { 1183 const _Tp __pi(atan2(+0., -0.)); 1184 if (std::__constexpr_isinf(__x.imag())) { 1185 return complex<_Tp>(std::copysign(_Tp(0), __x.real()), std::copysign(__pi / _Tp(2), __x.imag())); 1186 } 1187 if (std::__constexpr_isnan(__x.imag())) { 1188 if (std::__constexpr_isinf(__x.real()) || __x.real() == 0) 1189 return complex<_Tp>(std::copysign(_Tp(0), __x.real()), __x.imag()); 1190 return complex<_Tp>(__x.imag(), __x.imag()); 1191 } 1192 if (std::__constexpr_isnan(__x.real())) { 1193 return complex<_Tp>(__x.real(), __x.real()); 1194 } 1195 if (std::__constexpr_isinf(__x.real())) { 1196 return complex<_Tp>(std::copysign(_Tp(0), __x.real()), std::copysign(__pi / _Tp(2), __x.imag())); 1197 } 1198 if (std::abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0)) { 1199 return complex<_Tp>(std::copysign(_Tp(INFINITY), __x.real()), std::copysign(_Tp(0), __x.imag())); 1200 } 1201 complex<_Tp> __z = std::log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2); 1202 return complex<_Tp>(std::copysign(__z.real(), __x.real()), std::copysign(__z.imag(), __x.imag())); 1203} 1204 1205// sinh 1206 1207template <class _Tp> 1208_LIBCPP_HIDE_FROM_ABI complex<_Tp> sinh(const complex<_Tp>& __x) { 1209 if (std::__constexpr_isinf(__x.real()) && !std::__constexpr_isfinite(__x.imag())) 1210 return complex<_Tp>(__x.real(), _Tp(NAN)); 1211 if (__x.real() == 0 && !std::__constexpr_isfinite(__x.imag())) 1212 return complex<_Tp>(__x.real(), _Tp(NAN)); 1213 if (__x.imag() == 0 && !std::__constexpr_isfinite(__x.real())) 1214 return __x; 1215 return complex<_Tp>(std::sinh(__x.real()) * std::cos(__x.imag()), std::cosh(__x.real()) * std::sin(__x.imag())); 1216} 1217 1218// cosh 1219 1220template <class _Tp> 1221_LIBCPP_HIDE_FROM_ABI complex<_Tp> cosh(const complex<_Tp>& __x) { 1222 if (std::__constexpr_isinf(__x.real()) && !std::__constexpr_isfinite(__x.imag())) 1223 return complex<_Tp>(std::abs(__x.real()), _Tp(NAN)); 1224 if (__x.real() == 0 && !std::__constexpr_isfinite(__x.imag())) 1225 return complex<_Tp>(_Tp(NAN), __x.real()); 1226 if (__x.real() == 0 && __x.imag() == 0) 1227 return complex<_Tp>(_Tp(1), __x.imag()); 1228 if (__x.imag() == 0 && !std::__constexpr_isfinite(__x.real())) 1229 return complex<_Tp>(std::abs(__x.real()), __x.imag()); 1230 return complex<_Tp>(std::cosh(__x.real()) * std::cos(__x.imag()), std::sinh(__x.real()) * std::sin(__x.imag())); 1231} 1232 1233// tanh 1234 1235template <class _Tp> 1236_LIBCPP_HIDE_FROM_ABI complex<_Tp> tanh(const complex<_Tp>& __x) { 1237 if (std::__constexpr_isinf(__x.real())) { 1238 if (!std::__constexpr_isfinite(__x.imag())) 1239 return complex<_Tp>(std::copysign(_Tp(1), __x.real()), _Tp(0)); 1240 return complex<_Tp>(std::copysign(_Tp(1), __x.real()), std::copysign(_Tp(0), std::sin(_Tp(2) * __x.imag()))); 1241 } 1242 if (std::__constexpr_isnan(__x.real()) && __x.imag() == 0) 1243 return __x; 1244 _Tp __2r(_Tp(2) * __x.real()); 1245 _Tp __2i(_Tp(2) * __x.imag()); 1246 _Tp __d(std::cosh(__2r) + std::cos(__2i)); 1247 _Tp __2rsh(std::sinh(__2r)); 1248 if (std::__constexpr_isinf(__2rsh) && std::__constexpr_isinf(__d)) 1249 return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1), __2i > _Tp(0) ? _Tp(0) : _Tp(-0.)); 1250 return complex<_Tp>(__2rsh / __d, std::sin(__2i) / __d); 1251} 1252 1253// asin 1254 1255template <class _Tp> 1256_LIBCPP_HIDE_FROM_ABI complex<_Tp> asin(const complex<_Tp>& __x) { 1257 complex<_Tp> __z = std::asinh(complex<_Tp>(-__x.imag(), __x.real())); 1258 return complex<_Tp>(__z.imag(), -__z.real()); 1259} 1260 1261// acos 1262 1263template <class _Tp> 1264_LIBCPP_HIDE_FROM_ABI complex<_Tp> acos(const complex<_Tp>& __x) { 1265 const _Tp __pi(atan2(+0., -0.)); 1266 if (std::__constexpr_isinf(__x.real())) { 1267 if (std::__constexpr_isnan(__x.imag())) 1268 return complex<_Tp>(__x.imag(), __x.real()); 1269 if (std::__constexpr_isinf(__x.imag())) { 1270 if (__x.real() < _Tp(0)) 1271 return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag()); 1272 return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag()); 1273 } 1274 if (__x.real() < _Tp(0)) 1275 return complex<_Tp>(__pi, std::signbit(__x.imag()) ? -__x.real() : __x.real()); 1276 return complex<_Tp>(_Tp(0), std::signbit(__x.imag()) ? __x.real() : -__x.real()); 1277 } 1278 if (std::__constexpr_isnan(__x.real())) { 1279 if (std::__constexpr_isinf(__x.imag())) 1280 return complex<_Tp>(__x.real(), -__x.imag()); 1281 return complex<_Tp>(__x.real(), __x.real()); 1282 } 1283 if (std::__constexpr_isinf(__x.imag())) 1284 return complex<_Tp>(__pi / _Tp(2), -__x.imag()); 1285 if (__x.real() == 0 && (__x.imag() == 0 || std::isnan(__x.imag()))) 1286 return complex<_Tp>(__pi / _Tp(2), -__x.imag()); 1287 complex<_Tp> __z = std::log(__x + std::sqrt(std::__sqr(__x) - _Tp(1))); 1288 if (std::signbit(__x.imag())) 1289 return complex<_Tp>(std::abs(__z.imag()), std::abs(__z.real())); 1290 return complex<_Tp>(std::abs(__z.imag()), -std::abs(__z.real())); 1291} 1292 1293// atan 1294 1295template <class _Tp> 1296_LIBCPP_HIDE_FROM_ABI complex<_Tp> atan(const complex<_Tp>& __x) { 1297 complex<_Tp> __z = std::atanh(complex<_Tp>(-__x.imag(), __x.real())); 1298 return complex<_Tp>(__z.imag(), -__z.real()); 1299} 1300 1301// sin 1302 1303template <class _Tp> 1304_LIBCPP_HIDE_FROM_ABI complex<_Tp> sin(const complex<_Tp>& __x) { 1305 complex<_Tp> __z = std::sinh(complex<_Tp>(-__x.imag(), __x.real())); 1306 return complex<_Tp>(__z.imag(), -__z.real()); 1307} 1308 1309// cos 1310 1311template <class _Tp> 1312inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> cos(const complex<_Tp>& __x) { 1313 return std::cosh(complex<_Tp>(-__x.imag(), __x.real())); 1314} 1315 1316// tan 1317 1318template <class _Tp> 1319_LIBCPP_HIDE_FROM_ABI complex<_Tp> tan(const complex<_Tp>& __x) { 1320 complex<_Tp> __z = std::tanh(complex<_Tp>(-__x.imag(), __x.real())); 1321 return complex<_Tp>(__z.imag(), -__z.real()); 1322} 1323 1324#if !defined(_LIBCPP_HAS_NO_LOCALIZATION) 1325template <class _Tp, class _CharT, class _Traits> 1326_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& 1327operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x) { 1328 if (__is.good()) { 1329 std::ws(__is); 1330 if (__is.peek() == _CharT('(')) { 1331 __is.get(); 1332 _Tp __r; 1333 __is >> __r; 1334 if (!__is.fail()) { 1335 std::ws(__is); 1336 _CharT __c = __is.peek(); 1337 if (__c == _CharT(',')) { 1338 __is.get(); 1339 _Tp __i; 1340 __is >> __i; 1341 if (!__is.fail()) { 1342 std::ws(__is); 1343 __c = __is.peek(); 1344 if (__c == _CharT(')')) { 1345 __is.get(); 1346 __x = complex<_Tp>(__r, __i); 1347 } else 1348 __is.setstate(__is.failbit); 1349 } else 1350 __is.setstate(__is.failbit); 1351 } else if (__c == _CharT(')')) { 1352 __is.get(); 1353 __x = complex<_Tp>(__r, _Tp(0)); 1354 } else 1355 __is.setstate(__is.failbit); 1356 } else 1357 __is.setstate(__is.failbit); 1358 } else { 1359 _Tp __r; 1360 __is >> __r; 1361 if (!__is.fail()) 1362 __x = complex<_Tp>(__r, _Tp(0)); 1363 else 1364 __is.setstate(__is.failbit); 1365 } 1366 } else 1367 __is.setstate(__is.failbit); 1368 return __is; 1369} 1370 1371template <class _Tp, class _CharT, class _Traits> 1372_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 1373operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) { 1374 basic_ostringstream<_CharT, _Traits> __s; 1375 __s.flags(__os.flags()); 1376 __s.imbue(__os.getloc()); 1377 __s.precision(__os.precision()); 1378 __s << '(' << __x.real() << ',' << __x.imag() << ')'; 1379 return __os << __s.str(); 1380} 1381#endif // !_LIBCPP_HAS_NO_LOCALIZATION 1382 1383#if _LIBCPP_STD_VER >= 26 1384 1385// [complex.tuple], tuple interface 1386 1387template <class _Tp> 1388struct tuple_size<complex<_Tp>> : integral_constant<size_t, 2> {}; 1389 1390template <size_t _Ip, class _Tp> 1391struct tuple_element<_Ip, complex<_Tp>> { 1392 static_assert(_Ip < 2, "Index value is out of range."); 1393 using type = _Tp; 1394}; 1395 1396template <size_t _Ip, class _Xp> 1397_LIBCPP_HIDE_FROM_ABI constexpr _Xp& get(complex<_Xp>& __z) noexcept { 1398 static_assert(_Ip < 2, "Index value is out of range."); 1399 if constexpr (_Ip == 0) { 1400 return __z.__re_; 1401 } else { 1402 return __z.__im_; 1403 } 1404} 1405 1406template <size_t _Ip, class _Xp> 1407_LIBCPP_HIDE_FROM_ABI constexpr _Xp&& get(complex<_Xp>&& __z) noexcept { 1408 static_assert(_Ip < 2, "Index value is out of range."); 1409 if constexpr (_Ip == 0) { 1410 return std::move(__z.__re_); 1411 } else { 1412 return std::move(__z.__im_); 1413 } 1414} 1415 1416template <size_t _Ip, class _Xp> 1417_LIBCPP_HIDE_FROM_ABI constexpr const _Xp& get(const complex<_Xp>& __z) noexcept { 1418 static_assert(_Ip < 2, "Index value is out of range."); 1419 if constexpr (_Ip == 0) { 1420 return __z.__re_; 1421 } else { 1422 return __z.__im_; 1423 } 1424} 1425 1426template <size_t _Ip, class _Xp> 1427_LIBCPP_HIDE_FROM_ABI constexpr const _Xp&& get(const complex<_Xp>&& __z) noexcept { 1428 static_assert(_Ip < 2, "Index value is out of range."); 1429 if constexpr (_Ip == 0) { 1430 return std::move(__z.__re_); 1431 } else { 1432 return std::move(__z.__im_); 1433 } 1434} 1435 1436#endif // _LIBCPP_STD_VER >= 26 1437 1438#if _LIBCPP_STD_VER >= 14 1439// Literal suffix for complex number literals [complex.literals] 1440inline namespace literals { 1441inline namespace complex_literals { 1442_LIBCPP_HIDE_FROM_ABI inline constexpr complex<long double> operator""il(long double __im) { return {0.0l, __im}; } 1443 1444_LIBCPP_HIDE_FROM_ABI inline constexpr complex<long double> operator""il(unsigned long long __im) { 1445 return {0.0l, static_cast<long double>(__im)}; 1446} 1447 1448_LIBCPP_HIDE_FROM_ABI inline constexpr complex<double> operator""i(long double __im) { 1449 return {0.0, static_cast<double>(__im)}; 1450} 1451 1452_LIBCPP_HIDE_FROM_ABI inline constexpr complex<double> operator""i(unsigned long long __im) { 1453 return {0.0, static_cast<double>(__im)}; 1454} 1455 1456_LIBCPP_HIDE_FROM_ABI inline constexpr complex<float> operator""if(long double __im) { 1457 return {0.0f, static_cast<float>(__im)}; 1458} 1459 1460_LIBCPP_HIDE_FROM_ABI inline constexpr complex<float> operator""if(unsigned long long __im) { 1461 return {0.0f, static_cast<float>(__im)}; 1462} 1463} // namespace complex_literals 1464} // namespace literals 1465#endif 1466 1467_LIBCPP_END_NAMESPACE_STD 1468 1469_LIBCPP_POP_MACROS 1470 1471#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 1472# include <iosfwd> 1473# include <stdexcept> 1474# include <type_traits> 1475#endif 1476 1477#endif // _LIBCPP_COMPLEX 1478