complex 56 KB

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