parthtamu/rag-code-assistant
0
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4 <head>5 <meta charset="utf-8" />6 <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="cmath — Mathematical functions for complex numbers" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/cmath.html" />10<meta property="og:site_name" content="Python documentation" />11<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..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_cmath_a01a6ef5.png" />15<meta property="og:image:alt" 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..." />16<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..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20 <title>cmath — Mathematical functions for complex numbers — Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21 22 <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23 <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24 <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25 <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26 <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27 28 <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29 <script src="../_static/doctools.js?v=9bcbadda"></script>30 <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31 <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32 33 <script src="../_static/sidebar.js"></script>34 35 <link rel="search" type="application/opensearchdescription+xml"36 title="Search within Python 3.15.0a6 documentation"37 href="../_static/opensearch.xml"/>38 <link rel="author" title="About these documents" href="../about.html" />39 <link rel="index" title="Index" href="../genindex.html" />40 <link rel="search" title="Search" href="../search.html" />41 <link rel="copyright" title="Copyright" href="../copyright.html" />42 <link rel="next" title="decimal — Decimal fixed-point and floating-point arithmetic" href="decimal.html" />43 <link rel="prev" title="math.integer — integer-specific mathematics functions" href="math.integer.html" />44 45 46 <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47 48 <link rel="canonical" href="https://docs.python.org/3/library/cmath.html">49 50 51 52 53 <style>54 @media only screen {55 table.full-width-table {56 width: 100%;57 }58 }59 </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61 <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62 <script type="text/javascript" src="../_static/copybutton.js"></script>63 <script type="text/javascript" src="../_static/menu.js"></script>64 <script type="text/javascript" src="../_static/search-focus.js"></script>65 <script type="text/javascript" src="../_static/themetoggle.js"></script> 66 <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67 <meta name="readthedocs-addons-api-version" content="1">68 69 </head>70<body>71<div class="mobile-nav">72 <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73 aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74 <nav class="nav-content" role="navigation">75 <label for="menuToggler" class="toggler__label">76 <span></span>77 </label>78 <span class="nav-items-wrapper">79 <a href="https://www.python.org/" class="nav-logo">80 <img src="../_static/py.svg" alt="Python logo">81 </a>82 <span class="version_switcher_placeholder"></span>83 <form role="search" class="search" action="../search.html" method="get">84 <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85 <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>86 </svg>87 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88 <input type="submit" value="Go">89 </form>90 </span>91 </nav>92 <div class="menu-wrapper">93 <nav class="menu" role="navigation" aria-label="main navigation">94 <div class="language_switcher_placeholder"></div>95 96<label class="theme-selector-label">97 Theme98 <select class="theme-selector" oninput="activateTheme(this.value)">99 <option value="auto" selected>Auto</option>100 <option value="light">Light</option>101 <option value="dark">Dark</option>102 </select>103</label>104 <div>105 <h3><a href="../contents.html">Table of Contents</a></h3>106 <ul>107<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>108<li><a class="reference internal" href="#conversions-to-and-from-polar-coordinates">Conversions to and from polar coordinates</a></li>109<li><a class="reference internal" href="#power-and-logarithmic-functions">Power and logarithmic functions</a></li>110<li><a class="reference internal" href="#trigonometric-functions">Trigonometric functions</a></li>111<li><a class="reference internal" href="#hyperbolic-functions">Hyperbolic functions</a></li>112<li><a class="reference internal" href="#classification-functions">Classification functions</a></li>113<li><a class="reference internal" href="#constants">Constants</a></li>114</ul>115</li>116</ul>117 118 </div>119 <div>120 <h4>Previous topic</h4>121 <p class="topless"><a href="math.integer.html"122 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">math.integer</span></code> — integer-specific mathematics functions</a></p>123 </div>124 <div>125 <h4>Next topic</h4>126 <p class="topless"><a href="decimal.html"127 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>128 </div>129 <script>130 document.addEventListener('DOMContentLoaded', () => {131 const title = document.querySelector('meta[property="og:title"]').content;132 const elements = document.querySelectorAll('.improvepage');133 const pageurl = window.location.href.split('?')[0];134 elements.forEach(element => {135 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));136 url.searchParams.set('pagetitle', title);137 url.searchParams.set('pageurl', pageurl);138 url.searchParams.set('pagesource', "library/cmath.rst");139 element.href = url.toString();140 });141 });142 </script>143 <div role="note" aria-label="source link">144 <h3>This page</h3>145 <ul class="this-page-menu">146 <li><a href="../bugs.html">Report a bug</a></li>147 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>148 <li>149 <a href="https://github.com/python/cpython/blob/main/Doc/library/cmath.rst?plain=1"150 rel="nofollow">Show source151 </a>152 </li>153 154 </ul>155 </div>156 </nav>157 </div>158</div>159 160 161 <div class="related" role="navigation" aria-label="Related">162 <h3>Navigation</h3>163 <ul>164 <li class="right" style="margin-right: 10px">165 <a href="../genindex.html" title="General Index"166 accesskey="I">index</a></li>167 <li class="right" >168 <a href="../py-modindex.html" title="Python Module Index"169 >modules</a> |</li>170 <li class="right" >171 <a href="decimal.html" title="decimal — Decimal fixed-point and floating-point arithmetic"172 accesskey="N">next</a> |</li>173 <li class="right" >174 <a href="math.integer.html" title="math.integer — integer-specific mathematics functions"175 accesskey="P">previous</a> |</li>176 177 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>178 <li><a href="https://www.python.org/">Python</a> »</li>179 <li class="switchers">180 <div class="language_switcher_placeholder"></div>181 <div class="version_switcher_placeholder"></div>182 </li>183 <li>184 185 </li>186 <li id="cpython-language-and-version">187 <a href="../index.html">3.15.0a6 Documentation</a> »188 </li>189 190 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>191 <li class="nav-item nav-item-2"><a href="numeric.html" accesskey="U">Numeric and Mathematical Modules</a> »</li>192 <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>193 <li class="right">194 195 196 <div class="inline-search" role="search">197 <form class="inline-search" action="../search.html" method="get">198 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">199 <input type="submit" value="Go">200 </form>201 </div>202 |203 </li>204 <li class="right">205<label class="theme-selector-label">206 Theme207 <select class="theme-selector" oninput="activateTheme(this.value)">208 <option value="auto" selected>Auto</option>209 <option value="light">Light</option>210 <option value="dark">Dark</option>211 </select>212</label> |</li>213 214 </ul>215 </div> 216 217 <div class="document">218 <div class="documentwrapper">219 <div class="bodywrapper">220 <div class="body" role="main">221 222 <section id="module-cmath">223<span id="cmath-mathematical-functions-for-complex-numbers"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">cmath</span></code> — Mathematical functions for complex numbers<a class="headerlink" href="#module-cmath" title="Link to this heading">¶</a></h1>224<hr class="docutils" />225<p>This module provides access to mathematical functions for complex numbers. The226functions in this module accept integers, floating-point numbers or complex227numbers as arguments. They will also accept any Python object that has either a228<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 to229convert the object to a complex or floating-point number, respectively, and230the function is then applied to the result of the conversion.</p>231<div class="admonition note">232<p class="admonition-title">Note</p>233<p>For functions involving branch cuts, we have the problem of deciding how to234define those functions on the cut itself. Following Kahan’s “Branch cuts for235complex elementary functions” paper, as well as Annex G of C99 and later C236standards, we use the sign of zero to distinguish one side of the branch cut237from the other: for a branch cut along (a portion of) the real axis we look238at the sign of the imaginary part, while for a branch cut along the239imaginary axis we look at the sign of the real part.</p>240<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 the241negative real axis. An argument of <code class="docutils literal notranslate"><span class="pre">-2-0j</span></code> is treated as242though it lies <em>below</em> the branch cut, and so gives a result on the negative243imaginary axis:</p>244<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">cmath</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="o">-</span><span class="mi">0</span><span class="n">j</span><span class="p">)</span>245<span class="go">-1.4142135623730951j</span>246</pre></div>247</div>248<p>But an argument of <code class="docutils literal notranslate"><span class="pre">-2+0j</span></code> is treated as though it lies above249the branch cut:</p>250<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">cmath</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="o">+</span><span class="mi">0</span><span class="n">j</span><span class="p">)</span>251<span class="go">1.4142135623730951j</span>252</pre></div>253</div>254</div>255<table class="docutils align-default">256<tbody>257<tr class="row-odd"><td colspan="2"><p><strong>Conversions to and from polar coordinates</strong></p></td>258</tr>259<tr class="row-even"><td><p><a class="reference internal" href="#cmath.phase" title="cmath.phase"><code class="xref py py-func docutils literal notranslate"><span class="pre">phase(z)</span></code></a></p></td>260<td><p>Return the phase of <em>z</em></p></td>261</tr>262<tr class="row-odd"><td><p><a class="reference internal" href="#cmath.polar" title="cmath.polar"><code class="xref py py-func docutils literal notranslate"><span class="pre">polar(z)</span></code></a></p></td>263<td><p>Return the representation of <em>z</em> in polar coordinates</p></td>264</tr>265<tr class="row-even"><td><p><a class="reference internal" href="#cmath.rect" title="cmath.rect"><code class="xref py py-func docutils literal notranslate"><span class="pre">rect(r,</span> <span class="pre">phi)</span></code></a></p></td>266<td><p>Return the complex number <em>z</em> with polar coordinates <em>r</em> and <em>phi</em></p></td>267</tr>268<tr class="row-odd"><td colspan="2"><p><strong>Power and logarithmic functions</strong></p></td>269</tr>270<tr class="row-even"><td><p><a class="reference internal" href="#cmath.exp" title="cmath.exp"><code class="xref py py-func docutils literal notranslate"><span class="pre">exp(z)</span></code></a></p></td>271<td><p>Return <em>e</em> raised to the power <em>z</em></p></td>272</tr>273<tr class="row-odd"><td><p><a class="reference internal" href="#cmath.log" title="cmath.log"><code class="xref py py-func docutils literal notranslate"><span class="pre">log(z[,</span> <span class="pre">base])</span></code></a></p></td>274<td><p>Return the logarithm of <em>z</em> to the given <em>base</em> (<em>e</em> by default)</p></td>275</tr>276<tr class="row-even"><td><p><a class="reference internal" href="#cmath.log10" title="cmath.log10"><code class="xref py py-func docutils literal notranslate"><span class="pre">log10(z)</span></code></a></p></td>277<td><p>Return the base-10 logarithm of <em>z</em></p></td>278</tr>279<tr class="row-odd"><td><p><a class="reference internal" href="#cmath.sqrt" title="cmath.sqrt"><code class="xref py py-func docutils literal notranslate"><span class="pre">sqrt(z)</span></code></a></p></td>280<td><p>Return the square root of <em>z</em></p></td>281</tr>282<tr class="row-even"><td colspan="2"><p><strong>Trigonometric functions</strong></p></td>283</tr>284<tr class="row-odd"><td><p><a class="reference internal" href="#cmath.acos" title="cmath.acos"><code class="xref py py-func docutils literal notranslate"><span class="pre">acos(z)</span></code></a></p></td>285<td><p>Return the arc cosine of <em>z</em></p></td>286</tr>287<tr class="row-even"><td><p><a class="reference internal" href="#cmath.asin" title="cmath.asin"><code class="xref py py-func docutils literal notranslate"><span class="pre">asin(z)</span></code></a></p></td>288<td><p>Return the arc sine of <em>z</em></p></td>289</tr>290<tr class="row-odd"><td><p><a class="reference internal" href="#cmath.atan" title="cmath.atan"><code class="xref py py-func docutils literal notranslate"><span class="pre">atan(z)</span></code></a></p></td>291<td><p>Return the arc tangent of <em>z</em></p></td>292</tr>293<tr class="row-even"><td><p><a class="reference internal" href="#cmath.cos" title="cmath.cos"><code class="xref py py-func docutils literal notranslate"><span class="pre">cos(z)</span></code></a></p></td>294<td><p>Return the cosine of <em>z</em></p></td>295</tr>296<tr class="row-odd"><td><p><a class="reference internal" href="#cmath.sin" title="cmath.sin"><code class="xref py py-func docutils literal notranslate"><span class="pre">sin(z)</span></code></a></p></td>297<td><p>Return the sine of <em>z</em></p></td>298</tr>299<tr class="row-even"><td><p><a class="reference internal" href="#cmath.tan" title="cmath.tan"><code class="xref py py-func docutils literal notranslate"><span class="pre">tan(z)</span></code></a></p></td>300<td><p>Return the tangent of <em>z</em></p></td>301</tr>302<tr class="row-odd"><td colspan="2"><p><strong>Hyperbolic functions</strong></p></td>303</tr>304<tr class="row-even"><td><p><a class="reference internal" href="#cmath.acosh" title="cmath.acosh"><code class="xref py py-func docutils literal notranslate"><span class="pre">acosh(z)</span></code></a></p></td>305<td><p>Return the inverse hyperbolic cosine of <em>z</em></p></td>306</tr>307<tr class="row-odd"><td><p><a class="reference internal" href="#cmath.asinh" title="cmath.asinh"><code class="xref py py-func docutils literal notranslate"><span class="pre">asinh(z)</span></code></a></p></td>308<td><p>Return the inverse hyperbolic sine of <em>z</em></p></td>309</tr>310<tr class="row-even"><td><p><a class="reference internal" href="#cmath.atanh" title="cmath.atanh"><code class="xref py py-func docutils literal notranslate"><span class="pre">atanh(z)</span></code></a></p></td>311<td><p>Return the inverse hyperbolic tangent of <em>z</em></p></td>312</tr>313<tr class="row-odd"><td><p><a class="reference internal" href="#cmath.cosh" title="cmath.cosh"><code class="xref py py-func docutils literal notranslate"><span class="pre">cosh(z)</span></code></a></p></td>314<td><p>Return the hyperbolic cosine of <em>z</em></p></td>315</tr>316<tr class="row-even"><td><p><a class="reference internal" href="#cmath.sinh" title="cmath.sinh"><code class="xref py py-func docutils literal notranslate"><span class="pre">sinh(z)</span></code></a></p></td>317<td><p>Return the hyperbolic sine of <em>z</em></p></td>318</tr>319<tr class="row-odd"><td><p><a class="reference internal" href="#cmath.tanh" title="cmath.tanh"><code class="xref py py-func docutils literal notranslate"><span class="pre">tanh(z)</span></code></a></p></td>320<td><p>Return the hyperbolic tangent of <em>z</em></p></td>321</tr>322<tr class="row-even"><td colspan="2"><p><strong>Classification functions</strong></p></td>323</tr>324<tr class="row-odd"><td><p><a class="reference internal" href="#cmath.isfinite" title="cmath.isfinite"><code class="xref py py-func docutils literal notranslate"><span class="pre">isfinite(z)</span></code></a></p></td>325<td><p>Check if all components of <em>z</em> are finite</p></td>326</tr>327<tr class="row-even"><td><p><a class="reference internal" href="#cmath.isinf" title="cmath.isinf"><code class="xref py py-func docutils literal notranslate"><span class="pre">isinf(z)</span></code></a></p></td>328<td><p>Check if any component of <em>z</em> is infinite</p></td>329</tr>330<tr class="row-odd"><td><p><a class="reference internal" href="#cmath.isnan" title="cmath.isnan"><code class="xref py py-func docutils literal notranslate"><span class="pre">isnan(z)</span></code></a></p></td>331<td><p>Check if any component of <em>z</em> is a NaN</p></td>332</tr>333<tr class="row-even"><td><p><a class="reference internal" href="#cmath.isclose" title="cmath.isclose"><code class="xref py py-func docutils literal notranslate"><span class="pre">isclose(a,</span> <span class="pre">b,</span> <span class="pre">*,</span> <span class="pre">rel_tol,</span> <span class="pre">abs_tol)</span></code></a></p></td>334<td><p>Check if the values <em>a</em> and <em>b</em> are close to each other</p></td>335</tr>336<tr class="row-odd"><td colspan="2"><p><strong>Constants</strong></p></td>337</tr>338<tr class="row-even"><td><p><a class="reference internal" href="#cmath.pi" title="cmath.pi"><code class="xref py py-data docutils literal notranslate"><span class="pre">pi</span></code></a></p></td>339<td><p><em>π</em> = 3.141592…</p></td>340</tr>341<tr class="row-odd"><td><p><a class="reference internal" href="#cmath.e" title="cmath.e"><code class="xref py py-data docutils literal notranslate"><span class="pre">e</span></code></a></p></td>342<td><p><em>e</em> = 2.718281…</p></td>343</tr>344<tr class="row-even"><td><p><a class="reference internal" href="#cmath.tau" title="cmath.tau"><code class="xref py py-data docutils literal notranslate"><span class="pre">tau</span></code></a></p></td>345<td><p><em>τ</em> = 2<em>π</em> = 6.283185…</p></td>346</tr>347<tr class="row-odd"><td><p><a class="reference internal" href="#cmath.inf" title="cmath.inf"><code class="xref py py-data docutils literal notranslate"><span class="pre">inf</span></code></a></p></td>348<td><p>Positive infinity</p></td>349</tr>350<tr class="row-even"><td><p><a class="reference internal" href="#cmath.infj" title="cmath.infj"><code class="xref py py-data docutils literal notranslate"><span class="pre">infj</span></code></a></p></td>351<td><p>Pure imaginary infinity</p></td>352</tr>353<tr class="row-odd"><td><p><a class="reference internal" href="#cmath.nan" title="cmath.nan"><code class="xref py py-data docutils literal notranslate"><span class="pre">nan</span></code></a></p></td>354<td><p>“Not a number” (NaN)</p></td>355</tr>356<tr class="row-even"><td><p><a class="reference internal" href="#cmath.nanj" title="cmath.nanj"><code class="xref py py-data docutils literal notranslate"><span class="pre">nanj</span></code></a></p></td>357<td><p>Pure imaginary NaN</p></td>358</tr>359</tbody>360</table>361<section id="conversions-to-and-from-polar-coordinates">362<h2>Conversions to and from polar coordinates<a class="headerlink" href="#conversions-to-and-from-polar-coordinates" title="Link to this heading">¶</a></h2>363<p>A Python complex number <code class="docutils literal notranslate"><span class="pre">z</span></code> is stored internally using <em>rectangular</em>364or <em>Cartesian</em> coordinates. It is completely determined by its <em>real365part</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>.</p>366<p><em>Polar coordinates</em> give an alternative way to represent a complex367number. In polar coordinates, a complex number <em>z</em> is defined by the368modulus <em>r</em> and the phase angle <em>phi</em>. The modulus <em>r</em> is the distance369from <em>z</em> to the origin, while the phase <em>phi</em> is the counterclockwise370angle, measured in radians, from the positive x-axis to the line371segment that joins the origin to <em>z</em>.</p>372<p>The following functions can be used to convert from the native373rectangular coordinates to polar coordinates and back.</p>374<dl class="py function">375<dt class="sig sig-object py" id="cmath.phase">376<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">z</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.phase" title="Link to this definition">¶</a></dt>377<dd><p>Return the phase of <em>z</em> (also known as the <em>argument</em> of <em>z</em>), as a float.378<code class="docutils literal notranslate"><span class="pre">phase(z)</span></code> is equivalent to <code class="docutils literal notranslate"><span class="pre">math.atan2(z.imag,</span> <span class="pre">z.real)</span></code>. The result379lies in the range [-<em>π</em>, <em>π</em>], and the branch cut for this operation lies380along the negative real axis. The sign of the result is the same as the381sign of <code class="docutils literal notranslate"><span class="pre">z.imag</span></code>, even when <code class="docutils literal notranslate"><span class="pre">z.imag</span></code> is zero:</p>382<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">phase</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="o">+</span><span class="mi">0</span><span class="n">j</span><span class="p">)</span>383<span class="go">3.141592653589793</span>384<span class="gp">>>> </span><span class="n">phase</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="o">-</span><span class="mi">0</span><span class="n">j</span><span class="p">)</span>385<span class="go">-3.141592653589793</span>386</pre></div>387</div>388</dd></dl>389 390<div class="admonition note">391<p class="admonition-title">Note</p>392<p>The modulus (absolute value) of a complex number <em>z</em> can be393computed 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 no394separate <code class="xref py py-mod docutils literal notranslate"><span class="pre">cmath</span></code> module function for this operation.</p>395</div>396<dl class="py function">397<dt class="sig sig-object py" id="cmath.polar">398<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">z</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.polar" title="Link to this definition">¶</a></dt>399<dd><p>Return the representation of <em>z</em> in polar coordinates. Returns a400pair <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>z</em> and <em>phi</em> is the401phase of <em>z</em>. <code class="docutils literal notranslate"><span class="pre">polar(z)</span></code> is equivalent to <code class="docutils literal notranslate"><span class="pre">(abs(z),</span>402<span class="pre">phase(z))</span></code>.</p>403</dd></dl>404 405<dl class="py function">406<dt class="sig sig-object py" id="cmath.rect">407<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>408<dd><p>Return the complex number <em>z</em> with polar coordinates <em>r</em> and <em>phi</em>.409Equivalent to <code class="docutils literal notranslate"><span class="pre">complex(r</span> <span class="pre">*</span> <span class="pre">math.cos(phi),</span> <span class="pre">r</span> <span class="pre">*</span> <span class="pre">math.sin(phi))</span></code>.</p>410</dd></dl>411 412</section>413<section id="power-and-logarithmic-functions">414<h2>Power and logarithmic functions<a class="headerlink" href="#power-and-logarithmic-functions" title="Link to this heading">¶</a></h2>415<dl class="py function">416<dt class="sig sig-object py" id="cmath.exp">417<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">z</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.exp" title="Link to this definition">¶</a></dt>418<dd><p>Return <em>e</em> raised to the power <em>z</em>, where <em>e</em> is the base of natural419logarithms.</p>420</dd></dl>421 422<dl class="py function">423<dt class="sig sig-object py" id="cmath.log">424<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">z</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>425<dd><p>Return the logarithm of <em>z</em> to the given <em>base</em>. If the <em>base</em> is not426specified, returns the natural logarithm of <em>z</em>. There is one branch cut,427from 0 along the negative real axis to -∞.</p>428</dd></dl>429 430<dl class="py function">431<dt class="sig sig-object py" id="cmath.log10">432<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">z</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.log10" title="Link to this definition">¶</a></dt>433<dd><p>Return the base-10 logarithm of <em>z</em>. This has the same branch cut as434<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>435</dd></dl>436 437<dl class="py function">438<dt class="sig sig-object py" id="cmath.sqrt">439<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">z</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.sqrt" title="Link to this definition">¶</a></dt>440<dd><p>Return the square root of <em>z</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>441</dd></dl>442 443</section>444<section id="trigonometric-functions">445<h2>Trigonometric functions<a class="headerlink" href="#trigonometric-functions" title="Link to this heading">¶</a></h2>446<dl class="py function">447<dt class="sig sig-object py" id="cmath.acos">448<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">z</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.acos" title="Link to this definition">¶</a></dt>449<dd><p>Return the arc cosine of <em>z</em>. There are two branch cuts: One extends right450from 1 along the real axis to ∞. The other extends left from -1 along the451real axis to -∞.</p>452</dd></dl>453 454<dl class="py function">455<dt class="sig sig-object py" id="cmath.asin">456<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">z</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.asin" title="Link to this definition">¶</a></dt>457<dd><p>Return the arc sine of <em>z</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>458</dd></dl>459 460<dl class="py function">461<dt class="sig sig-object py" id="cmath.atan">462<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">z</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.atan" title="Link to this definition">¶</a></dt>463<dd><p>Return the arc tangent of <em>z</em>. There are two branch cuts: One extends from464<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>465along the imaginary axis to <code class="docutils literal notranslate"><span class="pre">-∞j</span></code>.</p>466</dd></dl>467 468<dl class="py function">469<dt class="sig sig-object py" id="cmath.cos">470<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">z</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.cos" title="Link to this definition">¶</a></dt>471<dd><p>Return the cosine of <em>z</em>.</p>472</dd></dl>473 474<dl class="py function">475<dt class="sig sig-object py" id="cmath.sin">476<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">z</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.sin" title="Link to this definition">¶</a></dt>477<dd><p>Return the sine of <em>z</em>.</p>478</dd></dl>479 480<dl class="py function">481<dt class="sig sig-object py" id="cmath.tan">482<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">z</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.tan" title="Link to this definition">¶</a></dt>483<dd><p>Return the tangent of <em>z</em>.</p>484</dd></dl>485 486</section>487<section id="hyperbolic-functions">488<h2>Hyperbolic functions<a class="headerlink" href="#hyperbolic-functions" title="Link to this heading">¶</a></h2>489<dl class="py function">490<dt class="sig sig-object py" id="cmath.acosh">491<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">z</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.acosh" title="Link to this definition">¶</a></dt>492<dd><p>Return the inverse hyperbolic cosine of <em>z</em>. There is one branch cut,493extending left from 1 along the real axis to -∞.</p>494</dd></dl>495 496<dl class="py function">497<dt class="sig sig-object py" id="cmath.asinh">498<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">z</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.asinh" title="Link to this definition">¶</a></dt>499<dd><p>Return the inverse hyperbolic sine of <em>z</em>. There are two branch cuts:500One 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 other501extends 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>502</dd></dl>503 504<dl class="py function">505<dt class="sig sig-object py" id="cmath.atanh">506<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">z</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.atanh" title="Link to this definition">¶</a></dt>507<dd><p>Return the inverse hyperbolic tangent of <em>z</em>. There are two branch cuts: One508extends 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 from509<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>510</dd></dl>511 512<dl class="py function">513<dt class="sig sig-object py" id="cmath.cosh">514<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">z</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.cosh" title="Link to this definition">¶</a></dt>515<dd><p>Return the hyperbolic cosine of <em>z</em>.</p>516</dd></dl>517 518<dl class="py function">519<dt class="sig sig-object py" id="cmath.sinh">520<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">z</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.sinh" title="Link to this definition">¶</a></dt>521<dd><p>Return the hyperbolic sine of <em>z</em>.</p>522</dd></dl>523 524<dl class="py function">525<dt class="sig sig-object py" id="cmath.tanh">526<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">z</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.tanh" title="Link to this definition">¶</a></dt>527<dd><p>Return the hyperbolic tangent of <em>z</em>.</p>528</dd></dl>529 530</section>531<section id="classification-functions">532<h2>Classification functions<a class="headerlink" href="#classification-functions" title="Link to this heading">¶</a></h2>533<dl class="py function">534<dt class="sig sig-object py" id="cmath.isfinite">535<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">z</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.isfinite" title="Link to this definition">¶</a></dt>536<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if both the real and imaginary parts of <em>z</em> are finite, and537<code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>538<div class="versionadded">539<p><span class="versionmodified added">Added in version 3.2.</span></p>540</div>541</dd></dl>542 543<dl class="py function">544<dt class="sig sig-object py" id="cmath.isinf">545<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">z</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.isinf" title="Link to this definition">¶</a></dt>546<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if either the real or the imaginary part of <em>z</em> is an547infinity, and <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>548</dd></dl>549 550<dl class="py function">551<dt class="sig sig-object py" id="cmath.isnan">552<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">z</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#cmath.isnan" title="Link to this definition">¶</a></dt>553<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if either the real or the imaginary part of <em>z</em> is a NaN,554and <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>555</dd></dl>556 557<dl class="py function">558<dt class="sig sig-object py" id="cmath.isclose">559<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="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></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>560<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 and561<code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>562<p>Whether or not two values are considered close is determined according to563given absolute and relative tolerances. If no errors occur, the result will564be: <code class="docutils literal notranslate"><span class="pre">abs(a-b)</span> <span class="pre"><=</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>565<p><em>rel_tol</em> is the relative tolerance – it is the maximum allowed difference566between <em>a</em> and <em>b</em>, relative to the larger absolute value of <em>a</em> or <em>b</em>.567For example, to set a tolerance of 5%, pass <code class="docutils literal notranslate"><span class="pre">rel_tol=0.05</span></code>. The default568tolerance is <code class="docutils literal notranslate"><span class="pre">1e-09</span></code>, which assures that the two values are the same569within about 9 decimal digits. <em>rel_tol</em> must be nonnegative and less570than <code class="docutils literal notranslate"><span class="pre">1.0</span></code>.</p>571<p><em>abs_tol</em> is the absolute tolerance; it defaults to <code class="docutils literal notranslate"><span class="pre">0.0</span></code> and it must be572nonnegative. When comparing <code class="docutils literal notranslate"><span class="pre">x</span></code> to <code class="docutils literal notranslate"><span class="pre">0.0</span></code>, <code class="docutils literal notranslate"><span class="pre">isclose(x,</span> <span class="pre">0)</span></code> is computed573as <code class="docutils literal notranslate"><span class="pre">abs(x)</span> <span class="pre"><=</span> <span class="pre">rel_tol</span>  <span class="pre">*</span> <span class="pre">abs(x)</span></code>, which is <code class="docutils literal notranslate"><span class="pre">False</span></code> for any <code class="docutils literal notranslate"><span class="pre">x</span></code> and574rel_tol less than <code class="docutils literal notranslate"><span class="pre">1.0</span></code>. So add an appropriate positive abs_tol argument575to the call.</p>576<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 be577handled according to IEEE rules. Specifically, <code class="docutils literal notranslate"><span class="pre">NaN</span></code> is not considered578close 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 only579considered close to themselves.</p>580<div class="versionadded">581<p><span class="versionmodified added">Added in version 3.5.</span></p>582</div>583<div class="admonition seealso">584<p class="admonition-title">See also</p>585<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>586</div>587</dd></dl>588 589</section>590<section id="constants">591<h2>Constants<a class="headerlink" href="#constants" title="Link to this heading">¶</a></h2>592<dl class="py data">593<dt class="sig sig-object py" id="cmath.pi">594<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>595<dd><p>The mathematical constant <em>π</em>, as a float.</p>596</dd></dl>597 598<dl class="py data">599<dt class="sig sig-object py" id="cmath.e">600<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>601<dd><p>The mathematical constant <em>e</em>, as a float.</p>602</dd></dl>603 604<dl class="py data">605<dt class="sig sig-object py" id="cmath.tau">606<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>607<dd><p>The mathematical constant <em>τ</em>, as a float.</p>608<div class="versionadded">609<p><span class="versionmodified added">Added in version 3.6.</span></p>610</div>611</dd></dl>612 613<dl class="py data">614<dt class="sig sig-object py" id="cmath.inf">615<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>616<dd><p>Floating-point positive infinity. Equivalent to <code class="docutils literal notranslate"><span class="pre">float('inf')</span></code>.</p>617<div class="versionadded">618<p><span class="versionmodified added">Added in version 3.6.</span></p>619</div>620</dd></dl>621 622<dl class="py data">623<dt class="sig sig-object py" id="cmath.infj">624<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>625<dd><p>Complex number with zero real part and positive infinity imaginary626part. Equivalent to <code class="docutils literal notranslate"><span class="pre">complex(0.0,</span> <span class="pre">float('inf'))</span></code>.</p>627<div class="versionadded">628<p><span class="versionmodified added">Added in version 3.6.</span></p>629</div>630</dd></dl>631 632<dl class="py data">633<dt class="sig sig-object py" id="cmath.nan">634<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>635<dd><p>A floating-point “not a number” (NaN) value. Equivalent to636<code class="docutils literal notranslate"><span class="pre">float('nan')</span></code>. See also <a class="reference internal" href="math.html#math.nan" title="math.nan"><code class="xref py py-data docutils literal notranslate"><span class="pre">math.nan</span></code></a>.</p>637<div class="versionadded">638<p><span class="versionmodified added">Added in version 3.6.</span></p>639</div>640</dd></dl>641 642<dl class="py data">643<dt class="sig sig-object py" id="cmath.nanj">644<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>645<dd><p>Complex number with zero real part and NaN imaginary part. Equivalent to646<code class="docutils literal notranslate"><span class="pre">complex(0.0,</span> <span class="pre">float('nan'))</span></code>.</p>647<div class="versionadded">648<p><span class="versionmodified added">Added in version 3.6.</span></p>649</div>650</dd></dl>651 652<p id="index-1">Note that the selection of functions is similar, but not identical, to that in653module <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’t654interested in complex numbers, and perhaps don’t even know what they are. They655would rather have <code class="docutils literal notranslate"><span class="pre">math.sqrt(-1)</span></code> raise an exception than return a complex656number. Also note that the functions defined in <code class="xref py py-mod docutils literal notranslate"><span class="pre">cmath</span></code> always return a657complex number, even if the answer can be expressed as a real number (in which658case the complex number has an imaginary part of zero).</p>659<p>A note on branch cuts: They are curves along which the given function fails to660be continuous. They are a necessary feature of many complex functions. It is661assumed that if you need to compute with complex functions, you will understand662about branch cuts. Consult almost any (not too elementary) book on complex663variables for enlightenment. For information of the proper choice of branch664cuts for numerical purposes, a good reference should be the following:</p>665<div class="admonition seealso">666<p class="admonition-title">See also</p>667<p>Kahan, W: Branch cuts for complex elementary functions; or, Much ado about668nothing’s sign bit. In Iserles, A., and Powell, M. (eds.), The state of the art669in numerical analysis. Clarendon Press (1987) pp165–211.</p>670</div>671</section>672</section>673 674 675 <div class="clearer"></div>676 </div>677 </div>678 </div>679 <div class="sphinxsidebar" role="navigation" aria-label="Main">680 <div class="sphinxsidebarwrapper">681 <div>682 <h3><a href="../contents.html">Table of Contents</a></h3>683 <ul>684<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>685<li><a class="reference internal" href="#conversions-to-and-from-polar-coordinates">Conversions to and from polar coordinates</a></li>686<li><a class="reference internal" href="#power-and-logarithmic-functions">Power and logarithmic functions</a></li>687<li><a class="reference internal" href="#trigonometric-functions">Trigonometric functions</a></li>688<li><a class="reference internal" href="#hyperbolic-functions">Hyperbolic functions</a></li>689<li><a class="reference internal" href="#classification-functions">Classification functions</a></li>690<li><a class="reference internal" href="#constants">Constants</a></li>691</ul>692</li>693</ul>694 695 </div>696 <div>697 <h4>Previous topic</h4>698 <p class="topless"><a href="math.integer.html"699 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">math.integer</span></code> — integer-specific mathematics functions</a></p>700 </div>701 <div>702 <h4>Next topic</h4>703 <p class="topless"><a href="decimal.html"704 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>705 </div>706 <script>707 document.addEventListener('DOMContentLoaded', () => {708 const title = document.querySelector('meta[property="og:title"]').content;709 const elements = document.querySelectorAll('.improvepage');710 const pageurl = window.location.href.split('?')[0];711 elements.forEach(element => {712 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));713 url.searchParams.set('pagetitle', title);714 url.searchParams.set('pageurl', pageurl);715 url.searchParams.set('pagesource', "library/cmath.rst");716 element.href = url.toString();717 });718 });719 </script>720 <div role="note" aria-label="source link">721 <h3>This page</h3>722 <ul class="this-page-menu">723 <li><a href="../bugs.html">Report a bug</a></li>724 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>725 <li>726 <a href="https://github.com/python/cpython/blob/main/Doc/library/cmath.rst?plain=1"727 rel="nofollow">Show source728 </a>729 </li>730 731 </ul>732 </div>733 </div>734<div id="sidebarbutton" title="Collapse sidebar">735<span>«</span>736</div>737 738 </div>739 <div class="clearer"></div>740 </div> 741 <div class="related" role="navigation" aria-label="Related">742 <h3>Navigation</h3>743 <ul>744 <li class="right" style="margin-right: 10px">745 <a href="../genindex.html" title="General Index"746 >index</a></li>747 <li class="right" >748 <a href="../py-modindex.html" title="Python Module Index"749 >modules</a> |</li>750 <li class="right" >751 <a href="decimal.html" title="decimal — Decimal fixed-point and floating-point arithmetic"752 >next</a> |</li>753 <li class="right" >754 <a href="math.integer.html" title="math.integer — integer-specific mathematics functions"755 >previous</a> |</li>756 757 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>758 <li><a href="https://www.python.org/">Python</a> »</li>759 <li class="switchers">760 <div class="language_switcher_placeholder"></div>761 <div class="version_switcher_placeholder"></div>762 </li>763 <li>764 765 </li>766 <li id="cpython-language-and-version">767 <a href="../index.html">3.15.0a6 Documentation</a> »768 </li>769 770 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>771 <li class="nav-item nav-item-2"><a href="numeric.html" >Numeric and Mathematical Modules</a> »</li>772 <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>773 <li class="right">774 775 776 <div class="inline-search" role="search">777 <form class="inline-search" action="../search.html" method="get">778 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">779 <input type="submit" value="Go">780 </form>781 </div>782 |783 </li>784 <li class="right">785<label class="theme-selector-label">786 Theme787 <select class="theme-selector" oninput="activateTheme(this.value)">788 <option value="auto" selected>Auto</option>789 <option value="light">Light</option>790 <option value="dark">Dark</option>791 </select>792</label> |</li>793 794 </ul>795 </div> 796 <div class="footer">797 © <a href="../copyright.html">Copyright</a> 2001 Python Software Foundation.798 <br>799 This page is licensed under the Python Software Foundation License Version 2.800 <br>801 Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.802 <br>803 804 See <a href="/license.html">History and License</a> for more information.<br>805 806 807 <br>808 809 The Python Software Foundation is a non-profit corporation.810<a href="https://www.python.org/psf/donations/">Please donate.</a>811<br>812 <br>813 Last updated on Mar 10, 2026 (08:58 UTC).814 815 <a href="/bugs.html">Found a bug</a>?816 817 <br>818 819 Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.820 </div>821 822 </body>823</html>