complex.html 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
  6. <meta property="og:title" content="Complex Number Objects" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/c-api/complex.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Python’s complex number objects are implemented as two distinct types when viewed from the C API: one is the Python object exposed to Python programs, and the other is a C structure which represent..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Python’s complex number objects are implemented as two distinct types when viewed from the C API: one is the Python object exposed to Python programs, and the other is a C structure which represent..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>Complex Number Objects &#8212; Python 3.12.0 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?digest=b37c26da2f7529d09fe70b41c4b2133fe4931a90" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css" />
  21. <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
  22. <script src="../_static/jquery.js"></script>
  23. <script src="../_static/underscore.js"></script>
  24. <script src="../_static/doctools.js"></script>
  25. <script src="../_static/sidebar.js"></script>
  26. <link rel="search" type="application/opensearchdescription+xml"
  27. title="Search within Python 3.12.0 documentation"
  28. href="../_static/opensearch.xml"/>
  29. <link rel="author" title="About these documents" href="../about.html" />
  30. <link rel="index" title="Index" href="../genindex.html" />
  31. <link rel="search" title="Search" href="../search.html" />
  32. <link rel="copyright" title="Copyright" href="../copyright.html" />
  33. <link rel="next" title="Bytes Objects" href="bytes.html" />
  34. <link rel="prev" title="Floating Point Objects" href="float.html" />
  35. <link rel="canonical" href="https://docs.python.org/3/c-api/complex.html" />
  36. <style>
  37. @media only screen {
  38. table.full-width-table {
  39. width: 100%;
  40. }
  41. }
  42. </style>
  43. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  44. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  45. <script type="text/javascript" src="../_static/copybutton.js"></script>
  46. <script type="text/javascript" src="../_static/menu.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#">Complex Number Objects</a><ul>
  86. <li><a class="reference internal" href="#complex-numbers-as-c-structures">Complex Numbers as C Structures</a></li>
  87. <li><a class="reference internal" href="#complex-numbers-as-python-objects">Complex Numbers as Python Objects</a></li>
  88. </ul>
  89. </li>
  90. </ul>
  91. </div>
  92. <div>
  93. <h4>Previous topic</h4>
  94. <p class="topless"><a href="float.html"
  95. title="previous chapter">Floating Point Objects</a></p>
  96. </div>
  97. <div>
  98. <h4>Next topic</h4>
  99. <p class="topless"><a href="bytes.html"
  100. title="next chapter">Bytes Objects</a></p>
  101. </div>
  102. <div role="note" aria-label="source link">
  103. <h3>This Page</h3>
  104. <ul class="this-page-menu">
  105. <li><a href="../bugs.html">Report a Bug</a></li>
  106. <li>
  107. <a href="https://github.com/python/cpython/blob/main/Doc/c-api/complex.rst"
  108. rel="nofollow">Show Source
  109. </a>
  110. </li>
  111. </ul>
  112. </div>
  113. </nav>
  114. </div>
  115. </div>
  116. <div class="related" role="navigation" aria-label="related navigation">
  117. <h3>Navigation</h3>
  118. <ul>
  119. <li class="right" style="margin-right: 10px">
  120. <a href="../genindex.html" title="General Index"
  121. accesskey="I">index</a></li>
  122. <li class="right" >
  123. <a href="../py-modindex.html" title="Python Module Index"
  124. >modules</a> |</li>
  125. <li class="right" >
  126. <a href="bytes.html" title="Bytes Objects"
  127. accesskey="N">next</a> |</li>
  128. <li class="right" >
  129. <a href="float.html" title="Floating Point Objects"
  130. accesskey="P">previous</a> |</li>
  131. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  132. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  133. <li class="switchers">
  134. <div class="language_switcher_placeholder"></div>
  135. <div class="version_switcher_placeholder"></div>
  136. </li>
  137. <li>
  138. </li>
  139. <li id="cpython-language-and-version">
  140. <a href="../index.html">3.12.0 Documentation</a> &#187;
  141. </li>
  142. <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &#187;</li>
  143. <li class="nav-item nav-item-2"><a href="concrete.html" accesskey="U">Concrete Objects Layer</a> &#187;</li>
  144. <li class="nav-item nav-item-this"><a href="">Complex Number Objects</a></li>
  145. <li class="right">
  146. <div class="inline-search" role="search">
  147. <form class="inline-search" action="../search.html" method="get">
  148. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  149. <input type="submit" value="Go" />
  150. </form>
  151. </div>
  152. |
  153. </li>
  154. <li class="right">
  155. <label class="theme-selector-label">
  156. Theme
  157. <select class="theme-selector" oninput="activateTheme(this.value)">
  158. <option value="auto" selected>Auto</option>
  159. <option value="light">Light</option>
  160. <option value="dark">Dark</option>
  161. </select>
  162. </label> |</li>
  163. </ul>
  164. </div>
  165. <div class="document">
  166. <div class="documentwrapper">
  167. <div class="bodywrapper">
  168. <div class="body" role="main">
  169. <section id="complex-number-objects">
  170. <span id="complexobjects"></span><h1>Complex Number Objects<a class="headerlink" href="#complex-number-objects" title="Permalink to this headline">¶</a></h1>
  171. <p id="index-0">Python’s complex number objects are implemented as two distinct types when
  172. viewed from the C API: one is the Python object exposed to Python programs, and
  173. the other is a C structure which represents the actual complex number value.
  174. The API provides functions for working with both.</p>
  175. <section id="complex-numbers-as-c-structures">
  176. <h2>Complex Numbers as C Structures<a class="headerlink" href="#complex-numbers-as-c-structures" title="Permalink to this headline">¶</a></h2>
  177. <p>Note that the functions which accept these structures as parameters and return
  178. them as results do so <em>by value</em> rather than dereferencing them through
  179. pointers. This is consistent throughout the API.</p>
  180. <dl class="c type">
  181. <dt class="sig sig-object c" id="c.Py_complex">
  182. <span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_complex</span></span></span><a class="headerlink" href="#c.Py_complex" title="Permalink to this definition">¶</a><br /></dt>
  183. <dd><p>The C structure which corresponds to the value portion of a Python complex
  184. number object. Most of the functions for dealing with complex number objects
  185. use structures of this type as input or output values, as appropriate. It is
  186. defined as:</p>
  187. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">typedef</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="p">{</span>
  188. <span class="w"> </span><span class="kt">double</span><span class="w"> </span><span class="n">real</span><span class="p">;</span>
  189. <span class="w"> </span><span class="kt">double</span><span class="w"> </span><span class="n">imag</span><span class="p">;</span>
  190. <span class="p">}</span><span class="w"> </span><span class="n">Py_complex</span><span class="p">;</span>
  191. </pre></div>
  192. </div>
  193. </dd></dl>
  194. <dl class="c function">
  195. <dt class="sig sig-object c" id="c._Py_c_sum">
  196. <a class="reference internal" href="#c.Py_complex" title="Py_complex"><span class="n"><span class="pre">Py_complex</span></span></a><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">_Py_c_sum</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_complex" title="Py_complex"><span class="n"><span class="pre">Py_complex</span></span></a><span class="w"> </span><span class="n"><span class="pre">left</span></span>, <a class="reference internal" href="#c.Py_complex" title="Py_complex"><span class="n"><span class="pre">Py_complex</span></span></a><span class="w"> </span><span class="n"><span class="pre">right</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c._Py_c_sum" title="Permalink to this definition">¶</a><br /></dt>
  197. <dd><p>Return the sum of two complex numbers, using the C <a class="reference internal" href="#c.Py_complex" title="Py_complex"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_complex</span></code></a>
  198. representation.</p>
  199. </dd></dl>
  200. <dl class="c function">
  201. <dt class="sig sig-object c" id="c._Py_c_diff">
  202. <a class="reference internal" href="#c.Py_complex" title="Py_complex"><span class="n"><span class="pre">Py_complex</span></span></a><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">_Py_c_diff</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_complex" title="Py_complex"><span class="n"><span class="pre">Py_complex</span></span></a><span class="w"> </span><span class="n"><span class="pre">left</span></span>, <a class="reference internal" href="#c.Py_complex" title="Py_complex"><span class="n"><span class="pre">Py_complex</span></span></a><span class="w"> </span><span class="n"><span class="pre">right</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c._Py_c_diff" title="Permalink to this definition">¶</a><br /></dt>
  203. <dd><p>Return the difference between two complex numbers, using the C
  204. <a class="reference internal" href="#c.Py_complex" title="Py_complex"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_complex</span></code></a> representation.</p>
  205. </dd></dl>
  206. <dl class="c function">
  207. <dt class="sig sig-object c" id="c._Py_c_neg">
  208. <a class="reference internal" href="#c.Py_complex" title="Py_complex"><span class="n"><span class="pre">Py_complex</span></span></a><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">_Py_c_neg</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_complex" title="Py_complex"><span class="n"><span class="pre">Py_complex</span></span></a><span class="w"> </span><span class="n"><span class="pre">num</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c._Py_c_neg" title="Permalink to this definition">¶</a><br /></dt>
  209. <dd><p>Return the negation of the complex number <em>num</em>, using the C
  210. <a class="reference internal" href="#c.Py_complex" title="Py_complex"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_complex</span></code></a> representation.</p>
  211. </dd></dl>
  212. <dl class="c function">
  213. <dt class="sig sig-object c" id="c._Py_c_prod">
  214. <a class="reference internal" href="#c.Py_complex" title="Py_complex"><span class="n"><span class="pre">Py_complex</span></span></a><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">_Py_c_prod</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_complex" title="Py_complex"><span class="n"><span class="pre">Py_complex</span></span></a><span class="w"> </span><span class="n"><span class="pre">left</span></span>, <a class="reference internal" href="#c.Py_complex" title="Py_complex"><span class="n"><span class="pre">Py_complex</span></span></a><span class="w"> </span><span class="n"><span class="pre">right</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c._Py_c_prod" title="Permalink to this definition">¶</a><br /></dt>
  215. <dd><p>Return the product of two complex numbers, using the C <a class="reference internal" href="#c.Py_complex" title="Py_complex"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_complex</span></code></a>
  216. representation.</p>
  217. </dd></dl>
  218. <dl class="c function">
  219. <dt class="sig sig-object c" id="c._Py_c_quot">
  220. <a class="reference internal" href="#c.Py_complex" title="Py_complex"><span class="n"><span class="pre">Py_complex</span></span></a><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">_Py_c_quot</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_complex" title="Py_complex"><span class="n"><span class="pre">Py_complex</span></span></a><span class="w"> </span><span class="n"><span class="pre">dividend</span></span>, <a class="reference internal" href="#c.Py_complex" title="Py_complex"><span class="n"><span class="pre">Py_complex</span></span></a><span class="w"> </span><span class="n"><span class="pre">divisor</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c._Py_c_quot" title="Permalink to this definition">¶</a><br /></dt>
  221. <dd><p>Return the quotient of two complex numbers, using the C <a class="reference internal" href="#c.Py_complex" title="Py_complex"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_complex</span></code></a>
  222. representation.</p>
  223. <p>If <em>divisor</em> is null, this method returns zero and sets
  224. <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> to <code class="xref c c-macro docutils literal notranslate"><span class="pre">EDOM</span></code>.</p>
  225. </dd></dl>
  226. <dl class="c function">
  227. <dt class="sig sig-object c" id="c._Py_c_pow">
  228. <a class="reference internal" href="#c.Py_complex" title="Py_complex"><span class="n"><span class="pre">Py_complex</span></span></a><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">_Py_c_pow</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_complex" title="Py_complex"><span class="n"><span class="pre">Py_complex</span></span></a><span class="w"> </span><span class="n"><span class="pre">num</span></span>, <a class="reference internal" href="#c.Py_complex" title="Py_complex"><span class="n"><span class="pre">Py_complex</span></span></a><span class="w"> </span><span class="n"><span class="pre">exp</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c._Py_c_pow" title="Permalink to this definition">¶</a><br /></dt>
  229. <dd><p>Return the exponentiation of <em>num</em> by <em>exp</em>, using the C <a class="reference internal" href="#c.Py_complex" title="Py_complex"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_complex</span></code></a>
  230. representation.</p>
  231. <p>If <em>num</em> is null and <em>exp</em> is not a positive real number,
  232. this method returns zero and sets <code class="xref c c-data docutils literal notranslate"><span class="pre">errno</span></code> to <code class="xref c c-macro docutils literal notranslate"><span class="pre">EDOM</span></code>.</p>
  233. </dd></dl>
  234. </section>
  235. <section id="complex-numbers-as-python-objects">
  236. <h2>Complex Numbers as Python Objects<a class="headerlink" href="#complex-numbers-as-python-objects" title="Permalink to this headline">¶</a></h2>
  237. <dl class="c type">
  238. <dt class="sig sig-object c" id="c.PyComplexObject">
  239. <span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyComplexObject</span></span></span><a class="headerlink" href="#c.PyComplexObject" title="Permalink to this definition">¶</a><br /></dt>
  240. <dd><p>This subtype of <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyObject</span></code></a> represents a Python complex number object.</p>
  241. </dd></dl>
  242. <dl class="c var">
  243. <dt class="sig sig-object c" id="c.PyComplex_Type">
  244. <a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject"><span class="n"><span class="pre">PyTypeObject</span></span></a><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyComplex_Type</span></span></span><a class="headerlink" href="#c.PyComplex_Type" title="Permalink to this definition">¶</a><br /></dt>
  245. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>This instance of <a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyTypeObject</span></code></a> represents the Python complex number
  246. type. It is the same object as <a class="reference internal" href="../library/functions.html#complex" title="complex"><code class="xref py py-class docutils literal notranslate"><span class="pre">complex</span></code></a> in the Python layer.</p>
  247. </dd></dl>
  248. <dl class="c function">
  249. <dt class="sig sig-object c" id="c.PyComplex_Check">
  250. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyComplex_Check</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">p</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyComplex_Check" title="Permalink to this definition">¶</a><br /></dt>
  251. <dd><p>Return true if its argument is a <a class="reference internal" href="#c.PyComplexObject" title="PyComplexObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyComplexObject</span></code></a> or a subtype of
  252. <a class="reference internal" href="#c.PyComplexObject" title="PyComplexObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyComplexObject</span></code></a>. This function always succeeds.</p>
  253. </dd></dl>
  254. <dl class="c function">
  255. <dt class="sig sig-object c" id="c.PyComplex_CheckExact">
  256. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyComplex_CheckExact</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">p</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyComplex_CheckExact" title="Permalink to this definition">¶</a><br /></dt>
  257. <dd><p>Return true if its argument is a <a class="reference internal" href="#c.PyComplexObject" title="PyComplexObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyComplexObject</span></code></a>, but not a subtype of
  258. <a class="reference internal" href="#c.PyComplexObject" title="PyComplexObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyComplexObject</span></code></a>. This function always succeeds.</p>
  259. </dd></dl>
  260. <dl class="c function">
  261. <dt class="sig sig-object c" id="c.PyComplex_FromCComplex">
  262. <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyComplex_FromCComplex</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_complex" title="Py_complex"><span class="n"><span class="pre">Py_complex</span></span></a><span class="w"> </span><span class="n"><span class="pre">v</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyComplex_FromCComplex" title="Permalink to this definition">¶</a><br /></dt>
  263. <dd><em class="refcount">Return value: New reference.</em><p>Create a new Python complex number object from a C <a class="reference internal" href="#c.Py_complex" title="Py_complex"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_complex</span></code></a> value.</p>
  264. </dd></dl>
  265. <dl class="c function">
  266. <dt class="sig sig-object c" id="c.PyComplex_FromDoubles">
  267. <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyComplex_FromDoubles</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">double</span></span><span class="w"> </span><span class="n"><span class="pre">real</span></span>, <span class="kt"><span class="pre">double</span></span><span class="w"> </span><span class="n"><span class="pre">imag</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyComplex_FromDoubles" title="Permalink to this definition">¶</a><br /></dt>
  268. <dd><em class="refcount">Return value: New reference.</em><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Return a new <a class="reference internal" href="#c.PyComplexObject" title="PyComplexObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyComplexObject</span></code></a> object from <em>real</em> and <em>imag</em>.</p>
  269. </dd></dl>
  270. <dl class="c function">
  271. <dt class="sig sig-object c" id="c.PyComplex_RealAsDouble">
  272. <span class="kt"><span class="pre">double</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyComplex_RealAsDouble</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">op</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyComplex_RealAsDouble" title="Permalink to this definition">¶</a><br /></dt>
  273. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Return the real part of <em>op</em> as a C <span class="c-expr sig sig-inline c"><span class="kt">double</span></span>.</p>
  274. </dd></dl>
  275. <dl class="c function">
  276. <dt class="sig sig-object c" id="c.PyComplex_ImagAsDouble">
  277. <span class="kt"><span class="pre">double</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyComplex_ImagAsDouble</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">op</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyComplex_ImagAsDouble" title="Permalink to this definition">¶</a><br /></dt>
  278. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Return the imaginary part of <em>op</em> as a C <span class="c-expr sig sig-inline c"><span class="kt">double</span></span>.</p>
  279. </dd></dl>
  280. <dl class="c function">
  281. <dt class="sig sig-object c" id="c.PyComplex_AsCComplex">
  282. <a class="reference internal" href="#c.Py_complex" title="Py_complex"><span class="n"><span class="pre">Py_complex</span></span></a><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyComplex_AsCComplex</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">op</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyComplex_AsCComplex" title="Permalink to this definition">¶</a><br /></dt>
  283. <dd><p>Return the <a class="reference internal" href="#c.Py_complex" title="Py_complex"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_complex</span></code></a> value of the complex number <em>op</em>.</p>
  284. <p>If <em>op</em> is not a Python complex number object but has a <a class="reference internal" href="../reference/datamodel.html#object.__complex__" title="object.__complex__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__complex__()</span></code></a>
  285. method, this method will first be called to convert <em>op</em> to a Python complex
  286. number object. If <code class="xref py py-meth docutils literal notranslate"><span class="pre">__complex__()</span></code> is not defined then it falls back to
  287. <a class="reference internal" href="../reference/datamodel.html#object.__float__" title="object.__float__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__float__()</span></code></a>. If <code class="xref py py-meth docutils literal notranslate"><span class="pre">__float__()</span></code> is not defined then it falls back
  288. to <a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__index__()</span></code></a>. Upon failure, this method returns <code class="docutils literal notranslate"><span class="pre">-1.0</span></code> as a real
  289. value.</p>
  290. <div class="versionchanged">
  291. <p><span class="versionmodified changed">Changed in version 3.8: </span>Use <a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__index__()</span></code></a> if available.</p>
  292. </div>
  293. </dd></dl>
  294. </section>
  295. </section>
  296. <div class="clearer"></div>
  297. </div>
  298. </div>
  299. </div>
  300. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  301. <div class="sphinxsidebarwrapper">
  302. <div>
  303. <h3><a href="../contents.html">Table of Contents</a></h3>
  304. <ul>
  305. <li><a class="reference internal" href="#">Complex Number Objects</a><ul>
  306. <li><a class="reference internal" href="#complex-numbers-as-c-structures">Complex Numbers as C Structures</a></li>
  307. <li><a class="reference internal" href="#complex-numbers-as-python-objects">Complex Numbers as Python Objects</a></li>
  308. </ul>
  309. </li>
  310. </ul>
  311. </div>
  312. <div>
  313. <h4>Previous topic</h4>
  314. <p class="topless"><a href="float.html"
  315. title="previous chapter">Floating Point Objects</a></p>
  316. </div>
  317. <div>
  318. <h4>Next topic</h4>
  319. <p class="topless"><a href="bytes.html"
  320. title="next chapter">Bytes Objects</a></p>
  321. </div>
  322. <div role="note" aria-label="source link">
  323. <h3>This Page</h3>
  324. <ul class="this-page-menu">
  325. <li><a href="../bugs.html">Report a Bug</a></li>
  326. <li>
  327. <a href="https://github.com/python/cpython/blob/main/Doc/c-api/complex.rst"
  328. rel="nofollow">Show Source
  329. </a>
  330. </li>
  331. </ul>
  332. </div>
  333. </div>
  334. </div>
  335. <div class="clearer"></div>
  336. </div>
  337. <div class="related" role="navigation" aria-label="related navigation">
  338. <h3>Navigation</h3>
  339. <ul>
  340. <li class="right" style="margin-right: 10px">
  341. <a href="../genindex.html" title="General Index"
  342. >index</a></li>
  343. <li class="right" >
  344. <a href="../py-modindex.html" title="Python Module Index"
  345. >modules</a> |</li>
  346. <li class="right" >
  347. <a href="bytes.html" title="Bytes Objects"
  348. >next</a> |</li>
  349. <li class="right" >
  350. <a href="float.html" title="Floating Point Objects"
  351. >previous</a> |</li>
  352. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  353. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  354. <li class="switchers">
  355. <div class="language_switcher_placeholder"></div>
  356. <div class="version_switcher_placeholder"></div>
  357. </li>
  358. <li>
  359. </li>
  360. <li id="cpython-language-and-version">
  361. <a href="../index.html">3.12.0 Documentation</a> &#187;
  362. </li>
  363. <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &#187;</li>
  364. <li class="nav-item nav-item-2"><a href="concrete.html" >Concrete Objects Layer</a> &#187;</li>
  365. <li class="nav-item nav-item-this"><a href="">Complex Number Objects</a></li>
  366. <li class="right">
  367. <div class="inline-search" role="search">
  368. <form class="inline-search" action="../search.html" method="get">
  369. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  370. <input type="submit" value="Go" />
  371. </form>
  372. </div>
  373. |
  374. </li>
  375. <li class="right">
  376. <label class="theme-selector-label">
  377. Theme
  378. <select class="theme-selector" oninput="activateTheme(this.value)">
  379. <option value="auto" selected>Auto</option>
  380. <option value="light">Light</option>
  381. <option value="dark">Dark</option>
  382. </select>
  383. </label> |</li>
  384. </ul>
  385. </div>
  386. <div class="footer">
  387. &copy; <a href="../copyright.html">Copyright</a> 2001-2023, Python Software Foundation.
  388. <br />
  389. This page is licensed under the Python Software Foundation License Version 2.
  390. <br />
  391. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  392. <br />
  393. See <a href="/license.html">History and License</a> for more information.<br />
  394. <br />
  395. The Python Software Foundation is a non-profit corporation.
  396. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  397. <br />
  398. <br />
  399. Last updated on Oct 02, 2023.
  400. <a href="/bugs.html">Found a bug</a>?
  401. <br />
  402. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
  403. </div>
  404. </body>
  405. </html>