1299 lines · plain
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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP___CXX03_COMPLEX11#define _LIBCPP___CXX03_COMPLEX12 13/*14 complex synopsis15 16namespace std17{18 19template<class T>20class complex21{22public:23 typedef T value_type;24 25 complex(const T& re = T(), const T& im = T()); // constexpr in C++1426 complex(const complex&); // constexpr in C++1427 template<class X> complex(const complex<X>&); // constexpr in C++1428 29 T real() const; // constexpr in C++1430 T imag() const; // constexpr in C++1431 32 void real(T); // constexpr in C++2033 void imag(T); // constexpr in C++2034 35 complex<T>& operator= (const T&); // constexpr in C++2036 complex<T>& operator+=(const T&); // constexpr in C++2037 complex<T>& operator-=(const T&); // constexpr in C++2038 complex<T>& operator*=(const T&); // constexpr in C++2039 complex<T>& operator/=(const T&); // constexpr in C++2040 41 complex& operator=(const complex&); // constexpr in C++2042 template<class X> complex<T>& operator= (const complex<X>&); // constexpr in C++2043 template<class X> complex<T>& operator+=(const complex<X>&); // constexpr in C++2044 template<class X> complex<T>& operator-=(const complex<X>&); // constexpr in C++2045 template<class X> complex<T>& operator*=(const complex<X>&); // constexpr in C++2046 template<class X> complex<T>& operator/=(const complex<X>&); // constexpr in C++2047};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++2061 constexpr float imag() const;62 void imag(float); // constexpr in C++2063 64 complex<float>& operator= (float); // constexpr in C++2065 complex<float>& operator+=(float); // constexpr in C++2066 complex<float>& operator-=(float); // constexpr in C++2067 complex<float>& operator*=(float); // constexpr in C++2068 complex<float>& operator/=(float); // constexpr in C++2069 70 complex<float>& operator=(const complex<float>&); // constexpr in C++2071 template<class X> complex<float>& operator= (const complex<X>&); // constexpr in C++2072 template<class X> complex<float>& operator+=(const complex<X>&); // constexpr in C++2073 template<class X> complex<float>& operator-=(const complex<X>&); // constexpr in C++2074 template<class X> complex<float>& operator*=(const complex<X>&); // constexpr in C++2075 template<class X> complex<float>& operator/=(const complex<X>&); // constexpr in C++2076};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++2090 constexpr double imag() const;91 void imag(double); // constexpr in C++2092 93 complex<double>& operator= (double); // constexpr in C++2094 complex<double>& operator+=(double); // constexpr in C++2095 complex<double>& operator-=(double); // constexpr in C++2096 complex<double>& operator*=(double); // constexpr in C++2097 complex<double>& operator/=(double); // constexpr in C++2098 complex<double>& operator=(const complex<double>&); // constexpr in C++2099 100 template<class X> complex<double>& operator= (const complex<X>&); // constexpr in C++20101 template<class X> complex<double>& operator+=(const complex<X>&); // constexpr in C++20102 template<class X> complex<double>& operator-=(const complex<X>&); // constexpr in C++20103 template<class X> complex<double>& operator*=(const complex<X>&); // constexpr in C++20104 template<class X> complex<double>& operator/=(const complex<X>&); // constexpr in C++20105};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++20119 constexpr long double imag() const;120 void imag(long double); // constexpr in C++20121 122 complex<long double>& operator=(const complex<long double>&); // constexpr in C++20123 complex<long double>& operator= (long double); // constexpr in C++20124 complex<long double>& operator+=(long double); // constexpr in C++20125 complex<long double>& operator-=(long double); // constexpr in C++20126 complex<long double>& operator*=(long double); // constexpr in C++20127 complex<long double>& operator/=(long double); // constexpr in C++20128 129 template<class X> complex<long double>& operator= (const complex<X>&); // constexpr in C++20130 template<class X> complex<long double>& operator+=(const complex<X>&); // constexpr in C++20131 template<class X> complex<long double>& operator-=(const complex<X>&); // constexpr in C++20132 template<class X> complex<long double>& operator*=(const complex<X>&); // constexpr in C++20133 template<class X> complex<long double>& operator/=(const complex<X>&); // constexpr in C++20134};135 136// 26.3.6 operators:137template<class T> complex<T> operator+(const complex<T>&, const complex<T>&); // constexpr in C++20138template<class T> complex<T> operator+(const complex<T>&, const T&); // constexpr in C++20139template<class T> complex<T> operator+(const T&, const complex<T>&); // constexpr in C++20140template<class T> complex<T> operator-(const complex<T>&, const complex<T>&); // constexpr in C++20141template<class T> complex<T> operator-(const complex<T>&, const T&); // constexpr in C++20142template<class T> complex<T> operator-(const T&, const complex<T>&); // constexpr in C++20143template<class T> complex<T> operator*(const complex<T>&, const complex<T>&); // constexpr in C++20144template<class T> complex<T> operator*(const complex<T>&, const T&); // constexpr in C++20145template<class T> complex<T> operator*(const T&, const complex<T>&); // constexpr in C++20146template<class T> complex<T> operator/(const complex<T>&, const complex<T>&); // constexpr in C++20147template<class T> complex<T> operator/(const complex<T>&, const T&); // constexpr in C++20148template<class T> complex<T> operator/(const T&, const complex<T>&); // constexpr in C++20149template<class T> complex<T> operator+(const complex<T>&); // constexpr in C++20150template<class T> complex<T> operator-(const complex<T>&); // constexpr in C++20151template<class T> bool operator==(const complex<T>&, const complex<T>&); // constexpr in C++14152template<class T> bool operator==(const complex<T>&, const T&); // constexpr in C++14153template<class T> bool operator==(const T&, const complex<T>&); // constexpr in C++14, removed in C++20154template<class T> bool operator!=(const complex<T>&, const complex<T>&); // constexpr in C++14, removed in C++20155template<class T> bool operator!=(const complex<T>&, const T&); // constexpr in C++14, removed in C++20156template<class T> bool operator!=(const T&, const complex<T>&); // constexpr in C++14, removed in C++20157 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++14168 long double real(long double); // constexpr in C++14169 double real(double); // constexpr in C++14170template<Integral T> double real(T); // constexpr in C++14171 float real(float); // constexpr in C++14172 173template<class T> T imag(const complex<T>&); // constexpr in C++14174 long double imag(long double); // constexpr in C++14175 double imag(double); // constexpr in C++14176template<Integral T> double imag(T); // constexpr in C++14177 float imag(float); // constexpr in C++14178 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++20188 long double norm(long double); // constexpr in C++20189 double norm(double); // constexpr in C++20190template<Integral T> double norm(T); // constexpr in C++20191 float norm(float); // constexpr in C++20192 193template<class T> complex<T> conj(const complex<T>&); // constexpr in C++20194 complex<long double> conj(long double); // constexpr in C++20195 complex<double> conj(double); // constexpr in C++20196template<Integral T> complex<double> conj(T); // constexpr in C++20197 complex<float> conj(float); // constexpr in C++20198 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 interface231 template<class T> struct tuple_size; // Since C++26232 template<size_t I, class T> struct tuple_element; // Since C++26233 template<class T> struct tuple_size<complex<T>>; // Since C++26234 template<size_t I, class T> struct tuple_element<I, complex<T>>; // Since C++26235 template<size_t I, class T>236 constexpr T& get(complex<T>&) noexcept; // Since C++26237 template<size_t I, class T>238 constexpr T&& get(complex<T>&&) noexcept; // Since C++26239 template<size_t I, class T>240 constexpr const T& get(const complex<T>&) noexcept; // Since C++26241 template<size_t I, class T>242 constexpr const T&& get(const complex<T>&&) noexcept; // Since C++26243 244 // [complex.literals], complex literals245 inline namespace literals {246 inline namespace complex_literals {247 constexpr complex<long double> operator""il(long double); // Since C++14248 constexpr complex<long double> operator""il(unsigned long long); // Since C++14249 constexpr complex<double> operator""i(long double); // Since C++14250 constexpr complex<double> operator""i(unsigned long long); // Since C++14251 constexpr complex<float> operator""if(long double); // Since C++14252 constexpr complex<float> operator""if(unsigned long long); // Since C++14253 }254 }255} // std256 257*/258 259#include <__cxx03/__config>260#include <__cxx03/__fwd/complex.h>261#include <__cxx03/__fwd/tuple.h>262#include <__cxx03/__tuple/tuple_element.h>263#include <__cxx03/__tuple/tuple_size.h>264#include <__cxx03/__type_traits/conditional.h>265#include <__cxx03/__utility/move.h>266#include <__cxx03/cmath>267#include <__cxx03/version>268 269#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)270# include <__cxx03/sstream> // for std::basic_ostringstream271#endif272 273#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)274# pragma GCC system_header275#endif276 277_LIBCPP_PUSH_MACROS278#include <__cxx03/__undef_macros>279 280_LIBCPP_BEGIN_NAMESPACE_STD281 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 complex<_Tp> operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);287 288template <class _Tp, __enable_if_t<!is_floating_point<_Tp>::value, int> = 0>289_LIBCPP_HIDE_FROM_ABI complex<_Tp> operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);290 291template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> = 0>292_LIBCPP_HIDE_FROM_ABI complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);293 294template <class _Tp, __enable_if_t<!is_floating_point<_Tp>::value, int> = 0>295_LIBCPP_HIDE_FROM_ABI complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);296 297template <class _Tp>298class _LIBCPP_TEMPLATE_VIS complex {299public:300 typedef _Tp value_type;301 302private:303 value_type __re_;304 value_type __im_;305 306public:307 _LIBCPP_HIDE_FROM_ABI complex(const value_type& __re = value_type(), const value_type& __im = value_type())308 : __re_(__re), __im_(__im) {}309 template <class _Xp>310 _LIBCPP_HIDE_FROM_ABI complex(const complex<_Xp>& __c) : __re_(__c.real()), __im_(__c.imag()) {}311 312 _LIBCPP_HIDE_FROM_ABI value_type real() const { return __re_; }313 _LIBCPP_HIDE_FROM_ABI value_type imag() const { return __im_; }314 315 _LIBCPP_HIDE_FROM_ABI void real(value_type __re) { __re_ = __re; }316 _LIBCPP_HIDE_FROM_ABI void imag(value_type __im) { __im_ = __im; }317 318 _LIBCPP_HIDE_FROM_ABI complex& operator=(const value_type& __re) {319 __re_ = __re;320 __im_ = value_type();321 return *this;322 }323 _LIBCPP_HIDE_FROM_ABI complex& operator+=(const value_type& __re) {324 __re_ += __re;325 return *this;326 }327 _LIBCPP_HIDE_FROM_ABI complex& operator-=(const value_type& __re) {328 __re_ -= __re;329 return *this;330 }331 _LIBCPP_HIDE_FROM_ABI complex& operator*=(const value_type& __re) {332 __re_ *= __re;333 __im_ *= __re;334 return *this;335 }336 _LIBCPP_HIDE_FROM_ABI complex& operator/=(const value_type& __re) {337 __re_ /= __re;338 __im_ /= __re;339 return *this;340 }341 342 template <class _Xp>343 _LIBCPP_HIDE_FROM_ABI complex& operator=(const complex<_Xp>& __c) {344 __re_ = __c.real();345 __im_ = __c.imag();346 return *this;347 }348 template <class _Xp>349 _LIBCPP_HIDE_FROM_ABI 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 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 complex& operator*=(const complex<_Xp>& __c) {362 *this = *this * complex(__c.real(), __c.imag());363 return *this;364 }365 template <class _Xp>366 _LIBCPP_HIDE_FROM_ABI complex& operator/=(const complex<_Xp>& __c) {367 *this = *this / complex(__c.real(), __c.imag());368 return *this;369 }370};371 372template <>373class _LIBCPP_TEMPLATE_VIS complex<double>;374template <>375class _LIBCPP_TEMPLATE_VIS complex<long double>;376 377struct __from_builtin_tag {};378 379template <class _Tp>380using __complex_t =381 __conditional_t<is_same<_Tp, float>::value,382 _Complex float,383 __conditional_t<is_same<_Tp, double>::value, _Complex double, _Complex long double> >;384 385template <class _Tp>386_LIBCPP_HIDE_FROM_ABI __complex_t<_Tp> __make_complex(_Tp __re, _Tp __im) {387#if __has_builtin(__builtin_complex)388 return __builtin_complex(__re, __im);389#else390 return __complex_t<_Tp>{__re, __im};391#endif392}393 394template <>395class _LIBCPP_TEMPLATE_VIS complex<float> {396 float __re_;397 float __im_;398 399public:400 typedef float value_type;401 402 _LIBCPP_HIDE_FROM_ABI complex(float __re = 0.0f, float __im = 0.0f) : __re_(__re), __im_(__im) {}403 404 template <class _Tag, __enable_if_t<_IsSame<_Tag, __from_builtin_tag>::value, int> = 0>405 _LIBCPP_HIDE_FROM_ABI explicit complex(_Tag, _Complex float __v) : __re_(__real__ __v), __im_(__imag__ __v) {}406 407 _LIBCPP_HIDE_FROM_ABI explicit complex(const complex<double>& __c);408 _LIBCPP_HIDE_FROM_ABI explicit complex(const complex<long double>& __c);409 410 _LIBCPP_HIDE_FROM_ABI float real() const { return __re_; }411 _LIBCPP_HIDE_FROM_ABI float imag() const { return __im_; }412 413 _LIBCPP_HIDE_FROM_ABI void real(value_type __re) { __re_ = __re; }414 _LIBCPP_HIDE_FROM_ABI void imag(value_type __im) { __im_ = __im; }415 416 _LIBCPP_HIDE_FROM_ABI _Complex float __builtin() const { return std::__make_complex(__re_, __im_); }417 _LIBCPP_HIDE_FROM_ABI void __builtin(_Complex float __f) {418 __re_ = __real__ __f;419 __im_ = __imag__ __f;420 }421 422 _LIBCPP_HIDE_FROM_ABI complex& operator=(float __re) {423 __re_ = __re;424 __im_ = value_type();425 return *this;426 }427 _LIBCPP_HIDE_FROM_ABI complex& operator+=(float __re) {428 __re_ += __re;429 return *this;430 }431 _LIBCPP_HIDE_FROM_ABI complex& operator-=(float __re) {432 __re_ -= __re;433 return *this;434 }435 _LIBCPP_HIDE_FROM_ABI complex& operator*=(float __re) {436 __re_ *= __re;437 __im_ *= __re;438 return *this;439 }440 _LIBCPP_HIDE_FROM_ABI complex& operator/=(float __re) {441 __re_ /= __re;442 __im_ /= __re;443 return *this;444 }445 446 template <class _Xp>447 _LIBCPP_HIDE_FROM_ABI complex& operator=(const complex<_Xp>& __c) {448 __re_ = __c.real();449 __im_ = __c.imag();450 return *this;451 }452 template <class _Xp>453 _LIBCPP_HIDE_FROM_ABI complex& operator+=(const complex<_Xp>& __c) {454 __re_ += __c.real();455 __im_ += __c.imag();456 return *this;457 }458 template <class _Xp>459 _LIBCPP_HIDE_FROM_ABI complex& operator-=(const complex<_Xp>& __c) {460 __re_ -= __c.real();461 __im_ -= __c.imag();462 return *this;463 }464 template <class _Xp>465 _LIBCPP_HIDE_FROM_ABI complex& operator*=(const complex<_Xp>& __c) {466 *this = *this * complex(__c.real(), __c.imag());467 return *this;468 }469 template <class _Xp>470 _LIBCPP_HIDE_FROM_ABI complex& operator/=(const complex<_Xp>& __c) {471 *this = *this / complex(__c.real(), __c.imag());472 return *this;473 }474};475 476template <>477class _LIBCPP_TEMPLATE_VIS complex<double> {478 double __re_;479 double __im_;480 481public:482 typedef double value_type;483 484 _LIBCPP_HIDE_FROM_ABI complex(double __re = 0.0, double __im = 0.0) : __re_(__re), __im_(__im) {}485 486 template <class _Tag, __enable_if_t<_IsSame<_Tag, __from_builtin_tag>::value, int> = 0>487 _LIBCPP_HIDE_FROM_ABI explicit complex(_Tag, _Complex double __v) : __re_(__real__ __v), __im_(__imag__ __v) {}488 489 _LIBCPP_HIDE_FROM_ABI complex(const complex<float>& __c);490 _LIBCPP_HIDE_FROM_ABI explicit complex(const complex<long double>& __c);491 492 _LIBCPP_HIDE_FROM_ABI double real() const { return __re_; }493 _LIBCPP_HIDE_FROM_ABI double imag() const { return __im_; }494 495 _LIBCPP_HIDE_FROM_ABI void real(value_type __re) { __re_ = __re; }496 _LIBCPP_HIDE_FROM_ABI void imag(value_type __im) { __im_ = __im; }497 498 _LIBCPP_HIDE_FROM_ABI _Complex double __builtin() const { return std::__make_complex(__re_, __im_); }499 500 _LIBCPP_HIDE_FROM_ABI void __builtin(_Complex double __f) {501 __re_ = __real__ __f;502 __im_ = __imag__ __f;503 }504 505 _LIBCPP_HIDE_FROM_ABI complex& operator=(double __re) {506 __re_ = __re;507 __im_ = value_type();508 return *this;509 }510 _LIBCPP_HIDE_FROM_ABI complex& operator+=(double __re) {511 __re_ += __re;512 return *this;513 }514 _LIBCPP_HIDE_FROM_ABI complex& operator-=(double __re) {515 __re_ -= __re;516 return *this;517 }518 _LIBCPP_HIDE_FROM_ABI complex& operator*=(double __re) {519 __re_ *= __re;520 __im_ *= __re;521 return *this;522 }523 _LIBCPP_HIDE_FROM_ABI complex& operator/=(double __re) {524 __re_ /= __re;525 __im_ /= __re;526 return *this;527 }528 529 template <class _Xp>530 _LIBCPP_HIDE_FROM_ABI complex& operator=(const complex<_Xp>& __c) {531 __re_ = __c.real();532 __im_ = __c.imag();533 return *this;534 }535 template <class _Xp>536 _LIBCPP_HIDE_FROM_ABI complex& operator+=(const complex<_Xp>& __c) {537 __re_ += __c.real();538 __im_ += __c.imag();539 return *this;540 }541 template <class _Xp>542 _LIBCPP_HIDE_FROM_ABI complex& operator-=(const complex<_Xp>& __c) {543 __re_ -= __c.real();544 __im_ -= __c.imag();545 return *this;546 }547 template <class _Xp>548 _LIBCPP_HIDE_FROM_ABI complex& operator*=(const complex<_Xp>& __c) {549 *this = *this * complex(__c.real(), __c.imag());550 return *this;551 }552 template <class _Xp>553 _LIBCPP_HIDE_FROM_ABI complex& operator/=(const complex<_Xp>& __c) {554 *this = *this / complex(__c.real(), __c.imag());555 return *this;556 }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 complex(long double __re = 0.0L, long double __im = 0.0L) : __re_(__re), __im_(__im) {}568 569 template <class _Tag, __enable_if_t<_IsSame<_Tag, __from_builtin_tag>::value, int> = 0>570 _LIBCPP_HIDE_FROM_ABI explicit complex(_Tag, _Complex long double __v) : __re_(__real__ __v), __im_(__imag__ __v) {}571 572 _LIBCPP_HIDE_FROM_ABI complex(const complex<float>& __c);573 _LIBCPP_HIDE_FROM_ABI complex(const complex<double>& __c);574 575 _LIBCPP_HIDE_FROM_ABI long double real() const { return __re_; }576 _LIBCPP_HIDE_FROM_ABI long double imag() const { return __im_; }577 578 _LIBCPP_HIDE_FROM_ABI void real(value_type __re) { __re_ = __re; }579 _LIBCPP_HIDE_FROM_ABI void imag(value_type __im) { __im_ = __im; }580 581 _LIBCPP_HIDE_FROM_ABI _Complex long double __builtin() const { return std::__make_complex(__re_, __im_); }582 583 _LIBCPP_HIDE_FROM_ABI void __builtin(_Complex long double __f) {584 __re_ = __real__ __f;585 __im_ = __imag__ __f;586 }587 588 _LIBCPP_HIDE_FROM_ABI complex& operator=(long double __re) {589 __re_ = __re;590 __im_ = value_type();591 return *this;592 }593 _LIBCPP_HIDE_FROM_ABI complex& operator+=(long double __re) {594 __re_ += __re;595 return *this;596 }597 _LIBCPP_HIDE_FROM_ABI complex& operator-=(long double __re) {598 __re_ -= __re;599 return *this;600 }601 _LIBCPP_HIDE_FROM_ABI complex& operator*=(long double __re) {602 __re_ *= __re;603 __im_ *= __re;604 return *this;605 }606 _LIBCPP_HIDE_FROM_ABI complex& operator/=(long double __re) {607 __re_ /= __re;608 __im_ /= __re;609 return *this;610 }611 612 template <class _Xp>613 _LIBCPP_HIDE_FROM_ABI complex& operator=(const complex<_Xp>& __c) {614 __re_ = __c.real();615 __im_ = __c.imag();616 return *this;617 }618 template <class _Xp>619 _LIBCPP_HIDE_FROM_ABI complex& operator+=(const complex<_Xp>& __c) {620 __re_ += __c.real();621 __im_ += __c.imag();622 return *this;623 }624 template <class _Xp>625 _LIBCPP_HIDE_FROM_ABI complex& operator-=(const complex<_Xp>& __c) {626 __re_ -= __c.real();627 __im_ -= __c.imag();628 return *this;629 }630 template <class _Xp>631 _LIBCPP_HIDE_FROM_ABI complex& operator*=(const complex<_Xp>& __c) {632 *this = *this * complex(__c.real(), __c.imag());633 return *this;634 }635 template <class _Xp>636 _LIBCPP_HIDE_FROM_ABI complex& operator/=(const complex<_Xp>& __c) {637 *this = *this / complex(__c.real(), __c.imag());638 return *this;639 }640};641 642inline complex<float>::complex(const complex<double>& __c) : __re_(__c.real()), __im_(__c.imag()) {}643 644inline complex<float>::complex(const complex<long double>& __c) : __re_(__c.real()), __im_(__c.imag()) {}645 646inline complex<double>::complex(const complex<float>& __c) : __re_(__c.real()), __im_(__c.imag()) {}647 648inline complex<double>::complex(const complex<long double>& __c) : __re_(__c.real()), __im_(__c.imag()) {}649 650inline complex<long double>::complex(const complex<float>& __c) : __re_(__c.real()), __im_(__c.imag()) {}651 652inline complex<long double>::complex(const complex<double>& __c) : __re_(__c.real()), __im_(__c.imag()) {}653 654// 26.3.6 operators:655 656template <class _Tp>657inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> operator+(const complex<_Tp>& __x, const complex<_Tp>& __y) {658 complex<_Tp> __t(__x);659 __t += __y;660 return __t;661}662 663template <class _Tp>664inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> operator+(const complex<_Tp>& __x, const _Tp& __y) {665 complex<_Tp> __t(__x);666 __t += __y;667 return __t;668}669 670template <class _Tp>671inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> operator+(const _Tp& __x, const complex<_Tp>& __y) {672 complex<_Tp> __t(__y);673 __t += __x;674 return __t;675}676 677template <class _Tp>678inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> operator-(const complex<_Tp>& __x, const complex<_Tp>& __y) {679 complex<_Tp> __t(__x);680 __t -= __y;681 return __t;682}683 684template <class _Tp>685inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> operator-(const complex<_Tp>& __x, const _Tp& __y) {686 complex<_Tp> __t(__x);687 __t -= __y;688 return __t;689}690 691template <class _Tp>692inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> operator-(const _Tp& __x, const complex<_Tp>& __y) {693 complex<_Tp> __t(-__y);694 __t += __x;695 return __t;696}697 698template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> >699_LIBCPP_HIDE_FROM_ABI complex<_Tp> operator*(const complex<_Tp>& __lhs, const complex<_Tp>& __rhs) {700 return complex<_Tp>(__from_builtin_tag(), __lhs.__builtin() * __rhs.__builtin());701}702 703template <class _Tp, __enable_if_t<!is_floating_point<_Tp>::value, int> >704_LIBCPP_HIDE_FROM_ABI complex<_Tp> operator*(const complex<_Tp>& __z, const complex<_Tp>& __w) {705 _Tp __a = __z.real();706 _Tp __b = __z.imag();707 _Tp __c = __w.real();708 _Tp __d = __w.imag();709 710 return complex<_Tp>((__a * __c) - (__b * __d), (__a * __d) + (__b * __c));711}712 713template <class _Tp>714inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> operator*(const complex<_Tp>& __x, const _Tp& __y) {715 complex<_Tp> __t(__x);716 __t *= __y;717 return __t;718}719 720template <class _Tp>721inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> operator*(const _Tp& __x, const complex<_Tp>& __y) {722 complex<_Tp> __t(__y);723 __t *= __x;724 return __t;725}726 727template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> >728_LIBCPP_HIDE_FROM_ABI complex<_Tp> operator/(const complex<_Tp>& __lhs, const complex<_Tp>& __rhs) {729 return complex<_Tp>(__from_builtin_tag(), __lhs.__builtin() / __rhs.__builtin());730}731 732template <class _Tp, __enable_if_t<!is_floating_point<_Tp>::value, int> >733_LIBCPP_HIDE_FROM_ABI complex<_Tp> operator/(const complex<_Tp>& __z, const complex<_Tp>& __w) {734 _Tp __a = __z.real();735 _Tp __b = __z.imag();736 _Tp __c = __w.real();737 _Tp __d = __w.imag();738 739 _Tp __denom = __c * __c + __d * __d;740 return complex<_Tp>((__a * __c + __b * __d) / __denom, (__b * __c - __a * __d) / __denom);741}742 743template <class _Tp>744inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> operator/(const complex<_Tp>& __x, const _Tp& __y) {745 return complex<_Tp>(__x.real() / __y, __x.imag() / __y);746}747 748template <class _Tp>749inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> operator/(const _Tp& __x, const complex<_Tp>& __y) {750 complex<_Tp> __t(__x);751 __t /= __y;752 return __t;753}754 755template <class _Tp>756inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> operator+(const complex<_Tp>& __x) {757 return __x;758}759 760template <class _Tp>761inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> operator-(const complex<_Tp>& __x) {762 return complex<_Tp>(-__x.real(), -__x.imag());763}764 765template <class _Tp>766inline _LIBCPP_HIDE_FROM_ABI bool operator==(const complex<_Tp>& __x, const complex<_Tp>& __y) {767 return __x.real() == __y.real() && __x.imag() == __y.imag();768}769 770template <class _Tp>771inline _LIBCPP_HIDE_FROM_ABI bool operator==(const complex<_Tp>& __x, const _Tp& __y) {772 return __x.real() == __y && __x.imag() == 0;773}774 775template <class _Tp>776inline _LIBCPP_HIDE_FROM_ABI bool operator==(const _Tp& __x, const complex<_Tp>& __y) {777 return __x == __y.real() && 0 == __y.imag();778}779 780template <class _Tp>781inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y) {782 return !(__x == __y);783}784 785template <class _Tp>786inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const complex<_Tp>& __x, const _Tp& __y) {787 return !(__x == __y);788}789 790template <class _Tp>791inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const _Tp& __x, const complex<_Tp>& __y) {792 return !(__x == __y);793}794 795// 26.3.7 values:796 797template <class _Tp, bool = is_integral<_Tp>::value, bool = is_floating_point<_Tp>::value >798struct __libcpp_complex_overload_traits {};799 800// Integral Types801template <class _Tp>802struct __libcpp_complex_overload_traits<_Tp, true, false> {803 typedef double _ValueType;804 typedef complex<double> _ComplexType;805};806 807// Floating point types808template <class _Tp>809struct __libcpp_complex_overload_traits<_Tp, false, true> {810 typedef _Tp _ValueType;811 typedef complex<_Tp> _ComplexType;812};813 814// real815 816template <class _Tp>817inline _LIBCPP_HIDE_FROM_ABI _Tp real(const complex<_Tp>& __c) {818 return __c.real();819}820 821template <class _Tp>822inline _LIBCPP_HIDE_FROM_ABI typename __libcpp_complex_overload_traits<_Tp>::_ValueType real(_Tp __re) {823 return __re;824}825 826// imag827 828template <class _Tp>829inline _LIBCPP_HIDE_FROM_ABI _Tp imag(const complex<_Tp>& __c) {830 return __c.imag();831}832 833template <class _Tp>834inline _LIBCPP_HIDE_FROM_ABI typename __libcpp_complex_overload_traits<_Tp>::_ValueType imag(_Tp) {835 return 0;836}837 838// abs839 840template <class _Tp>841inline _LIBCPP_HIDE_FROM_ABI _Tp abs(const complex<_Tp>& __c) {842 return std::hypot(__c.real(), __c.imag());843}844 845// arg846 847template <class _Tp>848inline _LIBCPP_HIDE_FROM_ABI _Tp arg(const complex<_Tp>& __c) {849 return std::atan2(__c.imag(), __c.real());850}851 852template <class _Tp, __enable_if_t<is_same<_Tp, long double>::value, int> = 0>853inline _LIBCPP_HIDE_FROM_ABI long double arg(_Tp __re) {854 return std::atan2l(0.L, __re);855}856 857template <class _Tp, __enable_if_t<is_integral<_Tp>::value || is_same<_Tp, double>::value, int> = 0>858inline _LIBCPP_HIDE_FROM_ABI double arg(_Tp __re) {859 return std::atan2(0., __re);860}861 862template <class _Tp, __enable_if_t<is_same<_Tp, float>::value, int> = 0>863inline _LIBCPP_HIDE_FROM_ABI float arg(_Tp __re) {864 return std::atan2f(0.F, __re);865}866 867// norm868 869template <class _Tp>870inline _LIBCPP_HIDE_FROM_ABI _Tp norm(const complex<_Tp>& __c) {871 if (std::__constexpr_isinf(__c.real()))872 return std::abs(__c.real());873 if (std::__constexpr_isinf(__c.imag()))874 return std::abs(__c.imag());875 return __c.real() * __c.real() + __c.imag() * __c.imag();876}877 878template <class _Tp>879inline _LIBCPP_HIDE_FROM_ABI typename __libcpp_complex_overload_traits<_Tp>::_ValueType norm(_Tp __re) {880 typedef typename __libcpp_complex_overload_traits<_Tp>::_ValueType _ValueType;881 return static_cast<_ValueType>(__re) * __re;882}883 884// conj885 886template <class _Tp>887inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> conj(const complex<_Tp>& __c) {888 return complex<_Tp>(__c.real(), -__c.imag());889}890 891template <class _Tp>892inline _LIBCPP_HIDE_FROM_ABI typename __libcpp_complex_overload_traits<_Tp>::_ComplexType conj(_Tp __re) {893 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;894 return _ComplexType(__re);895}896 897// proj898 899template <class _Tp>900inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> proj(const complex<_Tp>& __c) {901 complex<_Tp> __r = __c;902 if (std::__constexpr_isinf(__c.real()) || std::__constexpr_isinf(__c.imag()))903 __r = complex<_Tp>(INFINITY, std::copysign(_Tp(0), __c.imag()));904 return __r;905}906 907template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> = 0>908inline _LIBCPP_HIDE_FROM_ABI typename __libcpp_complex_overload_traits<_Tp>::_ComplexType proj(_Tp __re) {909 if (std::__constexpr_isinf(__re))910 __re = std::abs(__re);911 return complex<_Tp>(__re);912}913 914template <class _Tp, __enable_if_t<is_integral<_Tp>::value, int> = 0>915inline _LIBCPP_HIDE_FROM_ABI typename __libcpp_complex_overload_traits<_Tp>::_ComplexType proj(_Tp __re) {916 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;917 return _ComplexType(__re);918}919 920// polar921 922template <class _Tp>923_LIBCPP_HIDE_FROM_ABI complex<_Tp> polar(const _Tp& __rho, const _Tp& __theta = _Tp()) {924 if (std::__constexpr_isnan(__rho) || std::signbit(__rho))925 return complex<_Tp>(_Tp(NAN), _Tp(NAN));926 if (std::__constexpr_isnan(__theta)) {927 if (std::__constexpr_isinf(__rho))928 return complex<_Tp>(__rho, __theta);929 return complex<_Tp>(__theta, __theta);930 }931 if (std::__constexpr_isinf(__theta)) {932 if (std::__constexpr_isinf(__rho))933 return complex<_Tp>(__rho, _Tp(NAN));934 return complex<_Tp>(_Tp(NAN), _Tp(NAN));935 }936 _Tp __x = __rho * std::cos(__theta);937 if (std::__constexpr_isnan(__x))938 __x = 0;939 _Tp __y = __rho * std::sin(__theta);940 if (std::__constexpr_isnan(__y))941 __y = 0;942 return complex<_Tp>(__x, __y);943}944 945// log946 947template <class _Tp>948inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> log(const complex<_Tp>& __x) {949 return complex<_Tp>(std::log(std::abs(__x)), std::arg(__x));950}951 952// log10953 954template <class _Tp>955inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> log10(const complex<_Tp>& __x) {956 return std::log(__x) / std::log(_Tp(10));957}958 959// sqrt960 961template <class _Tp>962_LIBCPP_HIDE_FROM_ABI complex<_Tp> sqrt(const complex<_Tp>& __x) {963 if (std::__constexpr_isinf(__x.imag()))964 return complex<_Tp>(_Tp(INFINITY), __x.imag());965 if (std::__constexpr_isinf(__x.real())) {966 if (__x.real() > _Tp(0))967 return complex<_Tp>(968 __x.real(), std::__constexpr_isnan(__x.imag()) ? __x.imag() : std::copysign(_Tp(0), __x.imag()));969 return complex<_Tp>(970 std::__constexpr_isnan(__x.imag()) ? __x.imag() : _Tp(0), std::copysign(__x.real(), __x.imag()));971 }972 return std::polar(std::sqrt(std::abs(__x)), std::arg(__x) / _Tp(2));973}974 975// exp976 977template <class _Tp>978_LIBCPP_HIDE_FROM_ABI complex<_Tp> exp(const complex<_Tp>& __x) {979 _Tp __i = __x.imag();980 if (__i == 0) {981 return complex<_Tp>(std::exp(__x.real()), std::copysign(_Tp(0), __x.imag()));982 }983 if (std::__constexpr_isinf(__x.real())) {984 if (__x.real() < _Tp(0)) {985 if (!std::__constexpr_isfinite(__i))986 __i = _Tp(1);987 } else if (__i == 0 || !std::__constexpr_isfinite(__i)) {988 if (std::__constexpr_isinf(__i))989 __i = _Tp(NAN);990 return complex<_Tp>(__x.real(), __i);991 }992 }993 _Tp __e = std::exp(__x.real());994 return complex<_Tp>(__e * std::cos(__i), __e * std::sin(__i));995}996 997// pow998 999template <class _Tp>1000inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> pow(const complex<_Tp>& __x, const complex<_Tp>& __y) {1001 return std::exp(__y * std::log(__x));1002}1003 1004template <class _Tp, class _Up>1005inline _LIBCPP_HIDE_FROM_ABI complex<typename __promote<_Tp, _Up>::type>1006pow(const complex<_Tp>& __x, const complex<_Up>& __y) {1007 typedef complex<typename __promote<_Tp, _Up>::type> result_type;1008 return std::pow(result_type(__x), result_type(__y));1009}1010 1011template <class _Tp, class _Up, __enable_if_t<is_arithmetic<_Up>::value, int> = 0>1012inline _LIBCPP_HIDE_FROM_ABI complex<typename __promote<_Tp, _Up>::type> pow(const complex<_Tp>& __x, const _Up& __y) {1013 typedef complex<typename __promote<_Tp, _Up>::type> result_type;1014 return std::pow(result_type(__x), result_type(__y));1015}1016 1017template <class _Tp, class _Up, __enable_if_t<is_arithmetic<_Tp>::value, int> = 0>1018inline _LIBCPP_HIDE_FROM_ABI complex<typename __promote<_Tp, _Up>::type> pow(const _Tp& __x, const complex<_Up>& __y) {1019 typedef complex<typename __promote<_Tp, _Up>::type> result_type;1020 return std::pow(result_type(__x), result_type(__y));1021}1022 1023// __sqr, computes pow(x, 2)1024 1025template <class _Tp>1026inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> __sqr(const complex<_Tp>& __x) {1027 return complex<_Tp>((__x.real() - __x.imag()) * (__x.real() + __x.imag()), _Tp(2) * __x.real() * __x.imag());1028}1029 1030// asinh1031 1032template <class _Tp>1033_LIBCPP_HIDE_FROM_ABI complex<_Tp> asinh(const complex<_Tp>& __x) {1034 const _Tp __pi(atan2(+0., -0.));1035 if (std::__constexpr_isinf(__x.real())) {1036 if (std::__constexpr_isnan(__x.imag()))1037 return __x;1038 if (std::__constexpr_isinf(__x.imag()))1039 return complex<_Tp>(__x.real(), std::copysign(__pi * _Tp(0.25), __x.imag()));1040 return complex<_Tp>(__x.real(), std::copysign(_Tp(0), __x.imag()));1041 }1042 if (std::__constexpr_isnan(__x.real())) {1043 if (std::__constexpr_isinf(__x.imag()))1044 return complex<_Tp>(__x.imag(), __x.real());1045 if (__x.imag() == 0)1046 return __x;1047 return complex<_Tp>(__x.real(), __x.real());1048 }1049 if (std::__constexpr_isinf(__x.imag()))1050 return complex<_Tp>(std::copysign(__x.imag(), __x.real()), std::copysign(__pi / _Tp(2), __x.imag()));1051 complex<_Tp> __z = std::log(__x + std::sqrt(std::__sqr(__x) + _Tp(1)));1052 return complex<_Tp>(std::copysign(__z.real(), __x.real()), std::copysign(__z.imag(), __x.imag()));1053}1054 1055// acosh1056 1057template <class _Tp>1058_LIBCPP_HIDE_FROM_ABI complex<_Tp> acosh(const complex<_Tp>& __x) {1059 const _Tp __pi(atan2(+0., -0.));1060 if (std::__constexpr_isinf(__x.real())) {1061 if (std::__constexpr_isnan(__x.imag()))1062 return complex<_Tp>(std::abs(__x.real()), __x.imag());1063 if (std::__constexpr_isinf(__x.imag())) {1064 if (__x.real() > 0)1065 return complex<_Tp>(__x.real(), std::copysign(__pi * _Tp(0.25), __x.imag()));1066 else1067 return complex<_Tp>(-__x.real(), std::copysign(__pi * _Tp(0.75), __x.imag()));1068 }1069 if (__x.real() < 0)1070 return complex<_Tp>(-__x.real(), std::copysign(__pi, __x.imag()));1071 return complex<_Tp>(__x.real(), std::copysign(_Tp(0), __x.imag()));1072 }1073 if (std::__constexpr_isnan(__x.real())) {1074 if (std::__constexpr_isinf(__x.imag()))1075 return complex<_Tp>(std::abs(__x.imag()), __x.real());1076 return complex<_Tp>(__x.real(), __x.real());1077 }1078 if (std::__constexpr_isinf(__x.imag()))1079 return complex<_Tp>(std::abs(__x.imag()), std::copysign(__pi / _Tp(2), __x.imag()));1080 complex<_Tp> __z = std::log(__x + std::sqrt(std::__sqr(__x) - _Tp(1)));1081 return complex<_Tp>(std::copysign(__z.real(), _Tp(0)), std::copysign(__z.imag(), __x.imag()));1082}1083 1084// atanh1085 1086template <class _Tp>1087_LIBCPP_HIDE_FROM_ABI complex<_Tp> atanh(const complex<_Tp>& __x) {1088 const _Tp __pi(atan2(+0., -0.));1089 if (std::__constexpr_isinf(__x.imag())) {1090 return complex<_Tp>(std::copysign(_Tp(0), __x.real()), std::copysign(__pi / _Tp(2), __x.imag()));1091 }1092 if (std::__constexpr_isnan(__x.imag())) {1093 if (std::__constexpr_isinf(__x.real()) || __x.real() == 0)1094 return complex<_Tp>(std::copysign(_Tp(0), __x.real()), __x.imag());1095 return complex<_Tp>(__x.imag(), __x.imag());1096 }1097 if (std::__constexpr_isnan(__x.real())) {1098 return complex<_Tp>(__x.real(), __x.real());1099 }1100 if (std::__constexpr_isinf(__x.real())) {1101 return complex<_Tp>(std::copysign(_Tp(0), __x.real()), std::copysign(__pi / _Tp(2), __x.imag()));1102 }1103 if (std::abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0)) {1104 return complex<_Tp>(std::copysign(_Tp(INFINITY), __x.real()), std::copysign(_Tp(0), __x.imag()));1105 }1106 complex<_Tp> __z = std::log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2);1107 return complex<_Tp>(std::copysign(__z.real(), __x.real()), std::copysign(__z.imag(), __x.imag()));1108}1109 1110// sinh1111 1112template <class _Tp>1113_LIBCPP_HIDE_FROM_ABI complex<_Tp> sinh(const complex<_Tp>& __x) {1114 if (std::__constexpr_isinf(__x.real()) && !std::__constexpr_isfinite(__x.imag()))1115 return complex<_Tp>(__x.real(), _Tp(NAN));1116 if (__x.real() == 0 && !std::__constexpr_isfinite(__x.imag()))1117 return complex<_Tp>(__x.real(), _Tp(NAN));1118 if (__x.imag() == 0 && !std::__constexpr_isfinite(__x.real()))1119 return __x;1120 return complex<_Tp>(std::sinh(__x.real()) * std::cos(__x.imag()), std::cosh(__x.real()) * std::sin(__x.imag()));1121}1122 1123// cosh1124 1125template <class _Tp>1126_LIBCPP_HIDE_FROM_ABI complex<_Tp> cosh(const complex<_Tp>& __x) {1127 if (std::__constexpr_isinf(__x.real()) && !std::__constexpr_isfinite(__x.imag()))1128 return complex<_Tp>(std::abs(__x.real()), _Tp(NAN));1129 if (__x.real() == 0 && !std::__constexpr_isfinite(__x.imag()))1130 return complex<_Tp>(_Tp(NAN), __x.real());1131 if (__x.real() == 0 && __x.imag() == 0)1132 return complex<_Tp>(_Tp(1), __x.imag());1133 if (__x.imag() == 0 && !std::__constexpr_isfinite(__x.real()))1134 return complex<_Tp>(std::abs(__x.real()), __x.imag());1135 return complex<_Tp>(std::cosh(__x.real()) * std::cos(__x.imag()), std::sinh(__x.real()) * std::sin(__x.imag()));1136}1137 1138// tanh1139 1140template <class _Tp>1141_LIBCPP_HIDE_FROM_ABI complex<_Tp> tanh(const complex<_Tp>& __x) {1142 if (std::__constexpr_isinf(__x.real())) {1143 if (!std::__constexpr_isfinite(__x.imag()))1144 return complex<_Tp>(std::copysign(_Tp(1), __x.real()), _Tp(0));1145 return complex<_Tp>(std::copysign(_Tp(1), __x.real()), std::copysign(_Tp(0), std::sin(_Tp(2) * __x.imag())));1146 }1147 if (std::__constexpr_isnan(__x.real()) && __x.imag() == 0)1148 return __x;1149 _Tp __2r(_Tp(2) * __x.real());1150 _Tp __2i(_Tp(2) * __x.imag());1151 _Tp __d(std::cosh(__2r) + std::cos(__2i));1152 _Tp __2rsh(std::sinh(__2r));1153 if (std::__constexpr_isinf(__2rsh) && std::__constexpr_isinf(__d))1154 return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1), __2i > _Tp(0) ? _Tp(0) : _Tp(-0.));1155 return complex<_Tp>(__2rsh / __d, std::sin(__2i) / __d);1156}1157 1158// asin1159 1160template <class _Tp>1161_LIBCPP_HIDE_FROM_ABI complex<_Tp> asin(const complex<_Tp>& __x) {1162 complex<_Tp> __z = std::asinh(complex<_Tp>(-__x.imag(), __x.real()));1163 return complex<_Tp>(__z.imag(), -__z.real());1164}1165 1166// acos1167 1168template <class _Tp>1169_LIBCPP_HIDE_FROM_ABI complex<_Tp> acos(const complex<_Tp>& __x) {1170 const _Tp __pi(atan2(+0., -0.));1171 if (std::__constexpr_isinf(__x.real())) {1172 if (std::__constexpr_isnan(__x.imag()))1173 return complex<_Tp>(__x.imag(), __x.real());1174 if (std::__constexpr_isinf(__x.imag())) {1175 if (__x.real() < _Tp(0))1176 return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());1177 return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag());1178 }1179 if (__x.real() < _Tp(0))1180 return complex<_Tp>(__pi, std::signbit(__x.imag()) ? -__x.real() : __x.real());1181 return complex<_Tp>(_Tp(0), std::signbit(__x.imag()) ? __x.real() : -__x.real());1182 }1183 if (std::__constexpr_isnan(__x.real())) {1184 if (std::__constexpr_isinf(__x.imag()))1185 return complex<_Tp>(__x.real(), -__x.imag());1186 return complex<_Tp>(__x.real(), __x.real());1187 }1188 if (std::__constexpr_isinf(__x.imag()))1189 return complex<_Tp>(__pi / _Tp(2), -__x.imag());1190 if (__x.real() == 0 && (__x.imag() == 0 || std::isnan(__x.imag())))1191 return complex<_Tp>(__pi / _Tp(2), -__x.imag());1192 complex<_Tp> __z = std::log(__x + std::sqrt(std::__sqr(__x) - _Tp(1)));1193 if (std::signbit(__x.imag()))1194 return complex<_Tp>(std::abs(__z.imag()), std::abs(__z.real()));1195 return complex<_Tp>(std::abs(__z.imag()), -std::abs(__z.real()));1196}1197 1198// atan1199 1200template <class _Tp>1201_LIBCPP_HIDE_FROM_ABI complex<_Tp> atan(const complex<_Tp>& __x) {1202 complex<_Tp> __z = std::atanh(complex<_Tp>(-__x.imag(), __x.real()));1203 return complex<_Tp>(__z.imag(), -__z.real());1204}1205 1206// sin1207 1208template <class _Tp>1209_LIBCPP_HIDE_FROM_ABI complex<_Tp> sin(const complex<_Tp>& __x) {1210 complex<_Tp> __z = std::sinh(complex<_Tp>(-__x.imag(), __x.real()));1211 return complex<_Tp>(__z.imag(), -__z.real());1212}1213 1214// cos1215 1216template <class _Tp>1217inline _LIBCPP_HIDE_FROM_ABI complex<_Tp> cos(const complex<_Tp>& __x) {1218 return std::cosh(complex<_Tp>(-__x.imag(), __x.real()));1219}1220 1221// tan1222 1223template <class _Tp>1224_LIBCPP_HIDE_FROM_ABI complex<_Tp> tan(const complex<_Tp>& __x) {1225 complex<_Tp> __z = std::tanh(complex<_Tp>(-__x.imag(), __x.real()));1226 return complex<_Tp>(__z.imag(), -__z.real());1227}1228 1229#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)1230template <class _Tp, class _CharT, class _Traits>1231_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&1232operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x) {1233 if (__is.good()) {1234 std::ws(__is);1235 if (__is.peek() == _CharT('(')) {1236 __is.get();1237 _Tp __r;1238 __is >> __r;1239 if (!__is.fail()) {1240 std::ws(__is);1241 _CharT __c = __is.peek();1242 if (__c == _CharT(',')) {1243 __is.get();1244 _Tp __i;1245 __is >> __i;1246 if (!__is.fail()) {1247 std::ws(__is);1248 __c = __is.peek();1249 if (__c == _CharT(')')) {1250 __is.get();1251 __x = complex<_Tp>(__r, __i);1252 } else1253 __is.setstate(__is.failbit);1254 } else1255 __is.setstate(__is.failbit);1256 } else if (__c == _CharT(')')) {1257 __is.get();1258 __x = complex<_Tp>(__r, _Tp(0));1259 } else1260 __is.setstate(__is.failbit);1261 } else1262 __is.setstate(__is.failbit);1263 } else {1264 _Tp __r;1265 __is >> __r;1266 if (!__is.fail())1267 __x = complex<_Tp>(__r, _Tp(0));1268 else1269 __is.setstate(__is.failbit);1270 }1271 } else1272 __is.setstate(__is.failbit);1273 return __is;1274}1275 1276template <class _Tp, class _CharT, class _Traits>1277_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&1278operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) {1279 basic_ostringstream<_CharT, _Traits> __s;1280 __s.flags(__os.flags());1281 __s.imbue(__os.getloc());1282 __s.precision(__os.precision());1283 __s << '(' << __x.real() << ',' << __x.imag() << ')';1284 return __os << __s.str();1285}1286#endif // !_LIBCPP_HAS_NO_LOCALIZATION1287 1288_LIBCPP_END_NAMESPACE_STD1289 1290_LIBCPP_POP_MACROS1291 1292#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES)1293# include <__cxx03/iosfwd>1294# include <__cxx03/stdexcept>1295# include <__cxx03/type_traits>1296#endif1297 1298#endif // _LIBCPP___CXX03_COMPLEX1299