complex 56 KB

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