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