complex 54 KB

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