gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

683 lines
46KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="cmath — Mathematical functions for complex numbers" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/cmath.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="This module provides access to mathematical functions for complex numbers. The functions in this module accept integers, floating-point numbers or complex numbers as arguments. They will also accep..." />
  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="This module provides access to mathematical functions for complex numbers. The functions in this module accept integers, floating-point numbers or complex numbers as arguments. They will also accep..." />
  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>cmath — Mathematical functions for complex numbers &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="decimal — Decimal fixed point and floating point arithmetic" href="decimal.html" />
  33. <link rel="prev" title="math — Mathematical functions" href="math.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/cmath.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.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="Python 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="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">cmath</span></code> — Mathematical functions for complex numbers</a><ul>
  86. <li><a class="reference internal" href="#conversions-to-and-from-polar-coordinates">Conversions to and from polar coordinates</a></li>
  87. <li><a class="reference internal" href="#power-and-logarithmic-functions">Power and logarithmic functions</a></li>
  88. <li><a class="reference internal" href="#trigonometric-functions">Trigonometric functions</a></li>
  89. <li><a class="reference internal" href="#hyperbolic-functions">Hyperbolic functions</a></li>
  90. <li><a class="reference internal" href="#classification-functions">Classification functions</a></li>
  91. <li><a class="reference internal" href="#constants">Constants</a></li>
  92. </ul>
  93. </li>
  94. </ul>
  95. </div>
  96. <div>
  97. <h4>Previous topic</h4>
  98. <p class="topless"><a href="math.html"
  99. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">math</span></code> — Mathematical functions</a></p>
  100. </div>
  101. <div>
  102. <h4>Next topic</h4>
  103. <p class="topless"><a href="decimal.html"
  104. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">decimal</span></code> — Decimal fixed point and floating point arithmetic</a></p>
  105. </div>
  106. <div role="note" aria-label="source link">
  107. <h3>This Page</h3>
  108. <ul class="this-page-menu">
  109. <li><a href="../bugs.html">Report a Bug</a></li>
  110. <li>
  111. <a href="https://github.com/python/cpython/blob/main/Doc/library/cmath.rst"
  112. rel="nofollow">Show Source
  113. </a>
  114. </li>
  115. </ul>
  116. </div>
  117. </nav>
  118. </div>
  119. </div>
  120. <div class="related" role="navigation" aria-label="related navigation">
  121. <h3>Navigation</h3>
  122. <ul>
  123. <li class="right" style="margin-right: 10px">
  124. <a href="../genindex.html" title="General Index"
  125. accesskey="I">index</a></li>
  126. <li class="right" >
  127. <a href="../py-modindex.html" title="Python Module Index"
  128. >modules</a> |</li>
  129. <li class="right" >
  130. <a href="decimal.html" title="decimal — Decimal fixed point and floating point arithmetic"
  131. accesskey="N">next</a> |</li>
  132. <li class="right" >
  133. <a href="math.html" title="math — Mathematical functions"
  134. accesskey="P">previous</a> |</li>
  135. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  136. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  137. <li class="switchers">
  138. <div class="language_switcher_placeholder"></div>
  139. <div class="version_switcher_placeholder"></div>
  140. </li>
  141. <li>
  142. </li>
  143. <li id="cpython-language-and-version">
  144. <a href="../index.html">3.12.3 Documentation</a> &#187;
  145. </li>
  146. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  147. <li class="nav-item nav-item-2"><a href="numeric.html" accesskey="U">Numeric and Mathematical Modules</a> &#187;</li>
  148. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">cmath</span></code> — Mathematical functions for complex numbers</a></li>
  149. <li class="right">
  150. <div class="inline-search" role="search">
  151. <form class="inline-search" action="../search.html" method="get">
  152. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  153. <input type="submit" value="Go" />
  154. </form>
  155. </div>
  156. |
  157. </li>
  158. <li class="right">
  159. <label class="theme-selector-label">
  160. Theme
  161. <select class="theme-selector" oninput="activateTheme(this.value)">
  162. <option value="auto" selected>Auto</option>
  163. <option value="light">Light</option>
  164. <option value="dark">Dark</option>
  165. </select>
  166. </label> |</li>
  167. </ul>
  168. </div>
  169. <div class="document">
  170. <div class="documentwrapper">
  171. <div class="bodywrapper">
  172. <div class="body" role="main">
  173. <section id="module-cmath">
  174. <span id="cmath-mathematical-functions-for-complex-numbers"></span><h1><a class="reference internal" href="#module-cmath" title="cmath: Mathematical functions for complex numbers."><code class="xref py py-mod docutils literal notranslate"><span class="pre">cmath</span></code></a> — Mathematical functions for complex numbers<a class="headerlink" href="#module-cmath" title="Link to this heading">¶</a></h1>
  175. <hr class="docutils" />
  176. <p>This module provides access to mathematical functions for complex numbers. The
  177. functions in this module accept integers, floating-point numbers or complex
  178. numbers as arguments. They will also accept any Python object that has either a
  179. <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> or a <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> method: these methods are used to
  180. convert the object to a complex or floating-point number, respectively, and
  181. the function is then applied to the result of the conversion.</p>
  182. <div class="admonition note">
  183. <p class="admonition-title">Note</p>
  184. <p>For functions involving branch cuts, we have the problem of deciding how to
  185. define those functions on the cut itself. Following Kahan’s “Branch cuts for
  186. complex elementary functions” paper, as well as Annex G of C99 and later C
  187. standards, we use the sign of zero to distinguish one side of the branch cut
  188. from the other: for a branch cut along (a portion of) the real axis we look
  189. at the sign of the imaginary part, while for a branch cut along the
  190. imaginary axis we look at the sign of the real part.</p>
  191. <p>For example, the <a class="reference internal" href="#cmath.sqrt" title="cmath.sqrt"><code class="xref py py-func docutils literal notranslate"><span class="pre">cmath.sqrt()</span></code></a> function has a branch cut along the
  192. negative real axis. An argument of <code class="docutils literal notranslate"><span class="pre">complex(-2.0,</span> <span class="pre">-0.0)</span></code> is treated as
  193. though it lies <em>below</em> the branch cut, and so gives a result on the negative
  194. imaginary axis:</p>
  195. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">cmath</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="nb">complex</span><span class="p">(</span><span class="o">-</span><span class="mf">2.0</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.0</span><span class="p">))</span>
  196. <span class="go">-1.4142135623730951j</span>
  197. </pre></div>
  198. </div>
  199. <p>But an argument of <code class="docutils literal notranslate"><span class="pre">complex(-2.0,</span> <span class="pre">0.0)</span></code> is treated as though it lies above
  200. the branch cut:</p>
  201. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">cmath</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="nb">complex</span><span class="p">(</span><span class="o">-</span><span class="mf">2.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">))</span>
  202. <span class="go">1.4142135623730951j</span>
  203. </pre></div>
  204. </div>
  205. </div>
  206. <section id="conversions-to-and-from-polar-coordinates">
  207. <h2>Conversions to and from polar coordinates<a class="headerlink" href="#conversions-to-and-from-polar-coordinates" title="Link to this heading">¶</a></h2>
  208. <p>A Python complex number <code class="docutils literal notranslate"><span class="pre">z</span></code> is stored internally using <em>rectangular</em>
  209. or <em>Cartesian</em> coordinates. It is completely determined by its <em>real
  210. part</em> <code class="docutils literal notranslate"><span class="pre">z.real</span></code> and its <em>imaginary part</em> <code class="docutils literal notranslate"><span class="pre">z.imag</span></code>. In other
  211. words:</p>
  212. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">z</span> <span class="o">==</span> <span class="n">z</span><span class="o">.</span><span class="n">real</span> <span class="o">+</span> <span class="n">z</span><span class="o">.</span><span class="n">imag</span><span class="o">*</span><span class="mi">1</span><span class="n">j</span>
  213. </pre></div>
  214. </div>
  215. <p><em>Polar coordinates</em> give an alternative way to represent a complex
  216. number. In polar coordinates, a complex number <em>z</em> is defined by the
  217. modulus <em>r</em> and the phase angle <em>phi</em>. The modulus <em>r</em> is the distance
  218. from <em>z</em> to the origin, while the phase <em>phi</em> is the counterclockwise
  219. angle, measured in radians, from the positive x-axis to the line
  220. segment that joins the origin to <em>z</em>.</p>
  221. <p>The following functions can be used to convert from the native
  222. rectangular coordinates to polar coordinates and back.</p>
  223. <dl class="py function">
  224. <dt class="sig sig-object py" id="cmath.phase">
  225. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">phase</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.phase" title="Link to this definition">¶</a></dt>
  226. <dd><p>Return the phase of <em>x</em> (also known as the <em>argument</em> of <em>x</em>), as a float.
  227. <code class="docutils literal notranslate"><span class="pre">phase(x)</span></code> is equivalent to <code class="docutils literal notranslate"><span class="pre">math.atan2(x.imag,</span> <span class="pre">x.real)</span></code>. The result
  228. lies in the range [-<em>π</em>, <em>π</em>], and the branch cut for this operation lies
  229. along the negative real axis. The sign of the result is the same as the
  230. sign of <code class="docutils literal notranslate"><span class="pre">x.imag</span></code>, even when <code class="docutils literal notranslate"><span class="pre">x.imag</span></code> is zero:</p>
  231. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">phase</span><span class="p">(</span><span class="nb">complex</span><span class="p">(</span><span class="o">-</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">))</span>
  232. <span class="go">3.141592653589793</span>
  233. <span class="gp">&gt;&gt;&gt; </span><span class="n">phase</span><span class="p">(</span><span class="nb">complex</span><span class="p">(</span><span class="o">-</span><span class="mf">1.0</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.0</span><span class="p">))</span>
  234. <span class="go">-3.141592653589793</span>
  235. </pre></div>
  236. </div>
  237. </dd></dl>
  238. <div class="admonition note">
  239. <p class="admonition-title">Note</p>
  240. <p>The modulus (absolute value) of a complex number <em>x</em> can be
  241. computed using the built-in <a class="reference internal" href="functions.html#abs" title="abs"><code class="xref py py-func docutils literal notranslate"><span class="pre">abs()</span></code></a> function. There is no
  242. separate <a class="reference internal" href="#module-cmath" title="cmath: Mathematical functions for complex numbers."><code class="xref py py-mod docutils literal notranslate"><span class="pre">cmath</span></code></a> module function for this operation.</p>
  243. </div>
  244. <dl class="py function">
  245. <dt class="sig sig-object py" id="cmath.polar">
  246. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">polar</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.polar" title="Link to this definition">¶</a></dt>
  247. <dd><p>Return the representation of <em>x</em> in polar coordinates. Returns a
  248. pair <code class="docutils literal notranslate"><span class="pre">(r,</span> <span class="pre">phi)</span></code> where <em>r</em> is the modulus of <em>x</em> and phi is the
  249. phase of <em>x</em>. <code class="docutils literal notranslate"><span class="pre">polar(x)</span></code> is equivalent to <code class="docutils literal notranslate"><span class="pre">(abs(x),</span>
  250. <span class="pre">phase(x))</span></code>.</p>
  251. </dd></dl>
  252. <dl class="py function">
  253. <dt class="sig sig-object py" id="cmath.rect">
  254. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">rect</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">r</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">phi</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.rect" title="Link to this definition">¶</a></dt>
  255. <dd><p>Return the complex number <em>x</em> with polar coordinates <em>r</em> and <em>phi</em>.
  256. Equivalent to <code class="docutils literal notranslate"><span class="pre">r</span> <span class="pre">*</span> <span class="pre">(math.cos(phi)</span> <span class="pre">+</span> <span class="pre">math.sin(phi)*1j)</span></code>.</p>
  257. </dd></dl>
  258. </section>
  259. <section id="power-and-logarithmic-functions">
  260. <h2>Power and logarithmic functions<a class="headerlink" href="#power-and-logarithmic-functions" title="Link to this heading">¶</a></h2>
  261. <dl class="py function">
  262. <dt class="sig sig-object py" id="cmath.exp">
  263. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">exp</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.exp" title="Link to this definition">¶</a></dt>
  264. <dd><p>Return <em>e</em> raised to the power <em>x</em>, where <em>e</em> is the base of natural
  265. logarithms.</p>
  266. </dd></dl>
  267. <dl class="py function">
  268. <dt class="sig sig-object py" id="cmath.log">
  269. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">log</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">base</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#cmath.log" title="Link to this definition">¶</a></dt>
  270. <dd><p>Returns the logarithm of <em>x</em> to the given <em>base</em>. If the <em>base</em> is not
  271. specified, returns the natural logarithm of <em>x</em>. There is one branch cut,
  272. from 0 along the negative real axis to -∞.</p>
  273. </dd></dl>
  274. <dl class="py function">
  275. <dt class="sig sig-object py" id="cmath.log10">
  276. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">log10</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.log10" title="Link to this definition">¶</a></dt>
  277. <dd><p>Return the base-10 logarithm of <em>x</em>. This has the same branch cut as
  278. <a class="reference internal" href="#cmath.log" title="cmath.log"><code class="xref py py-func docutils literal notranslate"><span class="pre">log()</span></code></a>.</p>
  279. </dd></dl>
  280. <dl class="py function">
  281. <dt class="sig sig-object py" id="cmath.sqrt">
  282. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">sqrt</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.sqrt" title="Link to this definition">¶</a></dt>
  283. <dd><p>Return the square root of <em>x</em>. This has the same branch cut as <a class="reference internal" href="#cmath.log" title="cmath.log"><code class="xref py py-func docutils literal notranslate"><span class="pre">log()</span></code></a>.</p>
  284. </dd></dl>
  285. </section>
  286. <section id="trigonometric-functions">
  287. <h2>Trigonometric functions<a class="headerlink" href="#trigonometric-functions" title="Link to this heading">¶</a></h2>
  288. <dl class="py function">
  289. <dt class="sig sig-object py" id="cmath.acos">
  290. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">acos</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.acos" title="Link to this definition">¶</a></dt>
  291. <dd><p>Return the arc cosine of <em>x</em>. There are two branch cuts: One extends right
  292. from 1 along the real axis to ∞. The other extends left from -1 along the
  293. real axis to -∞.</p>
  294. </dd></dl>
  295. <dl class="py function">
  296. <dt class="sig sig-object py" id="cmath.asin">
  297. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">asin</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.asin" title="Link to this definition">¶</a></dt>
  298. <dd><p>Return the arc sine of <em>x</em>. This has the same branch cuts as <a class="reference internal" href="#cmath.acos" title="cmath.acos"><code class="xref py py-func docutils literal notranslate"><span class="pre">acos()</span></code></a>.</p>
  299. </dd></dl>
  300. <dl class="py function">
  301. <dt class="sig sig-object py" id="cmath.atan">
  302. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">atan</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.atan" title="Link to this definition">¶</a></dt>
  303. <dd><p>Return the arc tangent of <em>x</em>. There are two branch cuts: One extends from
  304. <code class="docutils literal notranslate"><span class="pre">1j</span></code> along the imaginary axis to <code class="docutils literal notranslate"><span class="pre">∞j</span></code>. The other extends from <code class="docutils literal notranslate"><span class="pre">-1j</span></code>
  305. along the imaginary axis to <code class="docutils literal notranslate"><span class="pre">-∞j</span></code>.</p>
  306. </dd></dl>
  307. <dl class="py function">
  308. <dt class="sig sig-object py" id="cmath.cos">
  309. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">cos</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.cos" title="Link to this definition">¶</a></dt>
  310. <dd><p>Return the cosine of <em>x</em>.</p>
  311. </dd></dl>
  312. <dl class="py function">
  313. <dt class="sig sig-object py" id="cmath.sin">
  314. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">sin</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.sin" title="Link to this definition">¶</a></dt>
  315. <dd><p>Return the sine of <em>x</em>.</p>
  316. </dd></dl>
  317. <dl class="py function">
  318. <dt class="sig sig-object py" id="cmath.tan">
  319. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">tan</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.tan" title="Link to this definition">¶</a></dt>
  320. <dd><p>Return the tangent of <em>x</em>.</p>
  321. </dd></dl>
  322. </section>
  323. <section id="hyperbolic-functions">
  324. <h2>Hyperbolic functions<a class="headerlink" href="#hyperbolic-functions" title="Link to this heading">¶</a></h2>
  325. <dl class="py function">
  326. <dt class="sig sig-object py" id="cmath.acosh">
  327. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">acosh</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.acosh" title="Link to this definition">¶</a></dt>
  328. <dd><p>Return the inverse hyperbolic cosine of <em>x</em>. There is one branch cut,
  329. extending left from 1 along the real axis to -∞.</p>
  330. </dd></dl>
  331. <dl class="py function">
  332. <dt class="sig sig-object py" id="cmath.asinh">
  333. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">asinh</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.asinh" title="Link to this definition">¶</a></dt>
  334. <dd><p>Return the inverse hyperbolic sine of <em>x</em>. There are two branch cuts:
  335. One extends from <code class="docutils literal notranslate"><span class="pre">1j</span></code> along the imaginary axis to <code class="docutils literal notranslate"><span class="pre">∞j</span></code>. The other
  336. extends from <code class="docutils literal notranslate"><span class="pre">-1j</span></code> along the imaginary axis to <code class="docutils literal notranslate"><span class="pre">-∞j</span></code>.</p>
  337. </dd></dl>
  338. <dl class="py function">
  339. <dt class="sig sig-object py" id="cmath.atanh">
  340. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">atanh</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.atanh" title="Link to this definition">¶</a></dt>
  341. <dd><p>Return the inverse hyperbolic tangent of <em>x</em>. There are two branch cuts: One
  342. extends from <code class="docutils literal notranslate"><span class="pre">1</span></code> along the real axis to <code class="docutils literal notranslate"><span class="pre">∞</span></code>. The other extends from
  343. <code class="docutils literal notranslate"><span class="pre">-1</span></code> along the real axis to <code class="docutils literal notranslate"><span class="pre">-∞</span></code>.</p>
  344. </dd></dl>
  345. <dl class="py function">
  346. <dt class="sig sig-object py" id="cmath.cosh">
  347. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">cosh</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.cosh" title="Link to this definition">¶</a></dt>
  348. <dd><p>Return the hyperbolic cosine of <em>x</em>.</p>
  349. </dd></dl>
  350. <dl class="py function">
  351. <dt class="sig sig-object py" id="cmath.sinh">
  352. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">sinh</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.sinh" title="Link to this definition">¶</a></dt>
  353. <dd><p>Return the hyperbolic sine of <em>x</em>.</p>
  354. </dd></dl>
  355. <dl class="py function">
  356. <dt class="sig sig-object py" id="cmath.tanh">
  357. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">tanh</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.tanh" title="Link to this definition">¶</a></dt>
  358. <dd><p>Return the hyperbolic tangent of <em>x</em>.</p>
  359. </dd></dl>
  360. </section>
  361. <section id="classification-functions">
  362. <h2>Classification functions<a class="headerlink" href="#classification-functions" title="Link to this heading">¶</a></h2>
  363. <dl class="py function">
  364. <dt class="sig sig-object py" id="cmath.isfinite">
  365. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">isfinite</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.isfinite" title="Link to this definition">¶</a></dt>
  366. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if both the real and imaginary parts of <em>x</em> are finite, and
  367. <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>
  368. <div class="versionadded">
  369. <p><span class="versionmodified added">New in version 3.2.</span></p>
  370. </div>
  371. </dd></dl>
  372. <dl class="py function">
  373. <dt class="sig sig-object py" id="cmath.isinf">
  374. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">isinf</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.isinf" title="Link to this definition">¶</a></dt>
  375. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if either the real or the imaginary part of <em>x</em> is an
  376. infinity, and <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>
  377. </dd></dl>
  378. <dl class="py function">
  379. <dt class="sig sig-object py" id="cmath.isnan">
  380. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">isnan</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.isnan" title="Link to this definition">¶</a></dt>
  381. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if either the real or the imaginary part of <em>x</em> is a NaN,
  382. and <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>
  383. </dd></dl>
  384. <dl class="py function">
  385. <dt class="sig sig-object py" id="cmath.isclose">
  386. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">isclose</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">a</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">b</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">rel_tol</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1e-09</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">abs_tol</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0.0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.isclose" title="Link to this definition">¶</a></dt>
  387. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the values <em>a</em> and <em>b</em> are close to each other and
  388. <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>
  389. <p>Whether or not two values are considered close is determined according to
  390. given absolute and relative tolerances.</p>
  391. <p><em>rel_tol</em> is the relative tolerance – it is the maximum allowed difference
  392. between <em>a</em> and <em>b</em>, relative to the larger absolute value of <em>a</em> or <em>b</em>.
  393. For example, to set a tolerance of 5%, pass <code class="docutils literal notranslate"><span class="pre">rel_tol=0.05</span></code>. The default
  394. tolerance is <code class="docutils literal notranslate"><span class="pre">1e-09</span></code>, which assures that the two values are the same
  395. within about 9 decimal digits. <em>rel_tol</em> must be greater than zero.</p>
  396. <p><em>abs_tol</em> is the minimum absolute tolerance – useful for comparisons near
  397. zero. <em>abs_tol</em> must be at least zero.</p>
  398. <p>If no errors occur, the result will be:
  399. <code class="docutils literal notranslate"><span class="pre">abs(a-b)</span> <span class="pre">&lt;=</span> <span class="pre">max(rel_tol</span> <span class="pre">*</span> <span class="pre">max(abs(a),</span> <span class="pre">abs(b)),</span> <span class="pre">abs_tol)</span></code>.</p>
  400. <p>The IEEE 754 special values of <code class="docutils literal notranslate"><span class="pre">NaN</span></code>, <code class="docutils literal notranslate"><span class="pre">inf</span></code>, and <code class="docutils literal notranslate"><span class="pre">-inf</span></code> will be
  401. handled according to IEEE rules. Specifically, <code class="docutils literal notranslate"><span class="pre">NaN</span></code> is not considered
  402. close to any other value, including <code class="docutils literal notranslate"><span class="pre">NaN</span></code>. <code class="docutils literal notranslate"><span class="pre">inf</span></code> and <code class="docutils literal notranslate"><span class="pre">-inf</span></code> are only
  403. considered close to themselves.</p>
  404. <div class="versionadded">
  405. <p><span class="versionmodified added">New in version 3.5.</span></p>
  406. </div>
  407. <div class="admonition seealso">
  408. <p class="admonition-title">See also</p>
  409. <p><span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0485/"><strong>PEP 485</strong></a> – A function for testing approximate equality</p>
  410. </div>
  411. </dd></dl>
  412. </section>
  413. <section id="constants">
  414. <h2>Constants<a class="headerlink" href="#constants" title="Link to this heading">¶</a></h2>
  415. <dl class="py data">
  416. <dt class="sig sig-object py" id="cmath.pi">
  417. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">pi</span></span><a class="headerlink" href="#cmath.pi" title="Link to this definition">¶</a></dt>
  418. <dd><p>The mathematical constant <em>π</em>, as a float.</p>
  419. </dd></dl>
  420. <dl class="py data">
  421. <dt class="sig sig-object py" id="cmath.e">
  422. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">e</span></span><a class="headerlink" href="#cmath.e" title="Link to this definition">¶</a></dt>
  423. <dd><p>The mathematical constant <em>e</em>, as a float.</p>
  424. </dd></dl>
  425. <dl class="py data">
  426. <dt class="sig sig-object py" id="cmath.tau">
  427. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">tau</span></span><a class="headerlink" href="#cmath.tau" title="Link to this definition">¶</a></dt>
  428. <dd><p>The mathematical constant <em>τ</em>, as a float.</p>
  429. <div class="versionadded">
  430. <p><span class="versionmodified added">New in version 3.6.</span></p>
  431. </div>
  432. </dd></dl>
  433. <dl class="py data">
  434. <dt class="sig sig-object py" id="cmath.inf">
  435. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">inf</span></span><a class="headerlink" href="#cmath.inf" title="Link to this definition">¶</a></dt>
  436. <dd><p>Floating-point positive infinity. Equivalent to <code class="docutils literal notranslate"><span class="pre">float('inf')</span></code>.</p>
  437. <div class="versionadded">
  438. <p><span class="versionmodified added">New in version 3.6.</span></p>
  439. </div>
  440. </dd></dl>
  441. <dl class="py data">
  442. <dt class="sig sig-object py" id="cmath.infj">
  443. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">infj</span></span><a class="headerlink" href="#cmath.infj" title="Link to this definition">¶</a></dt>
  444. <dd><p>Complex number with zero real part and positive infinity imaginary
  445. part. Equivalent to <code class="docutils literal notranslate"><span class="pre">complex(0.0,</span> <span class="pre">float('inf'))</span></code>.</p>
  446. <div class="versionadded">
  447. <p><span class="versionmodified added">New in version 3.6.</span></p>
  448. </div>
  449. </dd></dl>
  450. <dl class="py data">
  451. <dt class="sig sig-object py" id="cmath.nan">
  452. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">nan</span></span><a class="headerlink" href="#cmath.nan" title="Link to this definition">¶</a></dt>
  453. <dd><p>A floating-point “not a number” (NaN) value. Equivalent to
  454. <code class="docutils literal notranslate"><span class="pre">float('nan')</span></code>.</p>
  455. <div class="versionadded">
  456. <p><span class="versionmodified added">New in version 3.6.</span></p>
  457. </div>
  458. </dd></dl>
  459. <dl class="py data">
  460. <dt class="sig sig-object py" id="cmath.nanj">
  461. <span class="sig-prename descclassname"><span class="pre">cmath.</span></span><span class="sig-name descname"><span class="pre">nanj</span></span><a class="headerlink" href="#cmath.nanj" title="Link to this definition">¶</a></dt>
  462. <dd><p>Complex number with zero real part and NaN imaginary part. Equivalent to
  463. <code class="docutils literal notranslate"><span class="pre">complex(0.0,</span> <span class="pre">float('nan'))</span></code>.</p>
  464. <div class="versionadded">
  465. <p><span class="versionmodified added">New in version 3.6.</span></p>
  466. </div>
  467. </dd></dl>
  468. <p id="index-1">Note that the selection of functions is similar, but not identical, to that in
  469. module <a class="reference internal" href="math.html#module-math" title="math: Mathematical functions (sin() etc.)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">math</span></code></a>. The reason for having two modules is that some users aren’t
  470. interested in complex numbers, and perhaps don’t even know what they are. They
  471. would rather have <code class="docutils literal notranslate"><span class="pre">math.sqrt(-1)</span></code> raise an exception than return a complex
  472. number. Also note that the functions defined in <a class="reference internal" href="#module-cmath" title="cmath: Mathematical functions for complex numbers."><code class="xref py py-mod docutils literal notranslate"><span class="pre">cmath</span></code></a> always return a
  473. complex number, even if the answer can be expressed as a real number (in which
  474. case the complex number has an imaginary part of zero).</p>
  475. <p>A note on branch cuts: They are curves along which the given function fails to
  476. be continuous. They are a necessary feature of many complex functions. It is
  477. assumed that if you need to compute with complex functions, you will understand
  478. about branch cuts. Consult almost any (not too elementary) book on complex
  479. variables for enlightenment. For information of the proper choice of branch
  480. cuts for numerical purposes, a good reference should be the following:</p>
  481. <div class="admonition seealso">
  482. <p class="admonition-title">See also</p>
  483. <p>Kahan, W: Branch cuts for complex elementary functions; or, Much ado about
  484. nothing’s sign bit. In Iserles, A., and Powell, M. (eds.), The state of the art
  485. in numerical analysis. Clarendon Press (1987) pp165–211.</p>
  486. </div>
  487. </section>
  488. </section>
  489. <div class="clearer"></div>
  490. </div>
  491. </div>
  492. </div>
  493. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  494. <div class="sphinxsidebarwrapper">
  495. <div>
  496. <h3><a href="../contents.html">Table of Contents</a></h3>
  497. <ul>
  498. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">cmath</span></code> — Mathematical functions for complex numbers</a><ul>
  499. <li><a class="reference internal" href="#conversions-to-and-from-polar-coordinates">Conversions to and from polar coordinates</a></li>
  500. <li><a class="reference internal" href="#power-and-logarithmic-functions">Power and logarithmic functions</a></li>
  501. <li><a class="reference internal" href="#trigonometric-functions">Trigonometric functions</a></li>
  502. <li><a class="reference internal" href="#hyperbolic-functions">Hyperbolic functions</a></li>
  503. <li><a class="reference internal" href="#classification-functions">Classification functions</a></li>
  504. <li><a class="reference internal" href="#constants">Constants</a></li>
  505. </ul>
  506. </li>
  507. </ul>
  508. </div>
  509. <div>
  510. <h4>Previous topic</h4>
  511. <p class="topless"><a href="math.html"
  512. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">math</span></code> — Mathematical functions</a></p>
  513. </div>
  514. <div>
  515. <h4>Next topic</h4>
  516. <p class="topless"><a href="decimal.html"
  517. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">decimal</span></code> — Decimal fixed point and floating point arithmetic</a></p>
  518. </div>
  519. <div role="note" aria-label="source link">
  520. <h3>This Page</h3>
  521. <ul class="this-page-menu">
  522. <li><a href="../bugs.html">Report a Bug</a></li>
  523. <li>
  524. <a href="https://github.com/python/cpython/blob/main/Doc/library/cmath.rst"
  525. rel="nofollow">Show Source
  526. </a>
  527. </li>
  528. </ul>
  529. </div>
  530. </div>
  531. <div id="sidebarbutton" title="Collapse sidebar">
  532. <span>«</span>
  533. </div>
  534. </div>
  535. <div class="clearer"></div>
  536. </div>
  537. <div class="related" role="navigation" aria-label="related navigation">
  538. <h3>Navigation</h3>
  539. <ul>
  540. <li class="right" style="margin-right: 10px">
  541. <a href="../genindex.html" title="General Index"
  542. >index</a></li>
  543. <li class="right" >
  544. <a href="../py-modindex.html" title="Python Module Index"
  545. >modules</a> |</li>
  546. <li class="right" >
  547. <a href="decimal.html" title="decimal — Decimal fixed point and floating point arithmetic"
  548. >next</a> |</li>
  549. <li class="right" >
  550. <a href="math.html" title="math — Mathematical functions"
  551. >previous</a> |</li>
  552. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  553. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  554. <li class="switchers">
  555. <div class="language_switcher_placeholder"></div>
  556. <div class="version_switcher_placeholder"></div>
  557. </li>
  558. <li>
  559. </li>
  560. <li id="cpython-language-and-version">
  561. <a href="../index.html">3.12.3 Documentation</a> &#187;
  562. </li>
  563. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  564. <li class="nav-item nav-item-2"><a href="numeric.html" >Numeric and Mathematical Modules</a> &#187;</li>
  565. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">cmath</span></code> — Mathematical functions for complex numbers</a></li>
  566. <li class="right">
  567. <div class="inline-search" role="search">
  568. <form class="inline-search" action="../search.html" method="get">
  569. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  570. <input type="submit" value="Go" />
  571. </form>
  572. </div>
  573. |
  574. </li>
  575. <li class="right">
  576. <label class="theme-selector-label">
  577. Theme
  578. <select class="theme-selector" oninput="activateTheme(this.value)">
  579. <option value="auto" selected>Auto</option>
  580. <option value="light">Light</option>
  581. <option value="dark">Dark</option>
  582. </select>
  583. </label> |</li>
  584. </ul>
  585. </div>
  586. <div class="footer">
  587. &copy;
  588. <a href="../copyright.html">
  589. Copyright
  590. </a>
  591. 2001-2024, Python Software Foundation.
  592. <br />
  593. This page is licensed under the Python Software Foundation License Version 2.
  594. <br />
  595. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  596. <br />
  597. See <a href="/license.html">History and License</a> for more information.<br />
  598. <br />
  599. The Python Software Foundation is a non-profit corporation.
  600. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  601. <br />
  602. <br />
  603. Last updated on Apr 09, 2024 (13:47 UTC).
  604. <a href="/bugs.html">Found a bug</a>?
  605. <br />
  606. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  607. </div>
  608. </body>
  609. </html>
上海开阖软件有限公司 沪ICP备12045867号-1