complex 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645
  1. // The template and inlines for the -*- C++ -*- complex number classes.
  2. // Copyright (C) 1997-2023 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/complex
  21. * This is a Standard C++ Library header.
  22. */
  23. //
  24. // ISO C++ 14882: 26.2 Complex Numbers
  25. // Note: this is not a conforming implementation.
  26. // Initially implemented by Ulrich Drepper <drepper@cygnus.com>
  27. // Improved by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
  28. //
  29. #ifndef _GLIBCXX_COMPLEX
  30. #define _GLIBCXX_COMPLEX 1
  31. #pragma GCC system_header
  32. #include <bits/c++config.h>
  33. #include <bits/cpp_type_traits.h>
  34. #include <ext/type_traits.h>
  35. #include <cmath>
  36. #include <sstream>
  37. // Get rid of a macro possibly defined in <complex.h>
  38. #undef complex
  39. #if __cplusplus > 201703L
  40. # define __cpp_lib_constexpr_complex 201711L
  41. #endif
  42. namespace std _GLIBCXX_VISIBILITY(default)
  43. {
  44. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  45. /**
  46. * @defgroup complex_numbers Complex Numbers
  47. * @ingroup numerics
  48. *
  49. * Classes and functions for complex numbers.
  50. * @{
  51. */
  52. // Forward declarations.
  53. template<typename _Tp> class complex;
  54. template<> class complex<float>;
  55. template<> class complex<double>;
  56. template<> class complex<long double>;
  57. /// Return magnitude of @a z.
  58. template<typename _Tp> _Tp abs(const complex<_Tp>&);
  59. /// Return phase angle of @a z.
  60. template<typename _Tp> _Tp arg(const complex<_Tp>&);
  61. /// Return @a z magnitude squared.
  62. template<typename _Tp> _Tp _GLIBCXX20_CONSTEXPR norm(const complex<_Tp>&);
  63. /// Return complex conjugate of @a z.
  64. template<typename _Tp>
  65. _GLIBCXX20_CONSTEXPR complex<_Tp> conj(const complex<_Tp>&);
  66. /// Return complex with magnitude @a rho and angle @a theta.
  67. template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
  68. // Transcendentals:
  69. /// Return complex cosine of @a z.
  70. template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
  71. /// Return complex hyperbolic cosine of @a z.
  72. template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
  73. /// Return complex base e exponential of @a z.
  74. template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
  75. /// Return complex natural logarithm of @a z.
  76. template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
  77. /// Return complex base 10 logarithm of @a z.
  78. template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
  79. /// Return @a x to the @a y'th power.
  80. template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
  81. /// Return @a x to the @a y'th power.
  82. template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
  83. /// Return @a x to the @a y'th power.
  84. template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&,
  85. const complex<_Tp>&);
  86. /// Return @a x to the @a y'th power.
  87. template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
  88. /// Return complex sine of @a z.
  89. template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
  90. /// Return complex hyperbolic sine of @a z.
  91. template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
  92. /// Return complex square root of @a z.
  93. template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
  94. /// Return complex tangent of @a z.
  95. template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
  96. /// Return complex hyperbolic tangent of @a z.
  97. template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
  98. // 26.2.2 Primary template class complex
  99. /**
  100. * Template to represent complex numbers.
  101. *
  102. * Specializations for float, double, and long double are part of the
  103. * library. Results with any other type are not guaranteed.
  104. *
  105. * @param Tp Type of real and imaginary values.
  106. */
  107. template<typename _Tp>
  108. class complex
  109. {
  110. public:
  111. /// Value typedef.
  112. typedef _Tp value_type;
  113. /// Default constructor. First parameter is x, second parameter is y.
  114. /// Unspecified parameters default to 0.
  115. _GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
  116. : _M_real(__r), _M_imag(__i) { }
  117. // Let the compiler synthesize the copy constructor
  118. #if __cplusplus >= 201103L
  119. constexpr complex(const complex&) = default;
  120. #endif
  121. /// Converting constructor.
  122. template<typename _Up>
  123. #if __cplusplus > 202002L
  124. explicit(!requires(_Up __u) { _Tp{__u}; })
  125. #endif
  126. _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z)
  127. : _M_real(_Tp(__z.real())), _M_imag(_Tp(__z.imag())) { }
  128. #if __cplusplus >= 201103L
  129. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  130. // DR 387. std::complex over-encapsulated.
  131. _GLIBCXX_ABI_TAG_CXX11
  132. constexpr _Tp
  133. real() const { return _M_real; }
  134. _GLIBCXX_ABI_TAG_CXX11
  135. constexpr _Tp
  136. imag() const { return _M_imag; }
  137. #else
  138. /// Return real part of complex number.
  139. _Tp&
  140. real() { return _M_real; }
  141. /// Return real part of complex number.
  142. const _Tp&
  143. real() const { return _M_real; }
  144. /// Return imaginary part of complex number.
  145. _Tp&
  146. imag() { return _M_imag; }
  147. /// Return imaginary part of complex number.
  148. const _Tp&
  149. imag() const { return _M_imag; }
  150. #endif
  151. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  152. // DR 387. std::complex over-encapsulated.
  153. _GLIBCXX20_CONSTEXPR void
  154. real(_Tp __val) { _M_real = __val; }
  155. _GLIBCXX20_CONSTEXPR void
  156. imag(_Tp __val) { _M_imag = __val; }
  157. /// Assign a scalar to this complex number.
  158. _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const _Tp&);
  159. /// Add a scalar to this complex number.
  160. // 26.2.5/1
  161. _GLIBCXX20_CONSTEXPR complex<_Tp>&
  162. operator+=(const _Tp& __t)
  163. {
  164. _M_real += __t;
  165. return *this;
  166. }
  167. /// Subtract a scalar from this complex number.
  168. // 26.2.5/3
  169. _GLIBCXX20_CONSTEXPR complex<_Tp>&
  170. operator-=(const _Tp& __t)
  171. {
  172. _M_real -= __t;
  173. return *this;
  174. }
  175. /// Multiply this complex number by a scalar.
  176. _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const _Tp&);
  177. /// Divide this complex number by a scalar.
  178. _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const _Tp&);
  179. // Let the compiler synthesize the copy assignment operator
  180. #if __cplusplus >= 201103L
  181. _GLIBCXX20_CONSTEXPR complex& operator=(const complex&) = default;
  182. #endif
  183. /// Assign another complex number to this one.
  184. template<typename _Up>
  185. _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const complex<_Up>&);
  186. /// Add another complex number to this one.
  187. template<typename _Up>
  188. _GLIBCXX20_CONSTEXPR complex<_Tp>& operator+=(const complex<_Up>&);
  189. /// Subtract another complex number from this one.
  190. template<typename _Up>
  191. _GLIBCXX20_CONSTEXPR complex<_Tp>& operator-=(const complex<_Up>&);
  192. /// Multiply this complex number by another.
  193. template<typename _Up>
  194. _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const complex<_Up>&);
  195. /// Divide this complex number by another.
  196. template<typename _Up>
  197. _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const complex<_Up>&);
  198. _GLIBCXX_CONSTEXPR complex __rep() const
  199. { return *this; }
  200. private:
  201. _Tp _M_real;
  202. _Tp _M_imag;
  203. };
  204. template<typename _Tp>
  205. _GLIBCXX20_CONSTEXPR complex<_Tp>&
  206. complex<_Tp>::operator=(const _Tp& __t)
  207. {
  208. _M_real = __t;
  209. _M_imag = _Tp();
  210. return *this;
  211. }
  212. // 26.2.5/5
  213. template<typename _Tp>
  214. _GLIBCXX20_CONSTEXPR complex<_Tp>&
  215. complex<_Tp>::operator*=(const _Tp& __t)
  216. {
  217. _M_real *= __t;
  218. _M_imag *= __t;
  219. return *this;
  220. }
  221. // 26.2.5/7
  222. template<typename _Tp>
  223. _GLIBCXX20_CONSTEXPR complex<_Tp>&
  224. complex<_Tp>::operator/=(const _Tp& __t)
  225. {
  226. _M_real /= __t;
  227. _M_imag /= __t;
  228. return *this;
  229. }
  230. template<typename _Tp>
  231. template<typename _Up>
  232. _GLIBCXX20_CONSTEXPR complex<_Tp>&
  233. complex<_Tp>::operator=(const complex<_Up>& __z)
  234. {
  235. _M_real = __z.real();
  236. _M_imag = __z.imag();
  237. return *this;
  238. }
  239. // 26.2.5/9
  240. template<typename _Tp>
  241. template<typename _Up>
  242. _GLIBCXX20_CONSTEXPR complex<_Tp>&
  243. complex<_Tp>::operator+=(const complex<_Up>& __z)
  244. {
  245. _M_real += __z.real();
  246. _M_imag += __z.imag();
  247. return *this;
  248. }
  249. // 26.2.5/11
  250. template<typename _Tp>
  251. template<typename _Up>
  252. _GLIBCXX20_CONSTEXPR complex<_Tp>&
  253. complex<_Tp>::operator-=(const complex<_Up>& __z)
  254. {
  255. _M_real -= __z.real();
  256. _M_imag -= __z.imag();
  257. return *this;
  258. }
  259. // 26.2.5/13
  260. // XXX: This is a grammar school implementation.
  261. template<typename _Tp>
  262. template<typename _Up>
  263. _GLIBCXX20_CONSTEXPR complex<_Tp>&
  264. complex<_Tp>::operator*=(const complex<_Up>& __z)
  265. {
  266. const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
  267. _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
  268. _M_real = __r;
  269. return *this;
  270. }
  271. // 26.2.5/15
  272. // XXX: This is a grammar school implementation.
  273. template<typename _Tp>
  274. template<typename _Up>
  275. _GLIBCXX20_CONSTEXPR complex<_Tp>&
  276. complex<_Tp>::operator/=(const complex<_Up>& __z)
  277. {
  278. const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag();
  279. const _Tp __n = std::norm(__z);
  280. _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
  281. _M_real = __r / __n;
  282. return *this;
  283. }
  284. // Operators:
  285. ///@{
  286. /// Return new complex value @a x plus @a y.
  287. template<typename _Tp>
  288. inline _GLIBCXX20_CONSTEXPR complex<_Tp>
  289. operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
  290. {
  291. complex<_Tp> __r = __x;
  292. __r += __y;
  293. return __r;
  294. }
  295. template<typename _Tp>
  296. inline _GLIBCXX20_CONSTEXPR complex<_Tp>
  297. operator+(const complex<_Tp>& __x, const _Tp& __y)
  298. {
  299. complex<_Tp> __r = __x;
  300. __r += __y;
  301. return __r;
  302. }
  303. template<typename _Tp>
  304. inline _GLIBCXX20_CONSTEXPR complex<_Tp>
  305. operator+(const _Tp& __x, const complex<_Tp>& __y)
  306. {
  307. complex<_Tp> __r = __y;
  308. __r += __x;
  309. return __r;
  310. }
  311. ///@}
  312. ///@{
  313. /// Return new complex value @a x minus @a y.
  314. template<typename _Tp>
  315. inline _GLIBCXX20_CONSTEXPR complex<_Tp>
  316. operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
  317. {
  318. complex<_Tp> __r = __x;
  319. __r -= __y;
  320. return __r;
  321. }
  322. template<typename _Tp>
  323. inline _GLIBCXX20_CONSTEXPR complex<_Tp>
  324. operator-(const complex<_Tp>& __x, const _Tp& __y)
  325. {
  326. complex<_Tp> __r = __x;
  327. __r -= __y;
  328. return __r;
  329. }
  330. template<typename _Tp>
  331. inline _GLIBCXX20_CONSTEXPR complex<_Tp>
  332. operator-(const _Tp& __x, const complex<_Tp>& __y)
  333. {
  334. complex<_Tp> __r = -__y;
  335. __r += __x;
  336. return __r;
  337. }
  338. ///@}
  339. ///@{
  340. /// Return new complex value @a x times @a y.
  341. template<typename _Tp>
  342. inline _GLIBCXX20_CONSTEXPR complex<_Tp>
  343. operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
  344. {
  345. complex<_Tp> __r = __x;
  346. __r *= __y;
  347. return __r;
  348. }
  349. template<typename _Tp>
  350. inline _GLIBCXX20_CONSTEXPR complex<_Tp>
  351. operator*(const complex<_Tp>& __x, const _Tp& __y)
  352. {
  353. complex<_Tp> __r = __x;
  354. __r *= __y;
  355. return __r;
  356. }
  357. template<typename _Tp>
  358. inline _GLIBCXX20_CONSTEXPR complex<_Tp>
  359. operator*(const _Tp& __x, const complex<_Tp>& __y)
  360. {
  361. complex<_Tp> __r = __y;
  362. __r *= __x;
  363. return __r;
  364. }
  365. ///@}
  366. ///@{
  367. /// Return new complex value @a x divided by @a y.
  368. template<typename _Tp>
  369. inline _GLIBCXX20_CONSTEXPR complex<_Tp>
  370. operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
  371. {
  372. complex<_Tp> __r = __x;
  373. __r /= __y;
  374. return __r;
  375. }
  376. template<typename _Tp>
  377. inline _GLIBCXX20_CONSTEXPR complex<_Tp>
  378. operator/(const complex<_Tp>& __x, const _Tp& __y)
  379. {
  380. complex<_Tp> __r = __x;
  381. __r /= __y;
  382. return __r;
  383. }
  384. template<typename _Tp>
  385. inline _GLIBCXX20_CONSTEXPR complex<_Tp>
  386. operator/(const _Tp& __x, const complex<_Tp>& __y)
  387. {
  388. complex<_Tp> __r = __x;
  389. __r /= __y;
  390. return __r;
  391. }
  392. ///@}
  393. /// Return @a x.
  394. template<typename _Tp>
  395. inline _GLIBCXX20_CONSTEXPR complex<_Tp>
  396. operator+(const complex<_Tp>& __x)
  397. { return __x; }
  398. /// Return complex negation of @a x.
  399. template<typename _Tp>
  400. inline _GLIBCXX20_CONSTEXPR complex<_Tp>
  401. operator-(const complex<_Tp>& __x)
  402. { return complex<_Tp>(-__x.real(), -__x.imag()); }
  403. ///@{
  404. /// Return true if @a x is equal to @a y.
  405. template<typename _Tp>
  406. inline _GLIBCXX_CONSTEXPR bool
  407. operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
  408. { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
  409. template<typename _Tp>
  410. inline _GLIBCXX_CONSTEXPR bool
  411. operator==(const complex<_Tp>& __x, const _Tp& __y)
  412. { return __x.real() == __y && __x.imag() == _Tp(); }
  413. #if !(__cpp_impl_three_way_comparison >= 201907L)
  414. template<typename _Tp>
  415. inline _GLIBCXX_CONSTEXPR bool
  416. operator==(const _Tp& __x, const complex<_Tp>& __y)
  417. { return __x == __y.real() && _Tp() == __y.imag(); }
  418. ///@}
  419. ///@{
  420. /// Return false if @a x is equal to @a y.
  421. template<typename _Tp>
  422. inline _GLIBCXX_CONSTEXPR bool
  423. operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
  424. { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
  425. template<typename _Tp>
  426. inline _GLIBCXX_CONSTEXPR bool
  427. operator!=(const complex<_Tp>& __x, const _Tp& __y)
  428. { return __x.real() != __y || __x.imag() != _Tp(); }
  429. template<typename _Tp>
  430. inline _GLIBCXX_CONSTEXPR bool
  431. operator!=(const _Tp& __x, const complex<_Tp>& __y)
  432. { return __x != __y.real() || _Tp() != __y.imag(); }
  433. #endif
  434. ///@}
  435. /// Extraction operator for complex values.
  436. template<typename _Tp, typename _CharT, class _Traits>
  437. basic_istream<_CharT, _Traits>&
  438. operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
  439. {
  440. bool __fail = true;
  441. _CharT __ch;
  442. if (__is >> __ch)
  443. {
  444. if (_Traits::eq(__ch, __is.widen('(')))
  445. {
  446. _Tp __u;
  447. if (__is >> __u >> __ch)
  448. {
  449. const _CharT __rparen = __is.widen(')');
  450. if (_Traits::eq(__ch, __rparen))
  451. {
  452. __x = __u;
  453. __fail = false;
  454. }
  455. else if (_Traits::eq(__ch, __is.widen(',')))
  456. {
  457. _Tp __v;
  458. if (__is >> __v >> __ch)
  459. {
  460. if (_Traits::eq(__ch, __rparen))
  461. {
  462. __x = complex<_Tp>(__u, __v);
  463. __fail = false;
  464. }
  465. else
  466. __is.putback(__ch);
  467. }
  468. }
  469. else
  470. __is.putback(__ch);
  471. }
  472. }
  473. else
  474. {
  475. __is.putback(__ch);
  476. _Tp __u;
  477. if (__is >> __u)
  478. {
  479. __x = __u;
  480. __fail = false;
  481. }
  482. }
  483. }
  484. if (__fail)
  485. __is.setstate(ios_base::failbit);
  486. return __is;
  487. }
  488. /// Insertion operator for complex values.
  489. template<typename _Tp, typename _CharT, class _Traits>
  490. basic_ostream<_CharT, _Traits>&
  491. operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
  492. {
  493. basic_ostringstream<_CharT, _Traits> __s;
  494. __s.flags(__os.flags());
  495. __s.imbue(__os.getloc());
  496. __s.precision(__os.precision());
  497. __s << '(' << __x.real() << ',' << __x.imag() << ')';
  498. return __os << __s.str();
  499. }
  500. // Values
  501. #if __cplusplus >= 201103L
  502. template<typename _Tp>
  503. constexpr _Tp
  504. real(const complex<_Tp>& __z)
  505. { return __z.real(); }
  506. template<typename _Tp>
  507. constexpr _Tp
  508. imag(const complex<_Tp>& __z)
  509. { return __z.imag(); }
  510. #else
  511. template<typename _Tp>
  512. inline _Tp&
  513. real(complex<_Tp>& __z)
  514. { return __z.real(); }
  515. template<typename _Tp>
  516. inline const _Tp&
  517. real(const complex<_Tp>& __z)
  518. { return __z.real(); }
  519. template<typename _Tp>
  520. inline _Tp&
  521. imag(complex<_Tp>& __z)
  522. { return __z.imag(); }
  523. template<typename _Tp>
  524. inline const _Tp&
  525. imag(const complex<_Tp>& __z)
  526. { return __z.imag(); }
  527. #endif
  528. #if _GLIBCXX_USE_C99_COMPLEX
  529. #if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
  530. inline _Float16
  531. __complex_abs(__complex__ _Float16 __z)
  532. { return _Float16(__builtin_cabsf(__z)); }
  533. inline _Float16
  534. __complex_arg(__complex__ _Float16 __z)
  535. { return _Float16(__builtin_cargf(__z)); }
  536. inline __complex__ _Float16
  537. __complex_cos(__complex__ _Float16 __z)
  538. { return static_cast<__complex__ _Float16>(__builtin_ccosf(__z)); }
  539. inline __complex__ _Float16
  540. __complex_cosh(__complex__ _Float16 __z)
  541. { return static_cast<__complex__ _Float16>(__builtin_ccoshf(__z)); }
  542. inline __complex__ _Float16
  543. __complex_exp(__complex__ _Float16 __z)
  544. { return static_cast<__complex__ _Float16>(__builtin_cexpf(__z)); }
  545. inline __complex__ _Float16
  546. __complex_log(__complex__ _Float16 __z)
  547. { return static_cast<__complex__ _Float16>(__builtin_clogf(__z)); }
  548. inline __complex__ _Float16
  549. __complex_sin(__complex__ _Float16 __z)
  550. { return static_cast<__complex__ _Float16>(__builtin_csinf(__z)); }
  551. inline __complex__ _Float16
  552. __complex_sinh(__complex__ _Float16 __z)
  553. { return static_cast<__complex__ _Float16>(__builtin_csinhf(__z)); }
  554. inline __complex__ _Float16
  555. __complex_sqrt(__complex__ _Float16 __z)
  556. { return static_cast<__complex__ _Float16>(__builtin_csqrtf(__z)); }
  557. inline __complex__ _Float16
  558. __complex_tan(__complex__ _Float16 __z)
  559. { return static_cast<__complex__ _Float16>(__builtin_ctanf(__z)); }
  560. inline __complex__ _Float16
  561. __complex_tanh(__complex__ _Float16 __z)
  562. { return static_cast<__complex__ _Float16>(__builtin_ctanhf(__z)); }
  563. inline __complex__ _Float16
  564. __complex_pow(__complex__ _Float16 __x, __complex__ _Float16 __y)
  565. { return static_cast<__complex__ _Float16>(__builtin_cpowf(__x, __y)); }
  566. #endif
  567. #if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
  568. inline _Float32
  569. __complex_abs(__complex__ _Float32 __z) { return __builtin_cabsf(__z); }
  570. inline _Float32
  571. __complex_arg(__complex__ _Float32 __z) { return __builtin_cargf(__z); }
  572. inline __complex__ _Float32
  573. __complex_cos(__complex__ _Float32 __z) { return __builtin_ccosf(__z); }
  574. inline __complex__ _Float32
  575. __complex_cosh(__complex__ _Float32 __z) { return __builtin_ccoshf(__z); }
  576. inline __complex__ _Float32
  577. __complex_exp(__complex__ _Float32 __z) { return __builtin_cexpf(__z); }
  578. inline __complex__ _Float32
  579. __complex_log(__complex__ _Float32 __z) { return __builtin_clogf(__z); }
  580. inline __complex__ _Float32
  581. __complex_sin(__complex__ _Float32 __z) { return __builtin_csinf(__z); }
  582. inline __complex__ _Float32
  583. __complex_sinh(__complex__ _Float32 __z) { return __builtin_csinhf(__z); }
  584. inline __complex__ _Float32
  585. __complex_sqrt(__complex__ _Float32 __z) { return __builtin_csqrtf(__z); }
  586. inline __complex__ _Float32
  587. __complex_tan(__complex__ _Float32 __z) { return __builtin_ctanf(__z); }
  588. inline __complex__ _Float32
  589. __complex_tanh(__complex__ _Float32 __z) { return __builtin_ctanhf(__z); }
  590. inline __complex__ _Float32
  591. __complex_pow(__complex__ _Float32 __x, __complex__ _Float32 __y)
  592. { return __builtin_cpowf(__x, __y); }
  593. #endif
  594. #if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
  595. inline _Float64
  596. __complex_abs(__complex__ _Float64 __z) { return __builtin_cabs(__z); }
  597. inline _Float64
  598. __complex_arg(__complex__ _Float64 __z) { return __builtin_carg(__z); }
  599. inline __complex__ _Float64
  600. __complex_cos(__complex__ _Float64 __z) { return __builtin_ccos(__z); }
  601. inline __complex__ _Float64
  602. __complex_cosh(__complex__ _Float64 __z) { return __builtin_ccosh(__z); }
  603. inline __complex__ _Float64
  604. __complex_exp(__complex__ _Float64 __z) { return __builtin_cexp(__z); }
  605. inline __complex__ _Float64
  606. __complex_log(__complex__ _Float64 __z) { return __builtin_clog(__z); }
  607. inline __complex__ _Float64
  608. __complex_sin(__complex__ _Float64 __z) { return __builtin_csin(__z); }
  609. inline __complex__ _Float64
  610. __complex_sinh(__complex__ _Float64 __z) { return __builtin_csinh(__z); }
  611. inline __complex__ _Float64
  612. __complex_sqrt(__complex__ _Float64 __z) { return __builtin_csqrt(__z); }
  613. inline __complex__ _Float64
  614. __complex_tan(__complex__ _Float64 __z) { return __builtin_ctan(__z); }
  615. inline __complex__ _Float64
  616. __complex_tanh(__complex__ _Float64 __z) { return __builtin_ctanh(__z); }
  617. inline __complex__ _Float64
  618. __complex_pow(__complex__ _Float64 __x, __complex__ _Float64 __y)
  619. { return __builtin_cpow(__x, __y); }
  620. #endif
  621. #if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
  622. inline _Float128
  623. __complex_abs(__complex__ _Float128 __z) { return __builtin_cabsl(__z); }
  624. inline _Float128
  625. __complex_arg(__complex__ _Float128 __z) { return __builtin_cargl(__z); }
  626. inline __complex__ _Float128
  627. __complex_cos(__complex__ _Float128 __z) { return __builtin_ccosl(__z); }
  628. inline __complex__ _Float128
  629. __complex_cosh(__complex__ _Float128 __z) { return __builtin_ccoshl(__z); }
  630. inline __complex__ _Float128
  631. __complex_exp(__complex__ _Float128 __z) { return __builtin_cexpl(__z); }
  632. inline __complex__ _Float128
  633. __complex_log(__complex__ _Float128 __z) { return __builtin_clogl(__z); }
  634. inline __complex__ _Float128
  635. __complex_sin(__complex__ _Float128 __z) { return __builtin_csinl(__z); }
  636. inline __complex__ _Float128
  637. __complex_sinh(__complex__ _Float128 __z) { return __builtin_csinhl(__z); }
  638. inline __complex__ _Float128
  639. __complex_sqrt(__complex__ _Float128 __z) { return __builtin_csqrtl(__z); }
  640. inline __complex__ _Float128
  641. __complex_tan(__complex__ _Float128 __z) { return __builtin_ctanl(__z); }
  642. inline __complex__ _Float128
  643. __complex_tanh(__complex__ _Float128 __z) { return __builtin_ctanhl(__z); }
  644. inline __complex__ _Float128
  645. __complex_pow(__complex__ _Float128 __x, __complex__ _Float128 __y)
  646. { return __builtin_cpowl(__x, __y); }
  647. #elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
  648. inline _Float128
  649. __complex_abs(__complex__ _Float128 __z) { return __builtin_cabsf128(__z); }
  650. inline _Float128
  651. __complex_arg(__complex__ _Float128 __z) { return __builtin_cargf128(__z); }
  652. inline __complex__ _Float128
  653. __complex_cos(__complex__ _Float128 __z) { return __builtin_ccosf128(__z); }
  654. inline __complex__ _Float128
  655. __complex_cosh(__complex__ _Float128 __z) { return __builtin_ccoshf128(__z); }
  656. inline __complex__ _Float128
  657. __complex_exp(__complex__ _Float128 __z) { return __builtin_cexpf128(__z); }
  658. inline __complex__ _Float128
  659. __complex_log(__complex__ _Float128 __z) { return __builtin_clogf128(__z); }
  660. inline __complex__ _Float128
  661. __complex_sin(__complex__ _Float128 __z) { return __builtin_csinf128(__z); }
  662. inline __complex__ _Float128
  663. __complex_sinh(__complex__ _Float128 __z) { return __builtin_csinhf128(__z); }
  664. inline __complex__ _Float128
  665. __complex_sqrt(__complex__ _Float128 __z) { return __builtin_csqrtf128(__z); }
  666. inline __complex__ _Float128
  667. __complex_tan(__complex__ _Float128 __z) { return __builtin_ctanf128(__z); }
  668. inline __complex__ _Float128
  669. __complex_tanh(__complex__ _Float128 __z) { return __builtin_ctanhf128(__z); }
  670. inline __complex__ _Float128
  671. __complex_pow(__complex__ _Float128 __x, __complex__ _Float128 __y)
  672. { return __builtin_cpowf128(__x, __y); }
  673. #endif
  674. #if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
  675. inline __gnu_cxx::__bfloat16_t
  676. __complex_abs(__complex__ decltype(0.0bf16) __z)
  677. { return __gnu_cxx::__bfloat16_t(__builtin_cabsf(__z)); }
  678. inline __gnu_cxx::__bfloat16_t
  679. __complex_arg(__complex__ decltype(0.0bf16) __z)
  680. { return __gnu_cxx::__bfloat16_t(__builtin_cargf(__z)); }
  681. inline __complex__ decltype(0.0bf16)
  682. __complex_cos(__complex__ decltype(0.0bf16) __z)
  683. { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ccosf(__z)); }
  684. inline __complex__ decltype(0.0bf16)
  685. __complex_cosh(__complex__ decltype(0.0bf16) __z)
  686. { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ccoshf(__z)); }
  687. inline __complex__ decltype(0.0bf16)
  688. __complex_exp(__complex__ decltype(0.0bf16) __z)
  689. { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cexpf(__z)); }
  690. inline __complex__ decltype(0.0bf16)
  691. __complex_log(__complex__ decltype(0.0bf16) __z)
  692. { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_clogf(__z)); }
  693. inline __complex__ decltype(0.0bf16)
  694. __complex_sin(__complex__ decltype(0.0bf16) __z)
  695. { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_csinf(__z)); }
  696. inline __complex__ decltype(0.0bf16)
  697. __complex_sinh(__complex__ decltype(0.0bf16) __z)
  698. { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_csinhf(__z)); }
  699. inline __complex__ decltype(0.0bf16)
  700. __complex_sqrt(__complex__ decltype(0.0bf16) __z)
  701. { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_csqrtf(__z)); }
  702. inline __complex__ decltype(0.0bf16)
  703. __complex_tan(__complex__ decltype(0.0bf16) __z)
  704. { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ctanf(__z)); }
  705. inline __complex__ decltype(0.0bf16)
  706. __complex_tanh(__complex__ decltype(0.0bf16) __z)
  707. { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_ctanhf(__z)); }
  708. inline __complex__ decltype(0.0bf16)
  709. __complex_pow(__complex__ decltype(0.0bf16) __x,
  710. __complex__ decltype(0.0bf16) __y)
  711. { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cpowf(__x,
  712. __y)); }
  713. #endif
  714. #endif
  715. // 26.2.7/3 abs(__z): Returns the magnitude of __z.
  716. template<typename _Tp>
  717. inline _Tp
  718. __complex_abs(const complex<_Tp>& __z)
  719. {
  720. _Tp __x = __z.real();
  721. _Tp __y = __z.imag();
  722. const _Tp __s = std::max(abs(__x), abs(__y));
  723. if (__s == _Tp()) // well ...
  724. return __s;
  725. __x /= __s;
  726. __y /= __s;
  727. return __s * sqrt(__x * __x + __y * __y);
  728. }
  729. #if _GLIBCXX_USE_C99_COMPLEX
  730. inline float
  731. __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
  732. inline double
  733. __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
  734. inline long double
  735. __complex_abs(const __complex__ long double& __z)
  736. { return __builtin_cabsl(__z); }
  737. template<typename _Tp>
  738. inline _Tp
  739. abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
  740. #else
  741. template<typename _Tp>
  742. inline _Tp
  743. abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
  744. #endif
  745. // 26.2.7/4: arg(__z): Returns the phase angle of __z.
  746. template<typename _Tp>
  747. inline _Tp
  748. __complex_arg(const complex<_Tp>& __z)
  749. { return atan2(__z.imag(), __z.real()); }
  750. #if _GLIBCXX_USE_C99_COMPLEX
  751. inline float
  752. __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
  753. inline double
  754. __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
  755. inline long double
  756. __complex_arg(const __complex__ long double& __z)
  757. { return __builtin_cargl(__z); }
  758. template<typename _Tp>
  759. inline _Tp
  760. arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
  761. #else
  762. template<typename _Tp>
  763. inline _Tp
  764. arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
  765. #endif
  766. // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
  767. // As defined, norm() is -not- a norm is the common mathematical
  768. // sense used in numerics. The helper class _Norm_helper<> tries to
  769. // distinguish between builtin floating point and the rest, so as
  770. // to deliver an answer as close as possible to the real value.
  771. template<bool>
  772. struct _Norm_helper
  773. {
  774. template<typename _Tp>
  775. static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z)
  776. {
  777. const _Tp __x = __z.real();
  778. const _Tp __y = __z.imag();
  779. return __x * __x + __y * __y;
  780. }
  781. };
  782. template<>
  783. struct _Norm_helper<true>
  784. {
  785. template<typename _Tp>
  786. static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z)
  787. {
  788. //_Tp __res = std::abs(__z);
  789. //return __res * __res;
  790. const _Tp __x = __z.real();
  791. const _Tp __y = __z.imag();
  792. return __x * __x + __y * __y;
  793. }
  794. };
  795. template<typename _Tp>
  796. inline _GLIBCXX20_CONSTEXPR _Tp
  797. norm(const complex<_Tp>& __z)
  798. {
  799. return _Norm_helper<__is_floating<_Tp>::__value
  800. && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
  801. }
  802. template<typename _Tp>
  803. inline complex<_Tp>
  804. polar(const _Tp& __rho, const _Tp& __theta)
  805. {
  806. __glibcxx_assert( __rho >= 0 );
  807. return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta));
  808. }
  809. template<typename _Tp>
  810. inline _GLIBCXX20_CONSTEXPR complex<_Tp>
  811. conj(const complex<_Tp>& __z)
  812. { return complex<_Tp>(__z.real(), -__z.imag()); }
  813. // Transcendentals
  814. // 26.2.8/1 cos(__z): Returns the cosine of __z.
  815. template<typename _Tp>
  816. inline complex<_Tp>
  817. __complex_cos(const complex<_Tp>& __z)
  818. {
  819. const _Tp __x = __z.real();
  820. const _Tp __y = __z.imag();
  821. return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
  822. }
  823. #if _GLIBCXX_USE_C99_COMPLEX
  824. inline __complex__ float
  825. __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
  826. inline __complex__ double
  827. __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
  828. inline __complex__ long double
  829. __complex_cos(const __complex__ long double& __z)
  830. { return __builtin_ccosl(__z); }
  831. template<typename _Tp>
  832. inline complex<_Tp>
  833. cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
  834. #else
  835. template<typename _Tp>
  836. inline complex<_Tp>
  837. cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
  838. #endif
  839. // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
  840. template<typename _Tp>
  841. inline complex<_Tp>
  842. __complex_cosh(const complex<_Tp>& __z)
  843. {
  844. const _Tp __x = __z.real();
  845. const _Tp __y = __z.imag();
  846. return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
  847. }
  848. #if _GLIBCXX_USE_C99_COMPLEX
  849. inline __complex__ float
  850. __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
  851. inline __complex__ double
  852. __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
  853. inline __complex__ long double
  854. __complex_cosh(const __complex__ long double& __z)
  855. { return __builtin_ccoshl(__z); }
  856. template<typename _Tp>
  857. inline complex<_Tp>
  858. cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
  859. #else
  860. template<typename _Tp>
  861. inline complex<_Tp>
  862. cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
  863. #endif
  864. // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
  865. template<typename _Tp>
  866. inline complex<_Tp>
  867. __complex_exp(const complex<_Tp>& __z)
  868. { return std::polar<_Tp>(exp(__z.real()), __z.imag()); }
  869. #if _GLIBCXX_USE_C99_COMPLEX
  870. inline __complex__ float
  871. __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
  872. inline __complex__ double
  873. __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
  874. inline __complex__ long double
  875. __complex_exp(const __complex__ long double& __z)
  876. { return __builtin_cexpl(__z); }
  877. template<typename _Tp>
  878. inline complex<_Tp>
  879. exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
  880. #else
  881. template<typename _Tp>
  882. inline complex<_Tp>
  883. exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
  884. #endif
  885. // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
  886. // The branch cut is along the negative axis.
  887. template<typename _Tp>
  888. inline complex<_Tp>
  889. __complex_log(const complex<_Tp>& __z)
  890. { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
  891. #if _GLIBCXX_USE_C99_COMPLEX
  892. inline __complex__ float
  893. __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
  894. inline __complex__ double
  895. __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
  896. inline __complex__ long double
  897. __complex_log(const __complex__ long double& __z)
  898. { return __builtin_clogl(__z); }
  899. template<typename _Tp>
  900. inline complex<_Tp>
  901. log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
  902. #else
  903. template<typename _Tp>
  904. inline complex<_Tp>
  905. log(const complex<_Tp>& __z) { return __complex_log(__z); }
  906. #endif
  907. template<typename _Tp>
  908. inline complex<_Tp>
  909. log10(const complex<_Tp>& __z)
  910. { return std::log(__z) / log(_Tp(10.0)); }
  911. // 26.2.8/10 sin(__z): Returns the sine of __z.
  912. template<typename _Tp>
  913. inline complex<_Tp>
  914. __complex_sin(const complex<_Tp>& __z)
  915. {
  916. const _Tp __x = __z.real();
  917. const _Tp __y = __z.imag();
  918. return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
  919. }
  920. #if _GLIBCXX_USE_C99_COMPLEX
  921. inline __complex__ float
  922. __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
  923. inline __complex__ double
  924. __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
  925. inline __complex__ long double
  926. __complex_sin(const __complex__ long double& __z)
  927. { return __builtin_csinl(__z); }
  928. template<typename _Tp>
  929. inline complex<_Tp>
  930. sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
  931. #else
  932. template<typename _Tp>
  933. inline complex<_Tp>
  934. sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
  935. #endif
  936. // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
  937. template<typename _Tp>
  938. inline complex<_Tp>
  939. __complex_sinh(const complex<_Tp>& __z)
  940. {
  941. const _Tp __x = __z.real();
  942. const _Tp __y = __z.imag();
  943. return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
  944. }
  945. #if _GLIBCXX_USE_C99_COMPLEX
  946. inline __complex__ float
  947. __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
  948. inline __complex__ double
  949. __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
  950. inline __complex__ long double
  951. __complex_sinh(const __complex__ long double& __z)
  952. { return __builtin_csinhl(__z); }
  953. template<typename _Tp>
  954. inline complex<_Tp>
  955. sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
  956. #else
  957. template<typename _Tp>
  958. inline complex<_Tp>
  959. sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
  960. #endif
  961. // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
  962. // The branch cut is on the negative axis.
  963. template<typename _Tp>
  964. complex<_Tp>
  965. __complex_sqrt(const complex<_Tp>& __z)
  966. {
  967. _Tp __x = __z.real();
  968. _Tp __y = __z.imag();
  969. if (__x == _Tp())
  970. {
  971. _Tp __t = sqrt(abs(__y) / 2);
  972. return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
  973. }
  974. else
  975. {
  976. _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
  977. _Tp __u = __t / 2;
  978. return __x > _Tp()
  979. ? complex<_Tp>(__u, __y / __t)
  980. : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
  981. }
  982. }
  983. #if _GLIBCXX_USE_C99_COMPLEX
  984. inline __complex__ float
  985. __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
  986. inline __complex__ double
  987. __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
  988. inline __complex__ long double
  989. __complex_sqrt(const __complex__ long double& __z)
  990. { return __builtin_csqrtl(__z); }
  991. template<typename _Tp>
  992. inline complex<_Tp>
  993. sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
  994. #else
  995. template<typename _Tp>
  996. inline complex<_Tp>
  997. sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
  998. #endif
  999. // 26.2.8/14 tan(__z): Return the complex tangent of __z.
  1000. template<typename _Tp>
  1001. inline complex<_Tp>
  1002. __complex_tan(const complex<_Tp>& __z)
  1003. { return std::sin(__z) / std::cos(__z); }
  1004. #if _GLIBCXX_USE_C99_COMPLEX
  1005. inline __complex__ float
  1006. __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
  1007. inline __complex__ double
  1008. __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
  1009. inline __complex__ long double
  1010. __complex_tan(const __complex__ long double& __z)
  1011. { return __builtin_ctanl(__z); }
  1012. template<typename _Tp>
  1013. inline complex<_Tp>
  1014. tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
  1015. #else
  1016. template<typename _Tp>
  1017. inline complex<_Tp>
  1018. tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
  1019. #endif
  1020. // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z.
  1021. template<typename _Tp>
  1022. inline complex<_Tp>
  1023. __complex_tanh(const complex<_Tp>& __z)
  1024. { return std::sinh(__z) / std::cosh(__z); }
  1025. #if _GLIBCXX_USE_C99_COMPLEX
  1026. inline __complex__ float
  1027. __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
  1028. inline __complex__ double
  1029. __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
  1030. inline __complex__ long double
  1031. __complex_tanh(const __complex__ long double& __z)
  1032. { return __builtin_ctanhl(__z); }
  1033. template<typename _Tp>
  1034. inline complex<_Tp>
  1035. tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
  1036. #else
  1037. template<typename _Tp>
  1038. inline complex<_Tp>
  1039. tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
  1040. #endif
  1041. // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x
  1042. // raised to the __y-th power. The branch
  1043. // cut is on the negative axis.
  1044. template<typename _Tp>
  1045. complex<_Tp>
  1046. __complex_pow_unsigned(complex<_Tp> __x, unsigned __n)
  1047. {
  1048. complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1);
  1049. while (__n >>= 1)
  1050. {
  1051. __x *= __x;
  1052. if (__n % 2)
  1053. __y *= __x;
  1054. }
  1055. return __y;
  1056. }
  1057. // In C++11 mode we used to implement the resolution of
  1058. // DR 844. complex pow return type is ambiguous.
  1059. // thus the following overload was disabled in that mode. However, doing
  1060. // that causes all sorts of issues, see, for example:
  1061. // http://gcc.gnu.org/ml/libstdc++/2013-01/msg00058.html
  1062. // and also PR57974.
  1063. template<typename _Tp>
  1064. inline complex<_Tp>
  1065. pow(const complex<_Tp>& __z, int __n)
  1066. {
  1067. return __n < 0
  1068. ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n)
  1069. : std::__complex_pow_unsigned(__z, __n);
  1070. }
  1071. template<typename _Tp>
  1072. complex<_Tp>
  1073. pow(const complex<_Tp>& __x, const _Tp& __y)
  1074. {
  1075. #if ! _GLIBCXX_USE_C99_COMPLEX
  1076. if (__x == _Tp())
  1077. return _Tp();
  1078. #endif
  1079. if (__x.imag() == _Tp() && __x.real() > _Tp())
  1080. return pow(__x.real(), __y);
  1081. complex<_Tp> __t = std::log(__x);
  1082. return std::polar<_Tp>(exp(__y * __t.real()), __y * __t.imag());
  1083. }
  1084. template<typename _Tp>
  1085. inline complex<_Tp>
  1086. __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
  1087. { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
  1088. #if _GLIBCXX_USE_C99_COMPLEX
  1089. inline __complex__ float
  1090. __complex_pow(__complex__ float __x, __complex__ float __y)
  1091. { return __builtin_cpowf(__x, __y); }
  1092. inline __complex__ double
  1093. __complex_pow(__complex__ double __x, __complex__ double __y)
  1094. { return __builtin_cpow(__x, __y); }
  1095. inline __complex__ long double
  1096. __complex_pow(const __complex__ long double& __x,
  1097. const __complex__ long double& __y)
  1098. { return __builtin_cpowl(__x, __y); }
  1099. template<typename _Tp>
  1100. inline complex<_Tp>
  1101. pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
  1102. { return __complex_pow(__x.__rep(), __y.__rep()); }
  1103. #else
  1104. template<typename _Tp>
  1105. inline complex<_Tp>
  1106. pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
  1107. { return __complex_pow(__x, __y); }
  1108. #endif
  1109. template<typename _Tp>
  1110. inline complex<_Tp>
  1111. pow(const _Tp& __x, const complex<_Tp>& __y)
  1112. {
  1113. return __x > _Tp() ? std::polar<_Tp>(pow(__x, __y.real()),
  1114. __y.imag() * log(__x))
  1115. : std::pow(complex<_Tp>(__x), __y);
  1116. }
  1117. /// 26.2.3 complex specializations
  1118. /// complex<float> specialization
  1119. template<>
  1120. class complex<float>
  1121. {
  1122. public:
  1123. typedef float value_type;
  1124. typedef __complex__ float _ComplexT;
  1125. _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
  1126. _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f)
  1127. #if __cplusplus >= 201103L
  1128. : _M_value{ __r, __i } { }
  1129. #else
  1130. {
  1131. __real__ _M_value = __r;
  1132. __imag__ _M_value = __i;
  1133. }
  1134. #endif
  1135. #if __cplusplus >= 201103L
  1136. _GLIBCXX14_CONSTEXPR complex(const complex&) = default;
  1137. #endif
  1138. #if __cplusplus > 202002L
  1139. template<typename _Up>
  1140. explicit(!requires(_Up __u) { value_type{__u}; })
  1141. constexpr complex(const complex<_Up>& __z)
  1142. : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
  1143. #else
  1144. explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&);
  1145. explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
  1146. #endif
  1147. #if __cplusplus >= 201103L
  1148. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1149. // DR 387. std::complex over-encapsulated.
  1150. __attribute ((__abi_tag__ ("cxx11")))
  1151. constexpr float
  1152. real() const { return __real__ _M_value; }
  1153. __attribute ((__abi_tag__ ("cxx11")))
  1154. constexpr float
  1155. imag() const { return __imag__ _M_value; }
  1156. #else
  1157. float&
  1158. real() { return __real__ _M_value; }
  1159. const float&
  1160. real() const { return __real__ _M_value; }
  1161. float&
  1162. imag() { return __imag__ _M_value; }
  1163. const float&
  1164. imag() const { return __imag__ _M_value; }
  1165. #endif
  1166. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1167. // DR 387. std::complex over-encapsulated.
  1168. _GLIBCXX20_CONSTEXPR void
  1169. real(float __val) { __real__ _M_value = __val; }
  1170. _GLIBCXX20_CONSTEXPR void
  1171. imag(float __val) { __imag__ _M_value = __val; }
  1172. _GLIBCXX20_CONSTEXPR complex&
  1173. operator=(float __f)
  1174. {
  1175. _M_value = __f;
  1176. return *this;
  1177. }
  1178. _GLIBCXX20_CONSTEXPR complex&
  1179. operator+=(float __f)
  1180. {
  1181. _M_value += __f;
  1182. return *this;
  1183. }
  1184. _GLIBCXX20_CONSTEXPR complex&
  1185. operator-=(float __f)
  1186. {
  1187. _M_value -= __f;
  1188. return *this;
  1189. }
  1190. _GLIBCXX20_CONSTEXPR complex&
  1191. operator*=(float __f)
  1192. {
  1193. _M_value *= __f;
  1194. return *this;
  1195. }
  1196. _GLIBCXX20_CONSTEXPR complex&
  1197. operator/=(float __f)
  1198. {
  1199. _M_value /= __f;
  1200. return *this;
  1201. }
  1202. // Let the compiler synthesize the copy and assignment
  1203. // operator. It always does a pretty good job.
  1204. #if __cplusplus >= 201103L
  1205. _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
  1206. #endif
  1207. template<typename _Tp>
  1208. _GLIBCXX20_CONSTEXPR complex&
  1209. operator=(const complex<_Tp>& __z)
  1210. {
  1211. __real__ _M_value = __z.real();
  1212. __imag__ _M_value = __z.imag();
  1213. return *this;
  1214. }
  1215. template<typename _Tp>
  1216. _GLIBCXX20_CONSTEXPR complex&
  1217. operator+=(const complex<_Tp>& __z)
  1218. {
  1219. _M_value += __z.__rep();
  1220. return *this;
  1221. }
  1222. template<class _Tp>
  1223. _GLIBCXX20_CONSTEXPR complex&
  1224. operator-=(const complex<_Tp>& __z)
  1225. {
  1226. _M_value -= __z.__rep();
  1227. return *this;
  1228. }
  1229. template<class _Tp>
  1230. _GLIBCXX20_CONSTEXPR complex&
  1231. operator*=(const complex<_Tp>& __z)
  1232. {
  1233. const _ComplexT __t = __z.__rep();
  1234. _M_value *= __t;
  1235. return *this;
  1236. }
  1237. template<class _Tp>
  1238. _GLIBCXX20_CONSTEXPR complex&
  1239. operator/=(const complex<_Tp>& __z)
  1240. {
  1241. const _ComplexT __t = __z.__rep();
  1242. _M_value /= __t;
  1243. return *this;
  1244. }
  1245. _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
  1246. private:
  1247. _ComplexT _M_value;
  1248. };
  1249. /// 26.2.3 complex specializations
  1250. /// complex<double> specialization
  1251. template<>
  1252. class complex<double>
  1253. {
  1254. public:
  1255. typedef double value_type;
  1256. typedef __complex__ double _ComplexT;
  1257. _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
  1258. _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0)
  1259. #if __cplusplus >= 201103L
  1260. : _M_value{ __r, __i } { }
  1261. #else
  1262. {
  1263. __real__ _M_value = __r;
  1264. __imag__ _M_value = __i;
  1265. }
  1266. #endif
  1267. #if __cplusplus >= 201103L
  1268. _GLIBCXX14_CONSTEXPR complex(const complex&) = default;
  1269. #endif
  1270. #if __cplusplus > 202002L
  1271. template<typename _Up>
  1272. explicit(!requires(_Up __u) { value_type{__u}; })
  1273. constexpr complex(const complex<_Up>& __z)
  1274. : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
  1275. #else
  1276. _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
  1277. : _M_value(__z.__rep()) { }
  1278. explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
  1279. #endif
  1280. #if __cplusplus >= 201103L
  1281. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1282. // DR 387. std::complex over-encapsulated.
  1283. __attribute ((__abi_tag__ ("cxx11")))
  1284. constexpr double
  1285. real() const { return __real__ _M_value; }
  1286. __attribute ((__abi_tag__ ("cxx11")))
  1287. constexpr double
  1288. imag() const { return __imag__ _M_value; }
  1289. #else
  1290. double&
  1291. real() { return __real__ _M_value; }
  1292. const double&
  1293. real() const { return __real__ _M_value; }
  1294. double&
  1295. imag() { return __imag__ _M_value; }
  1296. const double&
  1297. imag() const { return __imag__ _M_value; }
  1298. #endif
  1299. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1300. // DR 387. std::complex over-encapsulated.
  1301. _GLIBCXX20_CONSTEXPR void
  1302. real(double __val) { __real__ _M_value = __val; }
  1303. _GLIBCXX20_CONSTEXPR void
  1304. imag(double __val) { __imag__ _M_value = __val; }
  1305. _GLIBCXX20_CONSTEXPR complex&
  1306. operator=(double __d)
  1307. {
  1308. _M_value = __d;
  1309. return *this;
  1310. }
  1311. _GLIBCXX20_CONSTEXPR complex&
  1312. operator+=(double __d)
  1313. {
  1314. _M_value += __d;
  1315. return *this;
  1316. }
  1317. _GLIBCXX20_CONSTEXPR complex&
  1318. operator-=(double __d)
  1319. {
  1320. _M_value -= __d;
  1321. return *this;
  1322. }
  1323. _GLIBCXX20_CONSTEXPR complex&
  1324. operator*=(double __d)
  1325. {
  1326. _M_value *= __d;
  1327. return *this;
  1328. }
  1329. _GLIBCXX20_CONSTEXPR complex&
  1330. operator/=(double __d)
  1331. {
  1332. _M_value /= __d;
  1333. return *this;
  1334. }
  1335. // The compiler will synthesize this, efficiently.
  1336. #if __cplusplus >= 201103L
  1337. _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
  1338. #endif
  1339. template<typename _Tp>
  1340. _GLIBCXX20_CONSTEXPR complex&
  1341. operator=(const complex<_Tp>& __z)
  1342. {
  1343. _M_value = __z.__rep();
  1344. return *this;
  1345. }
  1346. template<typename _Tp>
  1347. _GLIBCXX20_CONSTEXPR complex&
  1348. operator+=(const complex<_Tp>& __z)
  1349. {
  1350. _M_value += __z.__rep();
  1351. return *this;
  1352. }
  1353. template<typename _Tp>
  1354. _GLIBCXX20_CONSTEXPR complex&
  1355. operator-=(const complex<_Tp>& __z)
  1356. {
  1357. _M_value -= __z.__rep();
  1358. return *this;
  1359. }
  1360. template<typename _Tp>
  1361. _GLIBCXX20_CONSTEXPR complex&
  1362. operator*=(const complex<_Tp>& __z)
  1363. {
  1364. const _ComplexT __t = __z.__rep();
  1365. _M_value *= __t;
  1366. return *this;
  1367. }
  1368. template<typename _Tp>
  1369. _GLIBCXX20_CONSTEXPR complex&
  1370. operator/=(const complex<_Tp>& __z)
  1371. {
  1372. const _ComplexT __t = __z.__rep();
  1373. _M_value /= __t;
  1374. return *this;
  1375. }
  1376. _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
  1377. private:
  1378. _ComplexT _M_value;
  1379. };
  1380. /// 26.2.3 complex specializations
  1381. /// complex<long double> specialization
  1382. template<>
  1383. class complex<long double>
  1384. {
  1385. public:
  1386. typedef long double value_type;
  1387. typedef __complex__ long double _ComplexT;
  1388. _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
  1389. _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L,
  1390. long double __i = 0.0L)
  1391. #if __cplusplus >= 201103L
  1392. : _M_value{ __r, __i } { }
  1393. #else
  1394. {
  1395. __real__ _M_value = __r;
  1396. __imag__ _M_value = __i;
  1397. }
  1398. #endif
  1399. #if __cplusplus >= 201103L
  1400. _GLIBCXX14_CONSTEXPR complex(const complex&) = default;
  1401. #endif
  1402. #if __cplusplus > 202002L
  1403. template<typename _Up>
  1404. explicit(!requires(_Up __u) { value_type{__u}; })
  1405. constexpr complex(const complex<_Up>& __z)
  1406. : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
  1407. #else
  1408. _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
  1409. : _M_value(__z.__rep()) { }
  1410. _GLIBCXX_CONSTEXPR complex(const complex<double>& __z)
  1411. : _M_value(__z.__rep()) { }
  1412. #endif
  1413. #if __cplusplus >= 201103L
  1414. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1415. // DR 387. std::complex over-encapsulated.
  1416. __attribute ((__abi_tag__ ("cxx11")))
  1417. constexpr long double
  1418. real() const { return __real__ _M_value; }
  1419. __attribute ((__abi_tag__ ("cxx11")))
  1420. constexpr long double
  1421. imag() const { return __imag__ _M_value; }
  1422. #else
  1423. long double&
  1424. real() { return __real__ _M_value; }
  1425. const long double&
  1426. real() const { return __real__ _M_value; }
  1427. long double&
  1428. imag() { return __imag__ _M_value; }
  1429. const long double&
  1430. imag() const { return __imag__ _M_value; }
  1431. #endif
  1432. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1433. // DR 387. std::complex over-encapsulated.
  1434. _GLIBCXX20_CONSTEXPR void
  1435. real(long double __val) { __real__ _M_value = __val; }
  1436. _GLIBCXX20_CONSTEXPR void
  1437. imag(long double __val) { __imag__ _M_value = __val; }
  1438. _GLIBCXX20_CONSTEXPR complex&
  1439. operator=(long double __r)
  1440. {
  1441. _M_value = __r;
  1442. return *this;
  1443. }
  1444. _GLIBCXX20_CONSTEXPR complex&
  1445. operator+=(long double __r)
  1446. {
  1447. _M_value += __r;
  1448. return *this;
  1449. }
  1450. _GLIBCXX20_CONSTEXPR complex&
  1451. operator-=(long double __r)
  1452. {
  1453. _M_value -= __r;
  1454. return *this;
  1455. }
  1456. _GLIBCXX20_CONSTEXPR complex&
  1457. operator*=(long double __r)
  1458. {
  1459. _M_value *= __r;
  1460. return *this;
  1461. }
  1462. _GLIBCXX20_CONSTEXPR complex&
  1463. operator/=(long double __r)
  1464. {
  1465. _M_value /= __r;
  1466. return *this;
  1467. }
  1468. // The compiler knows how to do this efficiently
  1469. #if __cplusplus >= 201103L
  1470. _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default;
  1471. #endif
  1472. template<typename _Tp>
  1473. _GLIBCXX20_CONSTEXPR complex&
  1474. operator=(const complex<_Tp>& __z)
  1475. {
  1476. _M_value = __z.__rep();
  1477. return *this;
  1478. }
  1479. template<typename _Tp>
  1480. _GLIBCXX20_CONSTEXPR complex&
  1481. operator+=(const complex<_Tp>& __z)
  1482. {
  1483. _M_value += __z.__rep();
  1484. return *this;
  1485. }
  1486. template<typename _Tp>
  1487. _GLIBCXX20_CONSTEXPR complex&
  1488. operator-=(const complex<_Tp>& __z)
  1489. {
  1490. _M_value -= __z.__rep();
  1491. return *this;
  1492. }
  1493. template<typename _Tp>
  1494. _GLIBCXX20_CONSTEXPR complex&
  1495. operator*=(const complex<_Tp>& __z)
  1496. {
  1497. const _ComplexT __t = __z.__rep();
  1498. _M_value *= __t;
  1499. return *this;
  1500. }
  1501. template<typename _Tp>
  1502. _GLIBCXX20_CONSTEXPR complex&
  1503. operator/=(const complex<_Tp>& __z)
  1504. {
  1505. const _ComplexT __t = __z.__rep();
  1506. _M_value /= __t;
  1507. return *this;
  1508. }
  1509. _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
  1510. private:
  1511. _ComplexT _M_value;
  1512. };
  1513. #if __cplusplus > 202002L
  1514. template<typename _Tp>
  1515. struct __complex_type
  1516. { };
  1517. #ifdef __STDCPP_FLOAT16_T__
  1518. template<>
  1519. struct __complex_type<_Float16>
  1520. { typedef __complex__ _Float16 type; };
  1521. #endif
  1522. #ifdef __STDCPP_FLOAT32_T__
  1523. template<>
  1524. struct __complex_type<_Float32>
  1525. { typedef __complex__ _Float32 type; };
  1526. #endif
  1527. #ifdef __STDCPP_FLOAT64_T__
  1528. template<>
  1529. struct __complex_type<_Float64>
  1530. { typedef __complex__ _Float64 type; };
  1531. #endif
  1532. #ifdef __STDCPP_FLOAT128_T__
  1533. template<>
  1534. struct __complex_type<_Float128>
  1535. { typedef __complex__ _Float128 type; };
  1536. #endif
  1537. #ifdef __STDCPP_BFLOAT16_T__
  1538. template<>
  1539. struct __complex_type<__gnu_cxx::__bfloat16_t>
  1540. { typedef __complex__ decltype(0.0bf16) type; };
  1541. #endif
  1542. template<typename _Tp>
  1543. requires requires { typename __complex_type<_Tp>::type; }
  1544. class complex<_Tp>
  1545. {
  1546. public:
  1547. typedef _Tp value_type;
  1548. typedef typename std::__complex_type<_Tp>::type _ComplexT;
  1549. constexpr complex(_ComplexT __z) : _M_value(__z) { }
  1550. constexpr complex(_Tp __r = _Tp(), _Tp __i = _Tp())
  1551. : _M_value{ __r, __i } { }
  1552. template<typename _Up>
  1553. explicit(!requires(_Up __u) { value_type{__u}; })
  1554. constexpr complex(const complex<_Up>& __z)
  1555. : _M_value{ value_type(__z.real()), value_type(__z.imag()) } { }
  1556. constexpr _Tp
  1557. real() const { return __real__ _M_value; }
  1558. constexpr _Tp
  1559. imag() const { return __imag__ _M_value; }
  1560. constexpr void
  1561. real(_Tp __val) { __real__ _M_value = __val; }
  1562. constexpr void
  1563. imag(_Tp __val) { __imag__ _M_value = __val; }
  1564. constexpr complex&
  1565. operator=(_Tp __f)
  1566. {
  1567. _M_value = __f;
  1568. return *this;
  1569. }
  1570. constexpr complex&
  1571. operator+=(_Tp __f)
  1572. {
  1573. _M_value += __f;
  1574. return *this;
  1575. }
  1576. constexpr complex&
  1577. operator-=(_Tp __f)
  1578. {
  1579. _M_value -= __f;
  1580. return *this;
  1581. }
  1582. constexpr complex&
  1583. operator*=(_Tp __f)
  1584. {
  1585. _M_value *= __f;
  1586. return *this;
  1587. }
  1588. constexpr complex&
  1589. operator/=(_Tp __f)
  1590. {
  1591. _M_value /= __f;
  1592. return *this;
  1593. }
  1594. // Let the compiler synthesize the copy and assignment
  1595. // operator. It always does a pretty good job.
  1596. constexpr complex(const complex&) = default;
  1597. constexpr complex& operator=(const complex&) = default;
  1598. template<typename _Up>
  1599. constexpr complex&
  1600. operator=(const complex<_Up>& __z)
  1601. {
  1602. __real__ _M_value = __z.real();
  1603. __imag__ _M_value = __z.imag();
  1604. return *this;
  1605. }
  1606. template<typename _Up>
  1607. constexpr complex&
  1608. operator+=(const complex<_Up>& __z)
  1609. {
  1610. _M_value += __z.__rep();
  1611. return *this;
  1612. }
  1613. template<class _Up>
  1614. constexpr complex&
  1615. operator-=(const complex<_Up>& __z)
  1616. {
  1617. _M_value -= __z.__rep();
  1618. return *this;
  1619. }
  1620. template<class _Up>
  1621. constexpr complex&
  1622. operator*=(const complex<_Up>& __z)
  1623. {
  1624. const _ComplexT __t = __z.__rep();
  1625. _M_value *= __t;
  1626. return *this;
  1627. }
  1628. template<class _Up>
  1629. constexpr complex&
  1630. operator/=(const complex<_Up>& __z)
  1631. {
  1632. const _ComplexT __t = __z.__rep();
  1633. _M_value /= __t;
  1634. return *this;
  1635. }
  1636. constexpr _ComplexT __rep() const { return _M_value; }
  1637. private:
  1638. _ComplexT _M_value;
  1639. };
  1640. #endif
  1641. #if __cplusplus <= 202002L
  1642. // These bits have to be at the end of this file, so that the
  1643. // specializations have all been defined.
  1644. inline _GLIBCXX_CONSTEXPR
  1645. complex<float>::complex(const complex<double>& __z)
  1646. : _M_value(__z.__rep()) { }
  1647. inline _GLIBCXX_CONSTEXPR
  1648. complex<float>::complex(const complex<long double>& __z)
  1649. : _M_value(__z.__rep()) { }
  1650. inline _GLIBCXX_CONSTEXPR
  1651. complex<double>::complex(const complex<long double>& __z)
  1652. : _M_value(__z.__rep()) { }
  1653. #endif
  1654. // Inhibit implicit instantiations for required instantiations,
  1655. // which are defined via explicit instantiations elsewhere.
  1656. // NB: This syntax is a GNU extension.
  1657. #if _GLIBCXX_EXTERN_TEMPLATE
  1658. extern template istream& operator>>(istream&, complex<float>&);
  1659. extern template ostream& operator<<(ostream&, const complex<float>&);
  1660. extern template istream& operator>>(istream&, complex<double>&);
  1661. extern template ostream& operator<<(ostream&, const complex<double>&);
  1662. extern template istream& operator>>(istream&, complex<long double>&);
  1663. extern template ostream& operator<<(ostream&, const complex<long double>&);
  1664. #ifdef _GLIBCXX_USE_WCHAR_T
  1665. extern template wistream& operator>>(wistream&, complex<float>&);
  1666. extern template wostream& operator<<(wostream&, const complex<float>&);
  1667. extern template wistream& operator>>(wistream&, complex<double>&);
  1668. extern template wostream& operator<<(wostream&, const complex<double>&);
  1669. extern template wistream& operator>>(wistream&, complex<long double>&);
  1670. extern template wostream& operator<<(wostream&, const complex<long double>&);
  1671. #endif
  1672. #endif
  1673. /// @} group complex_numbers
  1674. _GLIBCXX_END_NAMESPACE_VERSION
  1675. } // namespace
  1676. #if __cplusplus >= 201103L
  1677. namespace std _GLIBCXX_VISIBILITY(default)
  1678. {
  1679. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  1680. // Forward declarations.
  1681. template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
  1682. template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
  1683. template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
  1684. template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
  1685. template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
  1686. template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
  1687. // DR 595.
  1688. template<typename _Tp> _Tp fabs(const std::complex<_Tp>&);
  1689. template<typename _Tp>
  1690. inline std::complex<_Tp>
  1691. __complex_acos(const std::complex<_Tp>& __z)
  1692. {
  1693. const std::complex<_Tp> __t = std::asin(__z);
  1694. const _Tp __pi_2 = 1.5707963267948966192313216916397514L;
  1695. return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
  1696. }
  1697. #if _GLIBCXX_USE_C99_COMPLEX_TR1
  1698. #if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
  1699. inline __complex__ _Float16
  1700. __complex_acos(__complex__ _Float16 __z)
  1701. { return static_cast<__complex__ _Float16>(__builtin_cacosf(__z)); }
  1702. inline __complex__ _Float16
  1703. __complex_asin(__complex__ _Float16 __z)
  1704. { return static_cast<__complex__ _Float16>(__builtin_casinf(__z)); }
  1705. inline __complex__ _Float16
  1706. __complex_atan(__complex__ _Float16 __z)
  1707. { return static_cast<__complex__ _Float16>(__builtin_catanf(__z)); }
  1708. inline __complex__ _Float16
  1709. __complex_acosh(__complex__ _Float16 __z)
  1710. { return static_cast<__complex__ _Float16>(__builtin_cacoshf(__z)); }
  1711. inline __complex__ _Float16
  1712. __complex_asinh(__complex__ _Float16 __z)
  1713. { return static_cast<__complex__ _Float16>(__builtin_casinhf(__z)); }
  1714. inline __complex__ _Float16
  1715. __complex_atanh(__complex__ _Float16 __z)
  1716. { return static_cast<__complex__ _Float16>(__builtin_catanhf(__z)); }
  1717. #endif
  1718. #if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
  1719. inline __complex__ _Float32
  1720. __complex_acos(__complex__ _Float32 __z)
  1721. { return __builtin_cacosf(__z); }
  1722. inline __complex__ _Float32
  1723. __complex_asin(__complex__ _Float32 __z)
  1724. { return __builtin_casinf(__z); }
  1725. inline __complex__ _Float32
  1726. __complex_atan(__complex__ _Float32 __z)
  1727. { return __builtin_catanf(__z); }
  1728. inline __complex__ _Float32
  1729. __complex_acosh(__complex__ _Float32 __z)
  1730. { return __builtin_cacoshf(__z); }
  1731. inline __complex__ _Float32
  1732. __complex_asinh(__complex__ _Float32 __z)
  1733. { return __builtin_casinhf(__z); }
  1734. inline __complex__ _Float32
  1735. __complex_atanh(__complex__ _Float32 __z)
  1736. { return __builtin_catanhf(__z); }
  1737. #endif
  1738. #if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
  1739. inline __complex__ _Float64
  1740. __complex_acos(__complex__ _Float64 __z)
  1741. { return __builtin_cacos(__z); }
  1742. inline __complex__ _Float64
  1743. __complex_asin(__complex__ _Float64 __z)
  1744. { return __builtin_casin(__z); }
  1745. inline __complex__ _Float64
  1746. __complex_atan(__complex__ _Float64 __z)
  1747. { return __builtin_catan(__z); }
  1748. inline __complex__ _Float64
  1749. __complex_acosh(__complex__ _Float64 __z)
  1750. { return __builtin_cacosh(__z); }
  1751. inline __complex__ _Float64
  1752. __complex_asinh(__complex__ _Float64 __z)
  1753. { return __builtin_casinh(__z); }
  1754. inline __complex__ _Float64
  1755. __complex_atanh(__complex__ _Float64 __z)
  1756. { return __builtin_catanh(__z); }
  1757. #endif
  1758. #if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
  1759. inline __complex__ _Float128
  1760. __complex_acos(__complex__ _Float128 __z)
  1761. { return __builtin_cacosl(__z); }
  1762. inline __complex__ _Float128
  1763. __complex_asin(__complex__ _Float128 __z)
  1764. { return __builtin_casinl(__z); }
  1765. inline __complex__ _Float128
  1766. __complex_atan(__complex__ _Float128 __z)
  1767. { return __builtin_catanl(__z); }
  1768. inline __complex__ _Float128
  1769. __complex_acosh(__complex__ _Float128 __z)
  1770. { return __builtin_cacoshl(__z); }
  1771. inline __complex__ _Float128
  1772. __complex_asinh(__complex__ _Float128 __z)
  1773. { return __builtin_casinhl(__z); }
  1774. inline __complex__ _Float128
  1775. __complex_atanh(__complex__ _Float128 __z)
  1776. { return __builtin_catanhl(__z); }
  1777. #elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
  1778. inline __complex__ _Float128
  1779. __complex_acos(__complex__ _Float128 __z)
  1780. { return __builtin_cacosf128(__z); }
  1781. inline __complex__ _Float128
  1782. __complex_asin(__complex__ _Float128 __z)
  1783. { return __builtin_casinf128(__z); }
  1784. inline __complex__ _Float128
  1785. __complex_atan(__complex__ _Float128 __z)
  1786. { return __builtin_catanf128(__z); }
  1787. inline __complex__ _Float128
  1788. __complex_acosh(__complex__ _Float128 __z)
  1789. { return __builtin_cacoshf128(__z); }
  1790. inline __complex__ _Float128
  1791. __complex_asinh(__complex__ _Float128 __z)
  1792. { return __builtin_casinhf128(__z); }
  1793. inline __complex__ _Float128
  1794. __complex_atanh(__complex__ _Float128 __z)
  1795. { return __builtin_catanhf128(__z); }
  1796. #endif
  1797. #if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
  1798. inline __complex__ decltype(0.0bf16)
  1799. __complex_acos(__complex__ decltype(0.0bf16) __z)
  1800. { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cacosf(__z)); }
  1801. inline __complex__ decltype(0.0bf16)
  1802. __complex_asin(__complex__ decltype(0.0bf16) __z)
  1803. { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_casinf(__z)); }
  1804. inline __complex__ decltype(0.0bf16)
  1805. __complex_atan(__complex__ decltype(0.0bf16) __z)
  1806. { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_catanf(__z)); }
  1807. inline __complex__ decltype(0.0bf16)
  1808. __complex_acosh(__complex__ decltype(0.0bf16) __z)
  1809. { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cacoshf(__z)); }
  1810. inline __complex__ decltype(0.0bf16)
  1811. __complex_asinh(__complex__ decltype(0.0bf16) __z)
  1812. { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_casinhf(__z)); }
  1813. inline __complex__ decltype(0.0bf16)
  1814. __complex_atanh(__complex__ decltype(0.0bf16) __z)
  1815. { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_catanhf(__z)); }
  1816. #endif
  1817. #endif
  1818. #if _GLIBCXX_USE_C99_COMPLEX_TR1
  1819. inline __complex__ float
  1820. __complex_acos(__complex__ float __z)
  1821. { return __builtin_cacosf(__z); }
  1822. inline __complex__ double
  1823. __complex_acos(__complex__ double __z)
  1824. { return __builtin_cacos(__z); }
  1825. inline __complex__ long double
  1826. __complex_acos(const __complex__ long double& __z)
  1827. { return __builtin_cacosl(__z); }
  1828. template<typename _Tp>
  1829. inline std::complex<_Tp>
  1830. acos(const std::complex<_Tp>& __z)
  1831. { return __complex_acos(__z.__rep()); }
  1832. #else
  1833. /// acos(__z) [8.1.2].
  1834. // Effects: Behaves the same as C99 function cacos, defined
  1835. // in subclause 7.3.5.1.
  1836. template<typename _Tp>
  1837. inline std::complex<_Tp>
  1838. acos(const std::complex<_Tp>& __z)
  1839. { return __complex_acos(__z); }
  1840. #endif
  1841. template<typename _Tp>
  1842. inline std::complex<_Tp>
  1843. __complex_asin(const std::complex<_Tp>& __z)
  1844. {
  1845. std::complex<_Tp> __t(-__z.imag(), __z.real());
  1846. __t = std::asinh(__t);
  1847. return std::complex<_Tp>(__t.imag(), -__t.real());
  1848. }
  1849. #if _GLIBCXX_USE_C99_COMPLEX_TR1
  1850. inline __complex__ float
  1851. __complex_asin(__complex__ float __z)
  1852. { return __builtin_casinf(__z); }
  1853. inline __complex__ double
  1854. __complex_asin(__complex__ double __z)
  1855. { return __builtin_casin(__z); }
  1856. inline __complex__ long double
  1857. __complex_asin(const __complex__ long double& __z)
  1858. { return __builtin_casinl(__z); }
  1859. template<typename _Tp>
  1860. inline std::complex<_Tp>
  1861. asin(const std::complex<_Tp>& __z)
  1862. { return __complex_asin(__z.__rep()); }
  1863. #else
  1864. /// asin(__z) [8.1.3].
  1865. // Effects: Behaves the same as C99 function casin, defined
  1866. // in subclause 7.3.5.2.
  1867. template<typename _Tp>
  1868. inline std::complex<_Tp>
  1869. asin(const std::complex<_Tp>& __z)
  1870. { return __complex_asin(__z); }
  1871. #endif
  1872. template<typename _Tp>
  1873. std::complex<_Tp>
  1874. __complex_atan(const std::complex<_Tp>& __z)
  1875. {
  1876. const _Tp __r2 = __z.real() * __z.real();
  1877. const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
  1878. _Tp __num = __z.imag() + _Tp(1.0);
  1879. _Tp __den = __z.imag() - _Tp(1.0);
  1880. __num = __r2 + __num * __num;
  1881. __den = __r2 + __den * __den;
  1882. return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
  1883. _Tp(0.25) * log(__num / __den));
  1884. }
  1885. #if _GLIBCXX_USE_C99_COMPLEX_TR1
  1886. inline __complex__ float
  1887. __complex_atan(__complex__ float __z)
  1888. { return __builtin_catanf(__z); }
  1889. inline __complex__ double
  1890. __complex_atan(__complex__ double __z)
  1891. { return __builtin_catan(__z); }
  1892. inline __complex__ long double
  1893. __complex_atan(const __complex__ long double& __z)
  1894. { return __builtin_catanl(__z); }
  1895. template<typename _Tp>
  1896. inline std::complex<_Tp>
  1897. atan(const std::complex<_Tp>& __z)
  1898. { return __complex_atan(__z.__rep()); }
  1899. #else
  1900. /// atan(__z) [8.1.4].
  1901. // Effects: Behaves the same as C99 function catan, defined
  1902. // in subclause 7.3.5.3.
  1903. template<typename _Tp>
  1904. inline std::complex<_Tp>
  1905. atan(const std::complex<_Tp>& __z)
  1906. { return __complex_atan(__z); }
  1907. #endif
  1908. template<typename _Tp>
  1909. std::complex<_Tp>
  1910. __complex_acosh(const std::complex<_Tp>& __z)
  1911. {
  1912. // Kahan's formula.
  1913. return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0)))
  1914. + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0))));
  1915. }
  1916. #if _GLIBCXX_USE_C99_COMPLEX_TR1
  1917. inline __complex__ float
  1918. __complex_acosh(__complex__ float __z)
  1919. { return __builtin_cacoshf(__z); }
  1920. inline __complex__ double
  1921. __complex_acosh(__complex__ double __z)
  1922. { return __builtin_cacosh(__z); }
  1923. inline __complex__ long double
  1924. __complex_acosh(const __complex__ long double& __z)
  1925. { return __builtin_cacoshl(__z); }
  1926. template<typename _Tp>
  1927. inline std::complex<_Tp>
  1928. acosh(const std::complex<_Tp>& __z)
  1929. { return __complex_acosh(__z.__rep()); }
  1930. #else
  1931. /// acosh(__z) [8.1.5].
  1932. // Effects: Behaves the same as C99 function cacosh, defined
  1933. // in subclause 7.3.6.1.
  1934. template<typename _Tp>
  1935. inline std::complex<_Tp>
  1936. acosh(const std::complex<_Tp>& __z)
  1937. { return __complex_acosh(__z); }
  1938. #endif
  1939. template<typename _Tp>
  1940. std::complex<_Tp>
  1941. __complex_asinh(const std::complex<_Tp>& __z)
  1942. {
  1943. std::complex<_Tp> __t((__z.real() - __z.imag())
  1944. * (__z.real() + __z.imag()) + _Tp(1.0),
  1945. _Tp(2.0) * __z.real() * __z.imag());
  1946. __t = std::sqrt(__t);
  1947. return std::log(__t + __z);
  1948. }
  1949. #if _GLIBCXX_USE_C99_COMPLEX_TR1
  1950. inline __complex__ float
  1951. __complex_asinh(__complex__ float __z)
  1952. { return __builtin_casinhf(__z); }
  1953. inline __complex__ double
  1954. __complex_asinh(__complex__ double __z)
  1955. { return __builtin_casinh(__z); }
  1956. inline __complex__ long double
  1957. __complex_asinh(const __complex__ long double& __z)
  1958. { return __builtin_casinhl(__z); }
  1959. template<typename _Tp>
  1960. inline std::complex<_Tp>
  1961. asinh(const std::complex<_Tp>& __z)
  1962. { return __complex_asinh(__z.__rep()); }
  1963. #else
  1964. /// asinh(__z) [8.1.6].
  1965. // Effects: Behaves the same as C99 function casin, defined
  1966. // in subclause 7.3.6.2.
  1967. template<typename _Tp>
  1968. inline std::complex<_Tp>
  1969. asinh(const std::complex<_Tp>& __z)
  1970. { return __complex_asinh(__z); }
  1971. #endif
  1972. template<typename _Tp>
  1973. std::complex<_Tp>
  1974. __complex_atanh(const std::complex<_Tp>& __z)
  1975. {
  1976. const _Tp __i2 = __z.imag() * __z.imag();
  1977. const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
  1978. _Tp __num = _Tp(1.0) + __z.real();
  1979. _Tp __den = _Tp(1.0) - __z.real();
  1980. __num = __i2 + __num * __num;
  1981. __den = __i2 + __den * __den;
  1982. return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
  1983. _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
  1984. }
  1985. #if _GLIBCXX_USE_C99_COMPLEX_TR1
  1986. inline __complex__ float
  1987. __complex_atanh(__complex__ float __z)
  1988. { return __builtin_catanhf(__z); }
  1989. inline __complex__ double
  1990. __complex_atanh(__complex__ double __z)
  1991. { return __builtin_catanh(__z); }
  1992. inline __complex__ long double
  1993. __complex_atanh(const __complex__ long double& __z)
  1994. { return __builtin_catanhl(__z); }
  1995. template<typename _Tp>
  1996. inline std::complex<_Tp>
  1997. atanh(const std::complex<_Tp>& __z)
  1998. { return __complex_atanh(__z.__rep()); }
  1999. #else
  2000. /// atanh(__z) [8.1.7].
  2001. // Effects: Behaves the same as C99 function catanh, defined
  2002. // in subclause 7.3.6.3.
  2003. template<typename _Tp>
  2004. inline std::complex<_Tp>
  2005. atanh(const std::complex<_Tp>& __z)
  2006. { return __complex_atanh(__z); }
  2007. #endif
  2008. template<typename _Tp>
  2009. inline _Tp
  2010. /// fabs(__z) [8.1.8].
  2011. // Effects: Behaves the same as C99 function cabs, defined
  2012. // in subclause 7.3.8.1.
  2013. fabs(const std::complex<_Tp>& __z)
  2014. { return std::abs(__z); }
  2015. /// Additional overloads [8.1.9].
  2016. template<typename _Tp>
  2017. inline typename __gnu_cxx::__promote<_Tp>::__type
  2018. arg(_Tp __x)
  2019. {
  2020. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  2021. #if (_GLIBCXX11_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
  2022. return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L)
  2023. : __type();
  2024. #else
  2025. return std::arg(std::complex<__type>(__x));
  2026. #endif
  2027. }
  2028. template<typename _Tp>
  2029. _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
  2030. imag(_Tp)
  2031. { return _Tp(); }
  2032. template<typename _Tp>
  2033. _GLIBCXX20_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
  2034. norm(_Tp __x)
  2035. {
  2036. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  2037. return __type(__x) * __type(__x);
  2038. }
  2039. template<typename _Tp>
  2040. _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
  2041. real(_Tp __x)
  2042. { return __x; }
  2043. template<typename _Tp, typename _Up>
  2044. inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
  2045. pow(const std::complex<_Tp>& __x, const _Up& __y)
  2046. {
  2047. typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
  2048. return std::pow(std::complex<__type>(__x), __type(__y));
  2049. }
  2050. template<typename _Tp, typename _Up>
  2051. inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
  2052. pow(const _Tp& __x, const std::complex<_Up>& __y)
  2053. {
  2054. typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
  2055. return std::pow(__type(__x), std::complex<__type>(__y));
  2056. }
  2057. template<typename _Tp, typename _Up>
  2058. inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
  2059. pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
  2060. {
  2061. typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
  2062. return std::pow(std::complex<__type>(__x),
  2063. std::complex<__type>(__y));
  2064. }
  2065. // Forward declarations.
  2066. // DR 781.
  2067. template<typename _Tp>
  2068. std::complex<_Tp> proj(const std::complex<_Tp>&);
  2069. // Generic implementation of std::proj, does not work for infinities.
  2070. template<typename _Tp>
  2071. inline std::complex<_Tp>
  2072. __complex_proj(const std::complex<_Tp>& __z)
  2073. { return __z; }
  2074. #if _GLIBCXX_USE_C99_COMPLEX
  2075. inline complex<float>
  2076. __complex_proj(const complex<float>& __z)
  2077. { return __builtin_cprojf(__z.__rep()); }
  2078. inline complex<double>
  2079. __complex_proj(const complex<double>& __z)
  2080. { return __builtin_cproj(__z.__rep()); }
  2081. inline complex<long double>
  2082. __complex_proj(const complex<long double>& __z)
  2083. { return __builtin_cprojl(__z.__rep()); }
  2084. #if __cplusplus > 202002L
  2085. #if defined(__STDCPP_FLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
  2086. inline __complex__ _Float16
  2087. __complex_proj(__complex__ _Float16 __z)
  2088. { return static_cast<__complex__ _Float16>(__builtin_cprojf(__z)); }
  2089. #endif
  2090. #if defined(__STDCPP_FLOAT32_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
  2091. inline __complex__ _Float32
  2092. __complex_proj(__complex__ _Float32 __z)
  2093. { return __builtin_cprojf(__z); }
  2094. #endif
  2095. #if defined(__STDCPP_FLOAT64_T__) && defined(_GLIBCXX_DOUBLE_IS_IEEE_BINARY64)
  2096. inline __complex__ _Float64
  2097. __complex_proj(__complex__ _Float64 __z)
  2098. { return __builtin_cproj(__z); }
  2099. #endif
  2100. #if defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_LDOUBLE_IS_IEEE_BINARY128)
  2101. inline __complex__ _Float128
  2102. __complex_proj(__complex__ _Float128 __z)
  2103. { return __builtin_cprojl(__z); }
  2104. #elif defined(__STDCPP_FLOAT128_T__) && defined(_GLIBCXX_HAVE_FLOAT128_MATH)
  2105. inline __complex__ _Float128
  2106. __complex_proj(__complex__ _Float128 __z)
  2107. { return __builtin_cprojf128(__z); }
  2108. #endif
  2109. #if defined(__STDCPP_BFLOAT16_T__) && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
  2110. inline __complex__ decltype(0.0bf16)
  2111. __complex_proj(__complex__ decltype(0.0bf16) __z)
  2112. { return static_cast<__complex__ decltype(0.0bf16)>(__builtin_cprojf(__z)); }
  2113. #endif
  2114. template<typename _Tp>
  2115. requires requires { typename __complex_type<_Tp>::type; }
  2116. inline complex<_Tp>
  2117. __complex_proj(const complex<_Tp>& __z)
  2118. { return __complex_proj(__z.__rep()); }
  2119. #endif
  2120. #elif defined _GLIBCXX_USE_C99_MATH_TR1
  2121. inline complex<float>
  2122. __complex_proj(const complex<float>& __z)
  2123. {
  2124. if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
  2125. return complex<float>(__builtin_inff(),
  2126. __builtin_copysignf(0.0f, __z.imag()));
  2127. return __z;
  2128. }
  2129. inline complex<double>
  2130. __complex_proj(const complex<double>& __z)
  2131. {
  2132. if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
  2133. return complex<double>(__builtin_inf(),
  2134. __builtin_copysign(0.0, __z.imag()));
  2135. return __z;
  2136. }
  2137. inline complex<long double>
  2138. __complex_proj(const complex<long double>& __z)
  2139. {
  2140. if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
  2141. return complex<long double>(__builtin_infl(),
  2142. __builtin_copysignl(0.0l, __z.imag()));
  2143. return __z;
  2144. }
  2145. #endif
  2146. template<typename _Tp>
  2147. inline std::complex<_Tp>
  2148. proj(const std::complex<_Tp>& __z)
  2149. { return __complex_proj(__z); }
  2150. // Overload for scalars
  2151. template<typename _Tp>
  2152. inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
  2153. proj(_Tp __x)
  2154. {
  2155. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  2156. return std::proj(std::complex<__type>(__x));
  2157. }
  2158. template<typename _Tp>
  2159. inline _GLIBCXX20_CONSTEXPR
  2160. std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
  2161. conj(_Tp __x)
  2162. {
  2163. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  2164. return std::complex<__type>(__x, -__type());
  2165. }
  2166. #if __cplusplus > 201103L
  2167. inline namespace literals {
  2168. inline namespace complex_literals {
  2169. #pragma GCC diagnostic push
  2170. #pragma GCC diagnostic ignored "-Wliteral-suffix"
  2171. #define __cpp_lib_complex_udls 201309L
  2172. constexpr std::complex<float>
  2173. operator""if(long double __num)
  2174. { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
  2175. constexpr std::complex<float>
  2176. operator""if(unsigned long long __num)
  2177. { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
  2178. constexpr std::complex<double>
  2179. operator""i(long double __num)
  2180. { return std::complex<double>{0.0, static_cast<double>(__num)}; }
  2181. constexpr std::complex<double>
  2182. operator""i(unsigned long long __num)
  2183. { return std::complex<double>{0.0, static_cast<double>(__num)}; }
  2184. constexpr std::complex<long double>
  2185. operator""il(long double __num)
  2186. { return std::complex<long double>{0.0L, __num}; }
  2187. constexpr std::complex<long double>
  2188. operator""il(unsigned long long __num)
  2189. { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; }
  2190. #pragma GCC diagnostic pop
  2191. } // inline namespace complex_literals
  2192. } // inline namespace literals
  2193. #endif // C++14
  2194. _GLIBCXX_END_NAMESPACE_VERSION
  2195. } // namespace
  2196. #endif // C++11
  2197. #endif /* _GLIBCXX_COMPLEX */