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