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="math — Mathematical functions" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/math.html" />10<meta property="og:site_name" content="Python documentation" />11<meta property="og:description" content="This module provides access to common mathematical functions and constants, including those defined by the C standard. These functions cannot be used with complex numbers; use the functions of the ..." />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_math_aa1f6de7.png" />15<meta property="og:image:alt" content="This module provides access to common mathematical functions and constants, including those defined by the C standard. These functions cannot be used with complex numbers; use the functions of the ..." />16<meta name="description" content="This module provides access to common mathematical functions and constants, including those defined by the C standard. These functions cannot be used with complex numbers; use the functions of the ..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20 <title>math — Mathematical functions — 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="math.integer — integer-specific mathematics functions" href="math.integer.html" />43 <link rel="prev" title="numbers — Numeric abstract base classes" href="numbers.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/math.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">math</span></code> — Mathematical functions</a><ul>108<li><a class="reference internal" href="#floating-point-arithmetic">Floating point arithmetic</a></li>109<li><a class="reference internal" href="#floating-point-manipulation-functions">Floating point manipulation functions</a></li>110<li><a class="reference internal" href="#power-exponential-and-logarithmic-functions">Power, exponential and logarithmic functions</a></li>111<li><a class="reference internal" href="#summation-and-product-functions">Summation and product functions</a></li>112<li><a class="reference internal" href="#angular-conversion">Angular conversion</a></li>113<li><a class="reference internal" href="#trigonometric-functions">Trigonometric functions</a></li>114<li><a class="reference internal" href="#hyperbolic-functions">Hyperbolic functions</a></li>115<li><a class="reference internal" href="#special-functions">Special functions</a></li>116<li><a class="reference internal" href="#number-theoretic-functions">Number-theoretic functions</a></li>117<li><a class="reference internal" href="#constants">Constants</a></li>118</ul>119</li>120</ul>121 122 </div>123 <div>124 <h4>Previous topic</h4>125 <p class="topless"><a href="numbers.html"126 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">numbers</span></code> — Numeric abstract base classes</a></p>127 </div>128 <div>129 <h4>Next topic</h4>130 <p class="topless"><a href="math.integer.html"131 title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">math.integer</span></code> — integer-specific mathematics functions</a></p>132 </div>133 <script>134 document.addEventListener('DOMContentLoaded', () => {135 const title = document.querySelector('meta[property="og:title"]').content;136 const elements = document.querySelectorAll('.improvepage');137 const pageurl = window.location.href.split('?')[0];138 elements.forEach(element => {139 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));140 url.searchParams.set('pagetitle', title);141 url.searchParams.set('pageurl', pageurl);142 url.searchParams.set('pagesource', "library/math.rst");143 element.href = url.toString();144 });145 });146 </script>147 <div role="note" aria-label="source link">148 <h3>This page</h3>149 <ul class="this-page-menu">150 <li><a href="../bugs.html">Report a bug</a></li>151 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>152 <li>153 <a href="https://github.com/python/cpython/blob/main/Doc/library/math.rst?plain=1"154 rel="nofollow">Show source155 </a>156 </li>157 158 </ul>159 </div>160 </nav>161 </div>162</div>163 164 165 <div class="related" role="navigation" aria-label="Related">166 <h3>Navigation</h3>167 <ul>168 <li class="right" style="margin-right: 10px">169 <a href="../genindex.html" title="General Index"170 accesskey="I">index</a></li>171 <li class="right" >172 <a href="../py-modindex.html" title="Python Module Index"173 >modules</a> |</li>174 <li class="right" >175 <a href="math.integer.html" title="math.integer — integer-specific mathematics functions"176 accesskey="N">next</a> |</li>177 <li class="right" >178 <a href="numbers.html" title="numbers — Numeric abstract base classes"179 accesskey="P">previous</a> |</li>180 181 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>182 <li><a href="https://www.python.org/">Python</a> »</li>183 <li class="switchers">184 <div class="language_switcher_placeholder"></div>185 <div class="version_switcher_placeholder"></div>186 </li>187 <li>188 189 </li>190 <li id="cpython-language-and-version">191 <a href="../index.html">3.15.0a6 Documentation</a> »192 </li>193 194 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>195 <li class="nav-item nav-item-2"><a href="numeric.html" accesskey="U">Numeric and Mathematical Modules</a> »</li>196 <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">math</span></code> — Mathematical functions</a></li>197 <li class="right">198 199 200 <div class="inline-search" role="search">201 <form class="inline-search" action="../search.html" method="get">202 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">203 <input type="submit" value="Go">204 </form>205 </div>206 |207 </li>208 <li class="right">209<label class="theme-selector-label">210 Theme211 <select class="theme-selector" oninput="activateTheme(this.value)">212 <option value="auto" selected>Auto</option>213 <option value="light">Light</option>214 <option value="dark">Dark</option>215 </select>216</label> |</li>217 218 </ul>219 </div> 220 221 <div class="document">222 <div class="documentwrapper">223 <div class="bodywrapper">224 <div class="body" role="main">225 226 <section id="module-math">227<span id="math-mathematical-functions"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">math</span></code> — Mathematical functions<a class="headerlink" href="#module-math" title="Link to this heading">¶</a></h1>228<hr class="docutils" />229<p>This module provides access to common mathematical functions and constants,230including those defined by the C standard.</p>231<p>These functions cannot be used with complex numbers; use the functions of the232same name from the <a class="reference internal" href="cmath.html#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 if you require support for complex233numbers. The distinction between functions which support complex numbers and234those which don’t is made since most users do not want to learn quite as much235mathematics as required to understand complex numbers. Receiving an exception236instead of a complex result allows earlier detection of the unexpected complex237number used as a parameter, so that the programmer can determine how and why it238was generated in the first place.</p>239<p>The following functions are provided by this module. Except when explicitly240noted otherwise, all return values are floats.</p>241<table class="docutils align-default">242<tbody>243<tr class="row-odd"><td colspan="2"><p><strong>Floating point arithmetic</strong></p></td>244</tr>245<tr class="row-even"><td><p><a class="reference internal" href="#math.ceil" title="math.ceil"><code class="xref py py-func docutils literal notranslate"><span class="pre">ceil(x)</span></code></a></p></td>246<td><p>Ceiling of <em>x</em>, the smallest integer greater than or equal to <em>x</em></p></td>247</tr>248<tr class="row-odd"><td><p><a class="reference internal" href="#math.fabs" title="math.fabs"><code class="xref py py-func docutils literal notranslate"><span class="pre">fabs(x)</span></code></a></p></td>249<td><p>Absolute value of <em>x</em></p></td>250</tr>251<tr class="row-even"><td><p><a class="reference internal" href="#math.floor" title="math.floor"><code class="xref py py-func docutils literal notranslate"><span class="pre">floor(x)</span></code></a></p></td>252<td><p>Floor of <em>x</em>, the largest integer less than or equal to <em>x</em></p></td>253</tr>254<tr class="row-odd"><td><p><a class="reference internal" href="#math.fma" title="math.fma"><code class="xref py py-func docutils literal notranslate"><span class="pre">fma(x,</span> <span class="pre">y,</span> <span class="pre">z)</span></code></a></p></td>255<td><p>Fused multiply-add operation: <code class="docutils literal notranslate"><span class="pre">(x</span> <span class="pre">*</span> <span class="pre">y)</span> <span class="pre">+</span> <span class="pre">z</span></code></p></td>256</tr>257<tr class="row-even"><td><p><a class="reference internal" href="#math.fmax" title="math.fmax"><code class="xref py py-func docutils literal notranslate"><span class="pre">fmax(x,</span> <span class="pre">y)</span></code></a></p></td>258<td><p>Maximum of two floating-point values</p></td>259</tr>260<tr class="row-odd"><td><p><a class="reference internal" href="#math.fmin" title="math.fmin"><code class="xref py py-func docutils literal notranslate"><span class="pre">fmin(x,</span> <span class="pre">y)</span></code></a></p></td>261<td><p>Minimum of two floating-point values</p></td>262</tr>263<tr class="row-even"><td><p><a class="reference internal" href="#math.fmod" title="math.fmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">fmod(x,</span> <span class="pre">y)</span></code></a></p></td>264<td><p>Remainder of division <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">/</span> <span class="pre">y</span></code></p></td>265</tr>266<tr class="row-odd"><td><p><a class="reference internal" href="#math.modf" title="math.modf"><code class="xref py py-func docutils literal notranslate"><span class="pre">modf(x)</span></code></a></p></td>267<td><p>Fractional and integer parts of <em>x</em></p></td>268</tr>269<tr class="row-even"><td><p><a class="reference internal" href="#math.remainder" title="math.remainder"><code class="xref py py-func docutils literal notranslate"><span class="pre">remainder(x,</span> <span class="pre">y)</span></code></a></p></td>270<td><p>Remainder of <em>x</em> with respect to <em>y</em></p></td>271</tr>272<tr class="row-odd"><td><p><a class="reference internal" href="#math.trunc" title="math.trunc"><code class="xref py py-func docutils literal notranslate"><span class="pre">trunc(x)</span></code></a></p></td>273<td><p>Integer part of <em>x</em></p></td>274</tr>275<tr class="row-even"><td colspan="2"><p><strong>Floating point manipulation functions</strong></p></td>276</tr>277<tr class="row-odd"><td><p><a class="reference internal" href="#math.copysign" title="math.copysign"><code class="xref py py-func docutils literal notranslate"><span class="pre">copysign(x,</span> <span class="pre">y)</span></code></a></p></td>278<td><p>Magnitude (absolute value) of <em>x</em> with the sign of <em>y</em></p></td>279</tr>280<tr class="row-even"><td><p><a class="reference internal" href="#math.frexp" title="math.frexp"><code class="xref py py-func docutils literal notranslate"><span class="pre">frexp(x)</span></code></a></p></td>281<td><p>Mantissa and exponent of <em>x</em></p></td>282</tr>283<tr class="row-odd"><td><p><a class="reference internal" href="#math.isclose" title="math.isclose"><code class="xref py py-func docutils literal notranslate"><span class="pre">isclose(a,</span> <span class="pre">b,</span> <span class="pre">rel_tol,</span> <span class="pre">abs_tol)</span></code></a></p></td>284<td><p>Check if the values <em>a</em> and <em>b</em> are close to each other</p></td>285</tr>286<tr class="row-even"><td><p><a class="reference internal" href="#math.isfinite" title="math.isfinite"><code class="xref py py-func docutils literal notranslate"><span class="pre">isfinite(x)</span></code></a></p></td>287<td><p>Check if <em>x</em> is neither an infinity nor a NaN</p></td>288</tr>289<tr class="row-odd"><td><p><a class="reference internal" href="#math.isnormal" title="math.isnormal"><code class="xref py py-func docutils literal notranslate"><span class="pre">isnormal(x)</span></code></a></p></td>290<td><p>Check if <em>x</em> is a normal number</p></td>291</tr>292<tr class="row-even"><td><p><a class="reference internal" href="#math.issubnormal" title="math.issubnormal"><code class="xref py py-func docutils literal notranslate"><span class="pre">issubnormal(x)</span></code></a></p></td>293<td><p>Check if <em>x</em> is a subnormal number</p></td>294</tr>295<tr class="row-odd"><td><p><a class="reference internal" href="#math.isinf" title="math.isinf"><code class="xref py py-func docutils literal notranslate"><span class="pre">isinf(x)</span></code></a></p></td>296<td><p>Check if <em>x</em> is a positive or negative infinity</p></td>297</tr>298<tr class="row-even"><td><p><a class="reference internal" href="#math.isnan" title="math.isnan"><code class="xref py py-func docutils literal notranslate"><span class="pre">isnan(x)</span></code></a></p></td>299<td><p>Check if <em>x</em> is a NaN (not a number)</p></td>300</tr>301<tr class="row-odd"><td><p><a class="reference internal" href="#math.ldexp" title="math.ldexp"><code class="xref py py-func docutils literal notranslate"><span class="pre">ldexp(x,</span> <span class="pre">i)</span></code></a></p></td>302<td><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">*</span> <span class="pre">(2**i)</span></code>, inverse of function <a class="reference internal" href="#math.frexp" title="math.frexp"><code class="xref py py-func docutils literal notranslate"><span class="pre">frexp()</span></code></a></p></td>303</tr>304<tr class="row-even"><td><p><a class="reference internal" href="#math.nextafter" title="math.nextafter"><code class="xref py py-func docutils literal notranslate"><span class="pre">nextafter(x,</span> <span class="pre">y,</span> <span class="pre">steps)</span></code></a></p></td>305<td><p>Floating-point value <em>steps</em> steps after <em>x</em> towards <em>y</em></p></td>306</tr>307<tr class="row-odd"><td><p><a class="reference internal" href="#math.signbit" title="math.signbit"><code class="xref py py-func docutils literal notranslate"><span class="pre">signbit(x)</span></code></a></p></td>308<td><p>Check if <em>x</em> is a negative number</p></td>309</tr>310<tr class="row-even"><td><p><a class="reference internal" href="#math.ulp" title="math.ulp"><code class="xref py py-func docutils literal notranslate"><span class="pre">ulp(x)</span></code></a></p></td>311<td><p>Value of the least significant bit of <em>x</em></p></td>312</tr>313<tr class="row-odd"><td colspan="2"><p><strong>Power, exponential and logarithmic functions</strong></p></td>314</tr>315<tr class="row-even"><td><p><a class="reference internal" href="#math.cbrt" title="math.cbrt"><code class="xref py py-func docutils literal notranslate"><span class="pre">cbrt(x)</span></code></a></p></td>316<td><p>Cube root of <em>x</em></p></td>317</tr>318<tr class="row-odd"><td><p><a class="reference internal" href="#math.exp" title="math.exp"><code class="xref py py-func docutils literal notranslate"><span class="pre">exp(x)</span></code></a></p></td>319<td><p><em>e</em> raised to the power <em>x</em></p></td>320</tr>321<tr class="row-even"><td><p><a class="reference internal" href="#math.exp2" title="math.exp2"><code class="xref py py-func docutils literal notranslate"><span class="pre">exp2(x)</span></code></a></p></td>322<td><p><em>2</em> raised to the power <em>x</em></p></td>323</tr>324<tr class="row-odd"><td><p><a class="reference internal" href="#math.expm1" title="math.expm1"><code class="xref py py-func docutils literal notranslate"><span class="pre">expm1(x)</span></code></a></p></td>325<td><p><em>e</em> raised to the power <em>x</em>, minus 1</p></td>326</tr>327<tr class="row-even"><td><p><a class="reference internal" href="#math.log" title="math.log"><code class="xref py py-func docutils literal notranslate"><span class="pre">log(x,</span> <span class="pre">base)</span></code></a></p></td>328<td><p>Logarithm of <em>x</em> to the given base (<em>e</em> by default)</p></td>329</tr>330<tr class="row-odd"><td><p><a class="reference internal" href="#math.log1p" title="math.log1p"><code class="xref py py-func docutils literal notranslate"><span class="pre">log1p(x)</span></code></a></p></td>331<td><p>Natural logarithm of <em>1+x</em> (base <em>e</em>)</p></td>332</tr>333<tr class="row-even"><td><p><a class="reference internal" href="#math.log2" title="math.log2"><code class="xref py py-func docutils literal notranslate"><span class="pre">log2(x)</span></code></a></p></td>334<td><p>Base-2 logarithm of <em>x</em></p></td>335</tr>336<tr class="row-odd"><td><p><a class="reference internal" href="#math.log10" title="math.log10"><code class="xref py py-func docutils literal notranslate"><span class="pre">log10(x)</span></code></a></p></td>337<td><p>Base-10 logarithm of <em>x</em></p></td>338</tr>339<tr class="row-even"><td><p><a class="reference internal" href="#math.pow" title="math.pow"><code class="xref py py-func docutils literal notranslate"><span class="pre">pow(x,</span> <span class="pre">y)</span></code></a></p></td>340<td><p><em>x</em> raised to the power <em>y</em></p></td>341</tr>342<tr class="row-odd"><td><p><a class="reference internal" href="#math.sqrt" title="math.sqrt"><code class="xref py py-func docutils literal notranslate"><span class="pre">sqrt(x)</span></code></a></p></td>343<td><p>Square root of <em>x</em></p></td>344</tr>345<tr class="row-even"><td colspan="2"><p><strong>Summation and product functions</strong></p></td>346</tr>347<tr class="row-odd"><td><p><a class="reference internal" href="#math.dist" title="math.dist"><code class="xref py py-func docutils literal notranslate"><span class="pre">dist(p,</span> <span class="pre">q)</span></code></a></p></td>348<td><p>Euclidean distance between two points <em>p</em> and <em>q</em> given as an iterable of coordinates</p></td>349</tr>350<tr class="row-even"><td><p><a class="reference internal" href="#math.fsum" title="math.fsum"><code class="xref py py-func docutils literal notranslate"><span class="pre">fsum(iterable)</span></code></a></p></td>351<td><p>Sum of values in the input <em>iterable</em></p></td>352</tr>353<tr class="row-odd"><td><p><a class="reference internal" href="#math.hypot" title="math.hypot"><code class="xref py py-func docutils literal notranslate"><span class="pre">hypot(*coordinates)</span></code></a></p></td>354<td><p>Euclidean norm of an iterable of coordinates</p></td>355</tr>356<tr class="row-even"><td><p><a class="reference internal" href="#math.prod" title="math.prod"><code class="xref py py-func docutils literal notranslate"><span class="pre">prod(iterable,</span> <span class="pre">start)</span></code></a></p></td>357<td><p>Product of elements in the input <em>iterable</em> with a <em>start</em> value</p></td>358</tr>359<tr class="row-odd"><td><p><a class="reference internal" href="#math.sumprod" title="math.sumprod"><code class="xref py py-func docutils literal notranslate"><span class="pre">sumprod(p,</span> <span class="pre">q)</span></code></a></p></td>360<td><p>Sum of products from two iterables <em>p</em> and <em>q</em></p></td>361</tr>362<tr class="row-even"><td colspan="2"><p><strong>Angular conversion</strong></p></td>363</tr>364<tr class="row-odd"><td><p><a class="reference internal" href="#math.degrees" title="math.degrees"><code class="xref py py-func docutils literal notranslate"><span class="pre">degrees(x)</span></code></a></p></td>365<td><p>Convert angle <em>x</em> from radians to degrees</p></td>366</tr>367<tr class="row-even"><td><p><a class="reference internal" href="#math.radians" title="math.radians"><code class="xref py py-func docutils literal notranslate"><span class="pre">radians(x)</span></code></a></p></td>368<td><p>Convert angle <em>x</em> from degrees to radians</p></td>369</tr>370<tr class="row-odd"><td colspan="2"><p><strong>Trigonometric functions</strong></p></td>371</tr>372<tr class="row-even"><td><p><a class="reference internal" href="#math.acos" title="math.acos"><code class="xref py py-func docutils literal notranslate"><span class="pre">acos(x)</span></code></a></p></td>373<td><p>Arc cosine of <em>x</em></p></td>374</tr>375<tr class="row-odd"><td><p><a class="reference internal" href="#math.asin" title="math.asin"><code class="xref py py-func docutils literal notranslate"><span class="pre">asin(x)</span></code></a></p></td>376<td><p>Arc sine of <em>x</em></p></td>377</tr>378<tr class="row-even"><td><p><a class="reference internal" href="#math.atan" title="math.atan"><code class="xref py py-func docutils literal notranslate"><span class="pre">atan(x)</span></code></a></p></td>379<td><p>Arc tangent of <em>x</em></p></td>380</tr>381<tr class="row-odd"><td><p><a class="reference internal" href="#math.atan2" title="math.atan2"><code class="xref py py-func docutils literal notranslate"><span class="pre">atan2(y,</span> <span class="pre">x)</span></code></a></p></td>382<td><p><code class="docutils literal notranslate"><span class="pre">atan(y</span> <span class="pre">/</span> <span class="pre">x)</span></code></p></td>383</tr>384<tr class="row-even"><td><p><a class="reference internal" href="#math.cos" title="math.cos"><code class="xref py py-func docutils literal notranslate"><span class="pre">cos(x)</span></code></a></p></td>385<td><p>Cosine of <em>x</em></p></td>386</tr>387<tr class="row-odd"><td><p><a class="reference internal" href="#math.sin" title="math.sin"><code class="xref py py-func docutils literal notranslate"><span class="pre">sin(x)</span></code></a></p></td>388<td><p>Sine of <em>x</em></p></td>389</tr>390<tr class="row-even"><td><p><a class="reference internal" href="#math.tan" title="math.tan"><code class="xref py py-func docutils literal notranslate"><span class="pre">tan(x)</span></code></a></p></td>391<td><p>Tangent of <em>x</em></p></td>392</tr>393<tr class="row-odd"><td colspan="2"><p><strong>Hyperbolic functions</strong></p></td>394</tr>395<tr class="row-even"><td><p><a class="reference internal" href="#math.acosh" title="math.acosh"><code class="xref py py-func docutils literal notranslate"><span class="pre">acosh(x)</span></code></a></p></td>396<td><p>Inverse hyperbolic cosine of <em>x</em></p></td>397</tr>398<tr class="row-odd"><td><p><a class="reference internal" href="#math.asinh" title="math.asinh"><code class="xref py py-func docutils literal notranslate"><span class="pre">asinh(x)</span></code></a></p></td>399<td><p>Inverse hyperbolic sine of <em>x</em></p></td>400</tr>401<tr class="row-even"><td><p><a class="reference internal" href="#math.atanh" title="math.atanh"><code class="xref py py-func docutils literal notranslate"><span class="pre">atanh(x)</span></code></a></p></td>402<td><p>Inverse hyperbolic tangent of <em>x</em></p></td>403</tr>404<tr class="row-odd"><td><p><a class="reference internal" href="#math.cosh" title="math.cosh"><code class="xref py py-func docutils literal notranslate"><span class="pre">cosh(x)</span></code></a></p></td>405<td><p>Hyperbolic cosine of <em>x</em></p></td>406</tr>407<tr class="row-even"><td><p><a class="reference internal" href="#math.sinh" title="math.sinh"><code class="xref py py-func docutils literal notranslate"><span class="pre">sinh(x)</span></code></a></p></td>408<td><p>Hyperbolic sine of <em>x</em></p></td>409</tr>410<tr class="row-odd"><td><p><a class="reference internal" href="#math.tanh" title="math.tanh"><code class="xref py py-func docutils literal notranslate"><span class="pre">tanh(x)</span></code></a></p></td>411<td><p>Hyperbolic tangent of <em>x</em></p></td>412</tr>413<tr class="row-even"><td colspan="2"><p><strong>Special functions</strong></p></td>414</tr>415<tr class="row-odd"><td><p><a class="reference internal" href="#math.erf" title="math.erf"><code class="xref py py-func docutils literal notranslate"><span class="pre">erf(x)</span></code></a></p></td>416<td><p><a class="reference external" href="https://en.wikipedia.org/wiki/Error_function">Error function</a> at <em>x</em></p></td>417</tr>418<tr class="row-even"><td><p><a class="reference internal" href="#math.erfc" title="math.erfc"><code class="xref py py-func docutils literal notranslate"><span class="pre">erfc(x)</span></code></a></p></td>419<td><p><a class="reference external" href="https://en.wikipedia.org/wiki/Error_function">Complementary error function</a> at <em>x</em></p></td>420</tr>421<tr class="row-odd"><td><p><a class="reference internal" href="#math.gamma" title="math.gamma"><code class="xref py py-func docutils literal notranslate"><span class="pre">gamma(x)</span></code></a></p></td>422<td><p><a class="reference external" href="https://en.wikipedia.org/wiki/Gamma_function">Gamma function</a> at <em>x</em></p></td>423</tr>424<tr class="row-even"><td><p><a class="reference internal" href="#math.lgamma" title="math.lgamma"><code class="xref py py-func docutils literal notranslate"><span class="pre">lgamma(x)</span></code></a></p></td>425<td><p>Natural logarithm of the absolute value of the <a class="reference external" href="https://en.wikipedia.org/wiki/Gamma_function">Gamma function</a> at <em>x</em></p></td>426</tr>427<tr class="row-odd"><td colspan="2"><p><strong>Constants</strong></p></td>428</tr>429<tr class="row-even"><td><p><a class="reference internal" href="#math.pi" title="math.pi"><code class="xref py py-data docutils literal notranslate"><span class="pre">pi</span></code></a></p></td>430<td><p><em>π</em> = 3.141592…</p></td>431</tr>432<tr class="row-odd"><td><p><a class="reference internal" href="#math.e" title="math.e"><code class="xref py py-data docutils literal notranslate"><span class="pre">e</span></code></a></p></td>433<td><p><em>e</em> = 2.718281…</p></td>434</tr>435<tr class="row-even"><td><p><a class="reference internal" href="#math.tau" title="math.tau"><code class="xref py py-data docutils literal notranslate"><span class="pre">tau</span></code></a></p></td>436<td><p><em>τ</em> = 2<em>π</em> = 6.283185…</p></td>437</tr>438<tr class="row-odd"><td><p><a class="reference internal" href="#math.inf" title="math.inf"><code class="xref py py-data docutils literal notranslate"><span class="pre">inf</span></code></a></p></td>439<td><p>Positive infinity</p></td>440</tr>441<tr class="row-even"><td><p><a class="reference internal" href="#math.nan" title="math.nan"><code class="xref py py-data docutils literal notranslate"><span class="pre">nan</span></code></a></p></td>442<td><p>“Not a number” (NaN)</p></td>443</tr>444</tbody>445</table>446<section id="floating-point-arithmetic">447<h2>Floating point arithmetic<a class="headerlink" href="#floating-point-arithmetic" title="Link to this heading">¶</a></h2>448<dl class="py function">449<dt class="sig sig-object py" id="math.ceil">450<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">ceil</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="#math.ceil" title="Link to this definition">¶</a></dt>451<dd><p>Return the ceiling of <em>x</em>, the smallest integer greater than or equal to <em>x</em>.452If <em>x</em> is not a float, delegates to <a class="reference internal" href="../reference/datamodel.html#object.__ceil__" title="object.__ceil__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">x.__ceil__</span></code></a>,453which should return an <a class="reference internal" href="numbers.html#numbers.Integral" title="numbers.Integral"><code class="xref py py-class docutils literal notranslate"><span class="pre">Integral</span></code></a> value.</p>454</dd></dl>455 456<dl class="py function">457<dt class="sig sig-object py" id="math.fabs">458<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">fabs</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="#math.fabs" title="Link to this definition">¶</a></dt>459<dd><p>Return the absolute value of <em>x</em>.</p>460</dd></dl>461 462<dl class="py function">463<dt class="sig sig-object py" id="math.floor">464<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">floor</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="#math.floor" title="Link to this definition">¶</a></dt>465<dd><p>Return the floor of <em>x</em>, the largest integer less than or equal to <em>x</em>. If466<em>x</em> is not a float, delegates to <a class="reference internal" href="../reference/datamodel.html#object.__floor__" title="object.__floor__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">x.__floor__</span></code></a>, which467should return an <a class="reference internal" href="numbers.html#numbers.Integral" title="numbers.Integral"><code class="xref py py-class docutils literal notranslate"><span class="pre">Integral</span></code></a> value.</p>468</dd></dl>469 470<dl class="py function">471<dt class="sig sig-object py" id="math.fma">472<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">fma</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">y</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">z</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#math.fma" title="Link to this definition">¶</a></dt>473<dd><p>Fused multiply-add operation. Return <code class="docutils literal notranslate"><span class="pre">(x</span> <span class="pre">*</span> <span class="pre">y)</span> <span class="pre">+</span> <span class="pre">z</span></code>, computed as though with474infinite precision and range followed by a single round to the <code class="docutils literal notranslate"><span class="pre">float</span></code>475format. This operation often provides better accuracy than the direct476expression <code class="docutils literal notranslate"><span class="pre">(x</span> <span class="pre">*</span> <span class="pre">y)</span> <span class="pre">+</span> <span class="pre">z</span></code>.</p>477<p>This function follows the specification of the fusedMultiplyAdd operation478described in the IEEE 754 standard. The standard leaves one case479implementation-defined, namely the result of <code class="docutils literal notranslate"><span class="pre">fma(0,</span> <span class="pre">inf,</span> <span class="pre">nan)</span></code>480and <code class="docutils literal notranslate"><span class="pre">fma(inf,</span> <span class="pre">0,</span> <span class="pre">nan)</span></code>. In these cases, <code class="docutils literal notranslate"><span class="pre">math.fma</span></code> returns a NaN,481and does not raise any exception.</p>482<div class="versionadded">483<p><span class="versionmodified added">Added in version 3.13.</span></p>484</div>485</dd></dl>486 487<dl class="py function">488<dt class="sig sig-object py" id="math.fmax">489<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">fmax</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">y</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#math.fmax" title="Link to this definition">¶</a></dt>490<dd><p>Get the larger of two floating-point values, treating NaNs as missing data.</p>491<p>When both operands are (signed) NaNs or zeroes, return <code class="docutils literal notranslate"><span class="pre">nan</span></code> and <code class="docutils literal notranslate"><span class="pre">0</span></code>492respectively and the sign of the result is implementation-defined, that493is, <code class="xref py py-func docutils literal notranslate"><span class="pre">fmax()</span></code> is not required to be sensitive to the sign of such494operands (see Annex F of the C11 standard, §F.10.0.3 and §F.10.9.2).</p>495<div class="versionadded">496<p><span class="versionmodified added">Added in version 3.15.</span></p>497</div>498</dd></dl>499 500<dl class="py function">501<dt class="sig sig-object py" id="math.fmin">502<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">fmin</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">y</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#math.fmin" title="Link to this definition">¶</a></dt>503<dd><p>Get the smaller of two floating-point values, treating NaNs as missing data.</p>504<p>When both operands are (signed) NaNs or zeroes, return <code class="docutils literal notranslate"><span class="pre">nan</span></code> and <code class="docutils literal notranslate"><span class="pre">0</span></code>505respectively and the sign of the result is implementation-defined, that506is, <code class="xref py py-func docutils literal notranslate"><span class="pre">fmin()</span></code> is not required to be sensitive to the sign of such507operands (see Annex F of the C11 standard, §F.10.0.3 and §F.10.9.3).</p>508<div class="versionadded">509<p><span class="versionmodified added">Added in version 3.15.</span></p>510</div>511</dd></dl>512 513<dl class="py function">514<dt class="sig sig-object py" id="math.fmod">515<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">fmod</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">y</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#math.fmod" title="Link to this definition">¶</a></dt>516<dd><p>Return the floating-point remainder of <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">/</span> <span class="pre">y</span></code>,517as defined by the platform C library function <code class="docutils literal notranslate"><span class="pre">fmod(x,</span> <span class="pre">y)</span></code>. Note that the518Python expression <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">%</span> <span class="pre">y</span></code> may not return the same result. The intent of the C519standard is that <code class="docutils literal notranslate"><span class="pre">fmod(x,</span> <span class="pre">y)</span></code> be exactly (mathematically; to infinite520precision) equal to <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">-</span> <span class="pre">n*y</span></code> for some integer <em>n</em> such that the result has521the same sign as <em>x</em> and magnitude less than <code class="docutils literal notranslate"><span class="pre">abs(y)</span></code>. Python’s <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">%</span> <span class="pre">y</span></code>522returns a result with the sign of <em>y</em> instead, and may not be exactly computable523for float arguments. For example, <code class="docutils literal notranslate"><span class="pre">fmod(-1e-100,</span> <span class="pre">1e100)</span></code> is <code class="docutils literal notranslate"><span class="pre">-1e-100</span></code>, but524the result of Python’s <code class="docutils literal notranslate"><span class="pre">-1e-100</span> <span class="pre">%</span> <span class="pre">1e100</span></code> is <code class="docutils literal notranslate"><span class="pre">1e100-1e-100</span></code>, which cannot be525represented exactly as a float, and rounds to the surprising <code class="docutils literal notranslate"><span class="pre">1e100</span></code>. For526this reason, function <code class="xref py py-func docutils literal notranslate"><span class="pre">fmod()</span></code> is generally preferred when working with527floats, while Python’s <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">%</span> <span class="pre">y</span></code> is preferred when working with integers.</p>528</dd></dl>529 530<dl class="py function">531<dt class="sig sig-object py" id="math.modf">532<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">modf</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="#math.modf" title="Link to this definition">¶</a></dt>533<dd><p>Return the fractional and integer parts of <em>x</em>. Both results carry the sign534of <em>x</em> and are floats.</p>535<p>Note that <code class="xref py py-func docutils literal notranslate"><span class="pre">modf()</span></code> has a different call/return pattern536than its C equivalents: it takes a single argument and return a pair of537values, rather than returning its second return value through an ‘output538parameter’ (there is no such thing in Python).</p>539</dd></dl>540 541<dl class="py function">542<dt class="sig sig-object py" id="math.remainder">543<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">remainder</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">y</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#math.remainder" title="Link to this definition">¶</a></dt>544<dd><p>Return the IEEE 754-style remainder of <em>x</em> with respect to <em>y</em>. For545finite <em>x</em> and finite nonzero <em>y</em>, this is the difference <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">-</span> <span class="pre">n*y</span></code>,546where <code class="docutils literal notranslate"><span class="pre">n</span></code> is the closest integer to the exact value of the quotient <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">/</span>547<span class="pre">y</span></code>. If <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">/</span> <span class="pre">y</span></code> is exactly halfway between two consecutive integers, the548nearest <em>even</em> integer is used for <code class="docutils literal notranslate"><span class="pre">n</span></code>. The remainder <code class="docutils literal notranslate"><span class="pre">r</span> <span class="pre">=</span> <span class="pre">remainder(x,</span>549<span class="pre">y)</span></code> thus always satisfies <code class="docutils literal notranslate"><span class="pre">abs(r)</span> <span class="pre"><=</span> <span class="pre">0.5</span> <span class="pre">*</span> <span class="pre">abs(y)</span></code>.</p>550<p>Special cases follow IEEE 754: in particular, <code class="docutils literal notranslate"><span class="pre">remainder(x,</span> <span class="pre">math.inf)</span></code> is551<em>x</em> for any finite <em>x</em>, and <code class="docutils literal notranslate"><span class="pre">remainder(x,</span> <span class="pre">0)</span></code> and552<code class="docutils literal notranslate"><span class="pre">remainder(math.inf,</span> <span class="pre">x)</span></code> raise <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> for any non-NaN <em>x</em>.553If the result of the remainder operation is zero, that zero will have554the same sign as <em>x</em>.</p>555<p>On platforms using IEEE 754 binary floating point, the result of this556operation is always exactly representable: no rounding error is introduced.</p>557<div class="versionadded">558<p><span class="versionmodified added">Added in version 3.7.</span></p>559</div>560</dd></dl>561 562<dl class="py function">563<dt class="sig sig-object py" id="math.trunc">564<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">trunc</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="#math.trunc" title="Link to this definition">¶</a></dt>565<dd><p>Return <em>x</em> with the fractional part566removed, leaving the integer part. This rounds toward 0: <code class="docutils literal notranslate"><span class="pre">trunc()</span></code> is567equivalent to <a class="reference internal" href="#math.floor" title="math.floor"><code class="xref py py-func docutils literal notranslate"><span class="pre">floor()</span></code></a> for positive <em>x</em>, and equivalent to <a class="reference internal" href="#math.ceil" title="math.ceil"><code class="xref py py-func docutils literal notranslate"><span class="pre">ceil()</span></code></a>568for negative <em>x</em>. If <em>x</em> is not a float, delegates to <a class="reference internal" href="../reference/datamodel.html#object.__trunc__" title="object.__trunc__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">x.__trunc__</span></code></a>, which should return an <a class="reference internal" href="numbers.html#numbers.Integral" title="numbers.Integral"><code class="xref py py-class docutils literal notranslate"><span class="pre">Integral</span></code></a> value.</p>569</dd></dl>570 571<p>For the <a class="reference internal" href="#math.ceil" title="math.ceil"><code class="xref py py-func docutils literal notranslate"><span class="pre">ceil()</span></code></a>, <a class="reference internal" href="#math.floor" title="math.floor"><code class="xref py py-func docutils literal notranslate"><span class="pre">floor()</span></code></a>, and <a class="reference internal" href="#math.modf" title="math.modf"><code class="xref py py-func docutils literal notranslate"><span class="pre">modf()</span></code></a> functions, note that <em>all</em>572floating-point numbers of sufficiently large magnitude are exact integers.573Python floats typically carry no more than 53 bits of precision (the same as the574platform C double type), in which case any float <em>x</em> with <code class="docutils literal notranslate"><span class="pre">abs(x)</span> <span class="pre">>=</span> <span class="pre">2**52</span></code>575necessarily has no fractional bits.</p>576</section>577<section id="floating-point-manipulation-functions">578<h2>Floating point manipulation functions<a class="headerlink" href="#floating-point-manipulation-functions" title="Link to this heading">¶</a></h2>579<dl class="py function">580<dt class="sig sig-object py" id="math.copysign">581<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">copysign</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">y</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#math.copysign" title="Link to this definition">¶</a></dt>582<dd><p>Return a float with the magnitude (absolute value) of <em>x</em> but the sign of583<em>y</em>. On platforms that support signed zeros, <code class="docutils literal notranslate"><span class="pre">copysign(1.0,</span> <span class="pre">-0.0)</span></code>584returns <em>-1.0</em>.</p>585</dd></dl>586 587<dl class="py function">588<dt class="sig sig-object py" id="math.frexp">589<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">frexp</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="#math.frexp" title="Link to this definition">¶</a></dt>590<dd><p>Return the mantissa and exponent of <em>x</em> as the pair <code class="docutils literal notranslate"><span class="pre">(m,</span> <span class="pre">e)</span></code>. <em>m</em> is a float591and <em>e</em> is an integer such that <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">==</span> <span class="pre">m</span> <span class="pre">*</span> <span class="pre">2**e</span></code> exactly. If <em>x</em> is zero,592returns <code class="docutils literal notranslate"><span class="pre">(0.0,</span> <span class="pre">0)</span></code>, otherwise <code class="docutils literal notranslate"><span class="pre">0.5</span> <span class="pre"><=</span> <span class="pre">abs(m)</span> <span class="pre"><</span> <span class="pre">1</span></code>. This is used to “pick593apart” the internal representation of a float in a portable way.</p>594<p>Note that <code class="xref py py-func docutils literal notranslate"><span class="pre">frexp()</span></code> has a different call/return pattern595than its C equivalents: it takes a single argument and return a pair of596values, rather than returning its second return value through an ‘output597parameter’ (there is no such thing in Python).</p>598</dd></dl>599 600<dl class="py function">601<dt class="sig sig-object py" id="math.isclose">602<span class="sig-prename descclassname"><span class="pre">math.</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="#math.isclose" title="Link to this definition">¶</a></dt>603<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 and604<code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>605<p>Whether or not two values are considered close is determined according to606given absolute and relative tolerances. If no errors occur, the result will607be: <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>608<p><em>rel_tol</em> is the relative tolerance – it is the maximum allowed difference609between <em>a</em> and <em>b</em>, relative to the larger absolute value of <em>a</em> or <em>b</em>.610For example, to set a tolerance of 5%, pass <code class="docutils literal notranslate"><span class="pre">rel_tol=0.05</span></code>. The default611tolerance is <code class="docutils literal notranslate"><span class="pre">1e-09</span></code>, which assures that the two values are the same612within about 9 decimal digits. <em>rel_tol</em> must be nonnegative and less613than <code class="docutils literal notranslate"><span class="pre">1.0</span></code>.</p>614<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 be615nonnegative. 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 computed616as <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 nonzero <code class="docutils literal notranslate"><span class="pre">x</span></code> and617<em>rel_tol</em> less than <code class="docutils literal notranslate"><span class="pre">1.0</span></code>. So add an appropriate positive <em>abs_tol</em> argument618to the call.</p>619<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 be620handled according to IEEE rules. Specifically, <code class="docutils literal notranslate"><span class="pre">NaN</span></code> is not considered621close 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 only622considered close to themselves.</p>623<div class="versionadded">624<p><span class="versionmodified added">Added in version 3.5.</span></p>625</div>626<div class="admonition seealso">627<p class="admonition-title">See also</p>628<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>629</div>630</dd></dl>631 632<dl class="py function">633<dt class="sig sig-object py" id="math.isfinite">634<span class="sig-prename descclassname"><span class="pre">math.</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="#math.isfinite" title="Link to this definition">¶</a></dt>635<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if <em>x</em> is neither an infinity nor a NaN, and636<code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise. (Note that <code class="docutils literal notranslate"><span class="pre">0.0</span></code> <em>is</em> considered finite.)</p>637<div class="versionadded">638<p><span class="versionmodified added">Added in version 3.2.</span></p>639</div>640</dd></dl>641 642<dl class="py function">643<dt class="sig sig-object py" id="math.isnormal">644<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">isnormal</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="#math.isnormal" title="Link to this definition">¶</a></dt>645<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if <em>x</em> is a normal number, that is a finite646nonzero number that is not a subnormal (see <a class="reference internal" href="#math.issubnormal" title="math.issubnormal"><code class="xref py py-func docutils literal notranslate"><span class="pre">issubnormal()</span></code></a>).647Return <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>648<div class="versionadded">649<p><span class="versionmodified added">Added in version 3.15.</span></p>650</div>651</dd></dl>652 653<dl class="py function">654<dt class="sig sig-object py" id="math.issubnormal">655<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">issubnormal</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="#math.issubnormal" title="Link to this definition">¶</a></dt>656<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if <em>x</em> is a subnormal number, that is a finite657nonzero number with a magnitude smaller than <a class="reference internal" href="sys.html#sys.float_info.min" title="sys.float_info.min"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.float_info.min</span></code></a>.658Return <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>659<div class="versionadded">660<p><span class="versionmodified added">Added in version 3.15.</span></p>661</div>662</dd></dl>663 664<dl class="py function">665<dt class="sig sig-object py" id="math.isinf">666<span class="sig-prename descclassname"><span class="pre">math.</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="#math.isinf" title="Link to this definition">¶</a></dt>667<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if <em>x</em> is a positive or negative infinity, and668<code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>669</dd></dl>670 671<dl class="py function">672<dt class="sig sig-object py" id="math.isnan">673<span class="sig-prename descclassname"><span class="pre">math.</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="#math.isnan" title="Link to this definition">¶</a></dt>674<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if <em>x</em> is a NaN (not a number), and <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>675</dd></dl>676 677<dl class="py function">678<dt class="sig sig-object py" id="math.ldexp">679<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">ldexp</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">i</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#math.ldexp" title="Link to this definition">¶</a></dt>680<dd><p>Return <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">*</span> <span class="pre">(2**i)</span></code>. This is essentially the inverse of function681<a class="reference internal" href="#math.frexp" title="math.frexp"><code class="xref py py-func docutils literal notranslate"><span class="pre">frexp()</span></code></a>.</p>682</dd></dl>683 684<dl class="py function">685<dt class="sig sig-object py" id="math.nextafter">686<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">nextafter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">y</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">steps</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#math.nextafter" title="Link to this definition">¶</a></dt>687<dd><p>Return the floating-point value <em>steps</em> steps after <em>x</em> towards <em>y</em>.</p>688<p>If <em>x</em> is equal to <em>y</em>, return <em>y</em>, unless <em>steps</em> is zero.</p>689<p>Examples:</p>690<ul class="simple">691<li><p><code class="docutils literal notranslate"><span class="pre">math.nextafter(x,</span> <span class="pre">math.inf)</span></code> goes up: towards positive infinity.</p></li>692<li><p><code class="docutils literal notranslate"><span class="pre">math.nextafter(x,</span> <span class="pre">-math.inf)</span></code> goes down: towards minus infinity.</p></li>693<li><p><code class="docutils literal notranslate"><span class="pre">math.nextafter(x,</span> <span class="pre">0.0)</span></code> goes towards zero.</p></li>694<li><p><code class="docutils literal notranslate"><span class="pre">math.nextafter(x,</span> <span class="pre">math.copysign(math.inf,</span> <span class="pre">x))</span></code> goes away from zero.</p></li>695</ul>696<p>See also <a class="reference internal" href="#math.ulp" title="math.ulp"><code class="xref py py-func docutils literal notranslate"><span class="pre">math.ulp()</span></code></a>.</p>697<div class="versionadded">698<p><span class="versionmodified added">Added in version 3.9.</span></p>699</div>700<div class="versionchanged">701<p><span class="versionmodified changed">Changed in version 3.12: </span>Added the <em>steps</em> argument.</p>702</div>703</dd></dl>704 705<dl class="py function">706<dt class="sig sig-object py" id="math.signbit">707<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">signbit</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="#math.signbit" title="Link to this definition">¶</a></dt>708<dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the sign of <em>x</em> is negative and <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>709<p>This is useful to detect the sign bit of zeroes, infinities and NaNs.</p>710<div class="versionadded">711<p><span class="versionmodified added">Added in version 3.15.</span></p>712</div>713</dd></dl>714 715<dl class="py function">716<dt class="sig sig-object py" id="math.ulp">717<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">ulp</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="#math.ulp" title="Link to this definition">¶</a></dt>718<dd><p>Return the value of the least significant bit of the float <em>x</em>:</p>719<ul class="simple">720<li><p>If <em>x</em> is a NaN (not a number), return <em>x</em>.</p></li>721<li><p>If <em>x</em> is negative, return <code class="docutils literal notranslate"><span class="pre">ulp(-x)</span></code>.</p></li>722<li><p>If <em>x</em> is a positive infinity, return <em>x</em>.</p></li>723<li><p>If <em>x</em> is equal to zero, return the smallest positive724<em>denormalized</em> representable float (smaller than the minimum positive725<em>normalized</em> float, <a class="reference internal" href="sys.html#sys.float_info" title="sys.float_info"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.float_info.min</span></code></a>).</p></li>726<li><p>If <em>x</em> is equal to the largest positive representable float,727return the value of the least significant bit of <em>x</em>, such that the first728float smaller than <em>x</em> is <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">-</span> <span class="pre">ulp(x)</span></code>.</p></li>729<li><p>Otherwise (<em>x</em> is a positive finite number), return the value of the least730significant bit of <em>x</em>, such that the first float bigger than <em>x</em>731is <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">+</span> <span class="pre">ulp(x)</span></code>.</p></li>732</ul>733<p>ULP stands for “Unit in the Last Place”.</p>734<p>See also <a class="reference internal" href="#math.nextafter" title="math.nextafter"><code class="xref py py-func docutils literal notranslate"><span class="pre">math.nextafter()</span></code></a> and <a class="reference internal" href="sys.html#sys.float_info" title="sys.float_info"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.float_info.epsilon</span></code></a>.</p>735<div class="versionadded">736<p><span class="versionmodified added">Added in version 3.9.</span></p>737</div>738</dd></dl>739 740</section>741<section id="power-exponential-and-logarithmic-functions">742<h2>Power, exponential and logarithmic functions<a class="headerlink" href="#power-exponential-and-logarithmic-functions" title="Link to this heading">¶</a></h2>743<dl class="py function">744<dt class="sig sig-object py" id="math.cbrt">745<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">cbrt</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="#math.cbrt" title="Link to this definition">¶</a></dt>746<dd><p>Return the cube root of <em>x</em>.</p>747<div class="versionadded">748<p><span class="versionmodified added">Added in version 3.11.</span></p>749</div>750</dd></dl>751 752<dl class="py function">753<dt class="sig sig-object py" id="math.exp">754<span class="sig-prename descclassname"><span class="pre">math.</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="#math.exp" title="Link to this definition">¶</a></dt>755<dd><p>Return <em>e</em> raised to the power <em>x</em>, where <em>e</em> = 2.718281… is the base756of natural logarithms. This is usually more accurate than <code class="docutils literal notranslate"><span class="pre">math.e</span> <span class="pre">**</span> <span class="pre">x</span></code>757or <code class="docutils literal notranslate"><span class="pre">pow(math.e,</span> <span class="pre">x)</span></code>.</p>758</dd></dl>759 760<dl class="py function">761<dt class="sig sig-object py" id="math.exp2">762<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">exp2</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="#math.exp2" title="Link to this definition">¶</a></dt>763<dd><p>Return <em>2</em> raised to the power <em>x</em>.</p>764<div class="versionadded">765<p><span class="versionmodified added">Added in version 3.11.</span></p>766</div>767</dd></dl>768 769<dl class="py function">770<dt class="sig sig-object py" id="math.expm1">771<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">expm1</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="#math.expm1" title="Link to this definition">¶</a></dt>772<dd><p>Return <em>e</em> raised to the power <em>x</em>, minus 1. Here <em>e</em> is the base of natural773logarithms. For small floats <em>x</em>, the subtraction in <code class="docutils literal notranslate"><span class="pre">exp(x)</span> <span class="pre">-</span> <span class="pre">1</span></code>774can result in a <a class="reference external" href="https://en.wikipedia.org/wiki/Loss_of_significance">significant loss of precision</a>; the <code class="xref py py-func docutils literal notranslate"><span class="pre">expm1()</span></code>775function provides a way to compute this quantity to full precision:</p>776<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span><span class="w"> </span><span class="nn">math</span><span class="w"> </span><span class="kn">import</span> <span class="n">exp</span><span class="p">,</span> <span class="n">expm1</span>777<span class="gp">>>> </span><span class="n">exp</span><span class="p">(</span><span class="mf">1e-5</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span> <span class="c1"># gives result accurate to 11 places</span>778<span class="go">1.0000050000069649e-05</span>779<span class="gp">>>> </span><span class="n">expm1</span><span class="p">(</span><span class="mf">1e-5</span><span class="p">)</span> <span class="c1"># result accurate to full precision</span>780<span class="go">1.0000050000166668e-05</span>781</pre></div>782</div>783<div class="versionadded">784<p><span class="versionmodified added">Added in version 3.2.</span></p>785</div>786</dd></dl>787 788<dl class="py function">789<dt class="sig sig-object py" id="math.log">790<span class="sig-prename descclassname"><span class="pre">math.</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="#math.log" title="Link to this definition">¶</a></dt>791<dd><p>With one argument, return the natural logarithm of <em>x</em> (to base <em>e</em>).</p>792<p>With two arguments, return the logarithm of <em>x</em> to the given <em>base</em>,793calculated as <code class="docutils literal notranslate"><span class="pre">log(x)/log(base)</span></code>.</p>794</dd></dl>795 796<dl class="py function">797<dt class="sig sig-object py" id="math.log1p">798<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">log1p</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="#math.log1p" title="Link to this definition">¶</a></dt>799<dd><p>Return the natural logarithm of <em>1+x</em> (base <em>e</em>). The800result is calculated in a way which is accurate for <em>x</em> near zero.</p>801</dd></dl>802 803<dl class="py function">804<dt class="sig sig-object py" id="math.log2">805<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">log2</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="#math.log2" title="Link to this definition">¶</a></dt>806<dd><p>Return the base-2 logarithm of <em>x</em>. This is usually more accurate than807<code class="docutils literal notranslate"><span class="pre">log(x,</span> <span class="pre">2)</span></code>.</p>808<div class="versionadded">809<p><span class="versionmodified added">Added in version 3.3.</span></p>810</div>811<div class="admonition seealso">812<p class="admonition-title">See also</p>813<p><a class="reference internal" href="stdtypes.html#int.bit_length" title="int.bit_length"><code class="xref py py-meth docutils literal notranslate"><span class="pre">int.bit_length()</span></code></a> returns the number of bits necessary to represent814an integer in binary, excluding the sign and leading zeros.</p>815</div>816</dd></dl>817 818<dl class="py function">819<dt class="sig sig-object py" id="math.log10">820<span class="sig-prename descclassname"><span class="pre">math.</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="#math.log10" title="Link to this definition">¶</a></dt>821<dd><p>Return the base-10 logarithm of <em>x</em>. This is usually more accurate822than <code class="docutils literal notranslate"><span class="pre">log(x,</span> <span class="pre">10)</span></code>.</p>823</dd></dl>824 825<dl class="py function">826<dt class="sig sig-object py" id="math.pow">827<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">pow</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">y</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#math.pow" title="Link to this definition">¶</a></dt>828<dd><p>Return <em>x</em> raised to the power <em>y</em>. Exceptional cases follow829the IEEE 754 standard as far as possible. In particular,830<code class="docutils literal notranslate"><span class="pre">pow(1.0,</span> <span class="pre">x)</span></code> and <code class="docutils literal notranslate"><span class="pre">pow(x,</span> <span class="pre">0.0)</span></code> always return <code class="docutils literal notranslate"><span class="pre">1.0</span></code>, even831when <em>x</em> is a zero or a NaN. If both <em>x</em> and <em>y</em> are finite,832<em>x</em> is negative, and <em>y</em> is not an integer then <code class="docutils literal notranslate"><span class="pre">pow(x,</span> <span class="pre">y)</span></code>833is undefined, and raises <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>.</p>834<p>Unlike the built-in <code class="docutils literal notranslate"><span class="pre">**</span></code> operator, <a class="reference internal" href="#math.pow" title="math.pow"><code class="xref py py-func docutils literal notranslate"><span class="pre">math.pow()</span></code></a> converts both835its arguments to type <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a>. Use <code class="docutils literal notranslate"><span class="pre">**</span></code> or the built-in836<code class="xref py py-func docutils literal notranslate"><span class="pre">pow()</span></code> function for computing exact integer powers.</p>837<div class="versionchanged">838<p><span class="versionmodified changed">Changed in version 3.11: </span>The special cases <code class="docutils literal notranslate"><span class="pre">pow(0.0,</span> <span class="pre">-inf)</span></code> and <code class="docutils literal notranslate"><span class="pre">pow(-0.0,</span> <span class="pre">-inf)</span></code> were839changed to return <code class="docutils literal notranslate"><span class="pre">inf</span></code> instead of raising <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>,840for consistency with IEEE 754.</p>841</div>842</dd></dl>843 844<dl class="py function">845<dt class="sig sig-object py" id="math.sqrt">846<span class="sig-prename descclassname"><span class="pre">math.</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="#math.sqrt" title="Link to this definition">¶</a></dt>847<dd><p>Return the square root of <em>x</em>.</p>848</dd></dl>849 850</section>851<section id="summation-and-product-functions">852<h2>Summation and product functions<a class="headerlink" href="#summation-and-product-functions" title="Link to this heading">¶</a></h2>853<dl class="py function">854<dt class="sig sig-object py" id="math.dist">855<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">dist</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">p</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">q</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#math.dist" title="Link to this definition">¶</a></dt>856<dd><p>Return the Euclidean distance between two points <em>p</em> and <em>q</em>, each857given as a sequence (or iterable) of coordinates. The two points858must have the same dimension.</p>859<p>Roughly equivalent to:</p>860<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">sqrt</span><span class="p">(</span><span class="nb">sum</span><span class="p">((</span><span class="n">px</span> <span class="o">-</span> <span class="n">qx</span><span class="p">)</span> <span class="o">**</span> <span class="mf">2.0</span> <span class="k">for</span> <span class="n">px</span><span class="p">,</span> <span class="n">qx</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">q</span><span class="p">,</span> <span class="n">strict</span><span class="o">=</span><span class="kc">True</span><span class="p">)))</span>861</pre></div>862</div>863<div class="versionadded">864<p><span class="versionmodified added">Added in version 3.8.</span></p>865</div>866</dd></dl>867 868<dl class="py function">869<dt class="sig sig-object py" id="math.fsum">870<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">fsum</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">iterable</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#math.fsum" title="Link to this definition">¶</a></dt>871<dd><p>Return an accurate floating-point sum of values in the iterable. Avoids872loss of precision by tracking multiple intermediate partial sums.</p>873<p>The algorithm’s accuracy depends on IEEE-754 arithmetic guarantees and the874typical case where the rounding mode is half-even. On some non-Windows875builds, the underlying C library uses extended precision addition and may876occasionally double-round an intermediate sum causing it to be off in its877least significant bit.</p>878<p>For further discussion and two alternative approaches, see the <a class="reference external" href="https://code.activestate.com/recipes/393090-binary-floating-point-summation-accurate-to-full-p/">ASPN cookbook879recipes for accurate floating-point summation</a>.</p>880</dd></dl>881 882<dl class="py function">883<dt class="sig sig-object py" id="math.hypot">884<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">hypot</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">coordinates</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#math.hypot" title="Link to this definition">¶</a></dt>885<dd><p>Return the Euclidean norm, <code class="docutils literal notranslate"><span class="pre">sqrt(sum(x**2</span> <span class="pre">for</span> <span class="pre">x</span> <span class="pre">in</span> <span class="pre">coordinates))</span></code>.886This is the length of the vector from the origin to the point887given by the coordinates.</p>888<p>For a two dimensional point <code class="docutils literal notranslate"><span class="pre">(x,</span> <span class="pre">y)</span></code>, this is equivalent to computing889the hypotenuse of a right triangle using the Pythagorean theorem,890<code class="docutils literal notranslate"><span class="pre">sqrt(x*x</span> <span class="pre">+</span> <span class="pre">y*y)</span></code>.</p>891<div class="versionchanged">892<p><span class="versionmodified changed">Changed in version 3.8: </span>Added support for n-dimensional points. Formerly, only the two893dimensional case was supported.</p>894</div>895<div class="versionchanged">896<p><span class="versionmodified changed">Changed in version 3.10: </span>Improved the algorithm’s accuracy so that the maximum error is897under 1 ulp (unit in the last place). More typically, the result898is almost always correctly rounded to within 1/2 ulp.</p>899</div>900</dd></dl>901 902<dl class="py function">903<dt class="sig sig-object py" id="math.prod">904<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">prod</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">iterable</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">start</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#math.prod" title="Link to this definition">¶</a></dt>905<dd><p>Calculate the product of all the elements in the input <em>iterable</em>.906The default <em>start</em> value for the product is <code class="docutils literal notranslate"><span class="pre">1</span></code>.</p>907<p>When the iterable is empty, return the start value. This function is908intended specifically for use with numeric values and may reject909non-numeric types.</p>910<div class="versionadded">911<p><span class="versionmodified added">Added in version 3.8.</span></p>912</div>913</dd></dl>914 915<dl class="py function">916<dt class="sig sig-object py" id="math.sumprod">917<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">sumprod</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">p</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">q</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#math.sumprod" title="Link to this definition">¶</a></dt>918<dd><p>Return the sum of products of values from two iterables <em>p</em> and <em>q</em>.</p>919<p>Raises <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> if the inputs do not have the same length.</p>920<p>Roughly equivalent to:</p>921<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">sum</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="n">operator</span><span class="o">.</span><span class="n">mul</span><span class="p">,</span> <span class="n">p</span><span class="p">,</span> <span class="n">q</span><span class="p">,</span> <span class="n">strict</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>922</pre></div>923</div>924<p>For float and mixed int/float inputs, the intermediate products925and sums are computed with extended precision.</p>926<div class="versionadded">927<p><span class="versionmodified added">Added in version 3.12.</span></p>928</div>929</dd></dl>930 931</section>932<section id="angular-conversion">933<h2>Angular conversion<a class="headerlink" href="#angular-conversion" title="Link to this heading">¶</a></h2>934<dl class="py function">935<dt class="sig sig-object py" id="math.degrees">936<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">degrees</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="#math.degrees" title="Link to this definition">¶</a></dt>937<dd><p>Convert angle <em>x</em> from radians to degrees.</p>938</dd></dl>939 940<dl class="py function">941<dt class="sig sig-object py" id="math.radians">942<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">radians</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="#math.radians" title="Link to this definition">¶</a></dt>943<dd><p>Convert angle <em>x</em> from degrees to radians.</p>944</dd></dl>945 946</section>947<section id="trigonometric-functions">948<h2>Trigonometric functions<a class="headerlink" href="#trigonometric-functions" title="Link to this heading">¶</a></h2>949<dl class="py function">950<dt class="sig sig-object py" id="math.acos">951<span class="sig-prename descclassname"><span class="pre">math.</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="#math.acos" title="Link to this definition">¶</a></dt>952<dd><p>Return the arc cosine of <em>x</em>, in radians. The result is between <code class="docutils literal notranslate"><span class="pre">0</span></code> and953<code class="docutils literal notranslate"><span class="pre">pi</span></code>.</p>954</dd></dl>955 956<dl class="py function">957<dt class="sig sig-object py" id="math.asin">958<span class="sig-prename descclassname"><span class="pre">math.</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="#math.asin" title="Link to this definition">¶</a></dt>959<dd><p>Return the arc sine of <em>x</em>, in radians. The result is between <code class="docutils literal notranslate"><span class="pre">-pi/2</span></code> and960<code class="docutils literal notranslate"><span class="pre">pi/2</span></code>.</p>961</dd></dl>962 963<dl class="py function">964<dt class="sig sig-object py" id="math.atan">965<span class="sig-prename descclassname"><span class="pre">math.</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="#math.atan" title="Link to this definition">¶</a></dt>966<dd><p>Return the arc tangent of <em>x</em>, in radians. The result is between <code class="docutils literal notranslate"><span class="pre">-pi/2</span></code> and967<code class="docutils literal notranslate"><span class="pre">pi/2</span></code>.</p>968</dd></dl>969 970<dl class="py function">971<dt class="sig sig-object py" id="math.atan2">972<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">atan2</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">y</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#math.atan2" title="Link to this definition">¶</a></dt>973<dd><p>Return <code class="docutils literal notranslate"><span class="pre">atan(y</span> <span class="pre">/</span> <span class="pre">x)</span></code>, in radians. The result is between <code class="docutils literal notranslate"><span class="pre">-pi</span></code> and <code class="docutils literal notranslate"><span class="pre">pi</span></code>.974The vector in the plane from the origin to point <code class="docutils literal notranslate"><span class="pre">(x,</span> <span class="pre">y)</span></code> makes this angle975with the positive X axis. The point of <code class="xref py py-func docutils literal notranslate"><span class="pre">atan2()</span></code> is that the signs of both976inputs are known to it, so it can compute the correct quadrant for the angle.977For example, <code class="docutils literal notranslate"><span class="pre">atan(1)</span></code> and <code class="docutils literal notranslate"><span class="pre">atan2(1,</span> <span class="pre">1)</span></code> are both <code class="docutils literal notranslate"><span class="pre">pi/4</span></code>, but <code class="docutils literal notranslate"><span class="pre">atan2(-1,</span>978<span class="pre">-1)</span></code> is <code class="docutils literal notranslate"><span class="pre">-3*pi/4</span></code>.</p>979</dd></dl>980 981<dl class="py function">982<dt class="sig sig-object py" id="math.cos">983<span class="sig-prename descclassname"><span class="pre">math.</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="#math.cos" title="Link to this definition">¶</a></dt>984<dd><p>Return the cosine of <em>x</em> radians.</p>985</dd></dl>986 987<dl class="py function">988<dt class="sig sig-object py" id="math.sin">989<span class="sig-prename descclassname"><span class="pre">math.</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="#math.sin" title="Link to this definition">¶</a></dt>990<dd><p>Return the sine of <em>x</em> radians.</p>991</dd></dl>992 993<dl class="py function">994<dt class="sig sig-object py" id="math.tan">995<span class="sig-prename descclassname"><span class="pre">math.</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="#math.tan" title="Link to this definition">¶</a></dt>996<dd><p>Return the tangent of <em>x</em> radians.</p>997</dd></dl>998 999</section>1000<section id="hyperbolic-functions">1001<h2>Hyperbolic functions<a class="headerlink" href="#hyperbolic-functions" title="Link to this heading">¶</a></h2>1002<p><a class="reference external" href="https://en.wikipedia.org/wiki/Hyperbolic_functions">Hyperbolic functions</a>1003are analogs of trigonometric functions that are based on hyperbolas1004instead of circles.</p>1005<dl class="py function">1006<dt class="sig sig-object py" id="math.acosh">1007<span class="sig-prename descclassname"><span class="pre">math.</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="#math.acosh" title="Link to this definition">¶</a></dt>1008<dd><p>Return the inverse hyperbolic cosine of <em>x</em>.</p>1009</dd></dl>1010 1011<dl class="py function">1012<dt class="sig sig-object py" id="math.asinh">1013<span class="sig-prename descclassname"><span class="pre">math.</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="#math.asinh" title="Link to this definition">¶</a></dt>1014<dd><p>Return the inverse hyperbolic sine of <em>x</em>.</p>1015</dd></dl>1016 1017<dl class="py function">1018<dt class="sig sig-object py" id="math.atanh">1019<span class="sig-prename descclassname"><span class="pre">math.</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="#math.atanh" title="Link to this definition">¶</a></dt>1020<dd><p>Return the inverse hyperbolic tangent of <em>x</em>.</p>1021</dd></dl>1022 1023<dl class="py function">1024<dt class="sig sig-object py" id="math.cosh">1025<span class="sig-prename descclassname"><span class="pre">math.</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="#math.cosh" title="Link to this definition">¶</a></dt>1026<dd><p>Return the hyperbolic cosine of <em>x</em>.</p>1027</dd></dl>1028 1029<dl class="py function">1030<dt class="sig sig-object py" id="math.sinh">1031<span class="sig-prename descclassname"><span class="pre">math.</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="#math.sinh" title="Link to this definition">¶</a></dt>1032<dd><p>Return the hyperbolic sine of <em>x</em>.</p>1033</dd></dl>1034 1035<dl class="py function">1036<dt class="sig sig-object py" id="math.tanh">1037<span class="sig-prename descclassname"><span class="pre">math.</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="#math.tanh" title="Link to this definition">¶</a></dt>1038<dd><p>Return the hyperbolic tangent of <em>x</em>.</p>1039</dd></dl>1040 1041</section>1042<section id="special-functions">1043<h2>Special functions<a class="headerlink" href="#special-functions" title="Link to this heading">¶</a></h2>1044<dl class="py function">1045<dt class="sig sig-object py" id="math.erf">1046<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">erf</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="#math.erf" title="Link to this definition">¶</a></dt>1047<dd><p>Return the <a class="reference external" href="https://en.wikipedia.org/wiki/Error_function">error function</a> at1048<em>x</em>.</p>1049<p>The <code class="xref py py-func docutils literal notranslate"><span class="pre">erf()</span></code> function can be used to compute traditional statistical1050functions such as the <a class="reference external" href="https://en.wikipedia.org/wiki/Cumulative_distribution_function">cumulative standard normal distribution</a>:</p>1051<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">phi</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>1052 <span class="s1">'Cumulative distribution function for the standard normal distribution'</span>1053 <span class="k">return</span> <span class="p">(</span><span class="mf">1.0</span> <span class="o">+</span> <span class="n">erf</span><span class="p">(</span><span class="n">x</span> <span class="o">/</span> <span class="n">sqrt</span><span class="p">(</span><span class="mf">2.0</span><span class="p">)))</span> <span class="o">/</span> <span class="mf">2.0</span>1054</pre></div>1055</div>1056<div class="versionadded">1057<p><span class="versionmodified added">Added in version 3.2.</span></p>1058</div>1059</dd></dl>1060 1061<dl class="py function">1062<dt class="sig sig-object py" id="math.erfc">1063<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">erfc</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="#math.erfc" title="Link to this definition">¶</a></dt>1064<dd><p>Return the complementary error function at <em>x</em>. The <a class="reference external" href="https://en.wikipedia.org/wiki/Error_function">complementary error1065function</a> is defined as1066<code class="docutils literal notranslate"><span class="pre">1.0</span> <span class="pre">-</span> <span class="pre">erf(x)</span></code>. It is used for large values of <em>x</em> where a subtraction1067from one would cause a <a class="reference external" href="https://en.wikipedia.org/wiki/Loss_of_significance">loss of significance</a>.</p>1068<div class="versionadded">1069<p><span class="versionmodified added">Added in version 3.2.</span></p>1070</div>1071</dd></dl>1072 1073<dl class="py function">1074<dt class="sig sig-object py" id="math.gamma">1075<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">gamma</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="#math.gamma" title="Link to this definition">¶</a></dt>1076<dd><p>Return the <a class="reference external" href="https://en.wikipedia.org/wiki/Gamma_function">Gamma function</a> at1077<em>x</em>.</p>1078<div class="versionadded">1079<p><span class="versionmodified added">Added in version 3.2.</span></p>1080</div>1081</dd></dl>1082 1083<dl class="py function">1084<dt class="sig sig-object py" id="math.lgamma">1085<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">lgamma</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="#math.lgamma" title="Link to this definition">¶</a></dt>1086<dd><p>Return the natural logarithm of the absolute value of the Gamma1087function at <em>x</em>.</p>1088<div class="versionadded">1089<p><span class="versionmodified added">Added in version 3.2.</span></p>1090</div>1091</dd></dl>1092 1093</section>1094<section id="number-theoretic-functions">1095<h2>Number-theoretic functions<a class="headerlink" href="#number-theoretic-functions" title="Link to this heading">¶</a></h2>1096<p>For backward compatibility, the <code class="xref py py-mod docutils literal notranslate"><span class="pre">math</span></code> module provides also aliases of1097the following functions from the <a class="reference internal" href="math.integer.html#module-math.integer" title="math.integer: Integer-specific mathematics functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">math.integer</span></code></a> module:</p>1098<table class="docutils align-default">1099<tbody>1100<tr class="row-odd"><td><p id="math.comb"><a class="reference internal" href="math.integer.html#math.integer.comb" title="math.integer.comb"><code class="xref py py-func docutils literal notranslate"><span class="pre">comb(n,</span> <span class="pre">k)</span></code></a></p>1101</td>1102<td><p>Number of ways to choose <em>k</em> items from <em>n</em> items without repetition1103and without order</p></td>1104</tr>1105<tr class="row-even"><td><p id="math.factorial"><a class="reference internal" href="math.integer.html#math.integer.factorial" title="math.integer.factorial"><code class="xref py py-func docutils literal notranslate"><span class="pre">factorial(n)</span></code></a></p>1106</td>1107<td><p><em>n</em> factorial</p></td>1108</tr>1109<tr class="row-odd"><td><p id="math.gcd"><a class="reference internal" href="math.integer.html#math.integer.gcd" title="math.integer.gcd"><code class="xref py py-func docutils literal notranslate"><span class="pre">gcd(*integers)</span></code></a></p>1110</td>1111<td><p>Greatest common divisor of the integer arguments</p></td>1112</tr>1113<tr class="row-even"><td><p id="math.isqrt"><a class="reference internal" href="math.integer.html#math.integer.isqrt" title="math.integer.isqrt"><code class="xref py py-func docutils literal notranslate"><span class="pre">isqrt(n)</span></code></a></p>1114</td>1115<td><p>Integer square root of a nonnegative integer <em>n</em></p></td>1116</tr>1117<tr class="row-odd"><td><p id="math.lcm"><a class="reference internal" href="math.integer.html#math.integer.lcm" title="math.integer.lcm"><code class="xref py py-func docutils literal notranslate"><span class="pre">lcm(*integers)</span></code></a></p>1118</td>1119<td><p>Least common multiple of the integer arguments</p></td>1120</tr>1121<tr class="row-even"><td><p id="math.perm"><a class="reference internal" href="math.integer.html#math.integer.perm" title="math.integer.perm"><code class="xref py py-func docutils literal notranslate"><span class="pre">perm(n,</span> <span class="pre">k)</span></code></a></p>1122</td>1123<td><p>Number of ways to choose <em>k</em> items from <em>n</em> items without repetition1124and with order</p></td>1125</tr>1126</tbody>1127</table>1128<div class="versionadded">1129<p><span class="versionmodified added">Added in version 3.5: </span>The <a class="reference internal" href="#math.gcd" title="math.gcd"><code class="xref py py-func docutils literal notranslate"><span class="pre">gcd()</span></code></a> function.</p>1130</div>1131<div class="versionadded">1132<p><span class="versionmodified added">Added in version 3.8: </span>The <a class="reference internal" href="#math.comb" title="math.comb"><code class="xref py py-func docutils literal notranslate"><span class="pre">comb()</span></code></a>, <a class="reference internal" href="#math.perm" title="math.perm"><code class="xref py py-func docutils literal notranslate"><span class="pre">perm()</span></code></a> and <a class="reference internal" href="#math.isqrt" title="math.isqrt"><code class="xref py py-func docutils literal notranslate"><span class="pre">isqrt()</span></code></a> functions.</p>1133</div>1134<div class="versionadded">1135<p><span class="versionmodified added">Added in version 3.9: </span>The <a class="reference internal" href="#math.lcm" title="math.lcm"><code class="xref py py-func docutils literal notranslate"><span class="pre">lcm()</span></code></a> function.</p>1136</div>1137<div class="versionchanged">1138<p><span class="versionmodified changed">Changed in version 3.9: </span>Added support for an arbitrary number of arguments in the <a class="reference internal" href="#math.gcd" title="math.gcd"><code class="xref py py-func docutils literal notranslate"><span class="pre">gcd()</span></code></a>1139function.1140Formerly, only two arguments were supported.</p>1141</div>1142<div class="versionchanged">1143<p><span class="versionmodified changed">Changed in version 3.10: </span>Floats with integral values (like <code class="docutils literal notranslate"><span class="pre">5.0</span></code>) are no longer accepted in the1144<a class="reference internal" href="#math.factorial" title="math.factorial"><code class="xref py py-func docutils literal notranslate"><span class="pre">factorial()</span></code></a> function.</p>1145</div>1146<div class="deprecated">1147<p><span class="versionmodified deprecated">Deprecated since version 3.15: </span>These aliases are <a class="reference internal" href="../glossary.html#term-soft-deprecated"><span class="xref std std-term">soft deprecated</span></a> in favor of the1148<a class="reference internal" href="math.integer.html#module-math.integer" title="math.integer: Integer-specific mathematics functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">math.integer</span></code></a> functions.</p>1149</div>1150</section>1151<section id="constants">1152<h2>Constants<a class="headerlink" href="#constants" title="Link to this heading">¶</a></h2>1153<dl class="py data">1154<dt class="sig sig-object py" id="math.pi">1155<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">pi</span></span><a class="headerlink" href="#math.pi" title="Link to this definition">¶</a></dt>1156<dd><p>The mathematical constant <em>π</em> = 3.141592…, to available precision.</p>1157</dd></dl>1158 1159<dl class="py data">1160<dt class="sig sig-object py" id="math.e">1161<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">e</span></span><a class="headerlink" href="#math.e" title="Link to this definition">¶</a></dt>1162<dd><p>The mathematical constant <em>e</em> = 2.718281…, to available precision.</p>1163</dd></dl>1164 1165<dl class="py data">1166<dt class="sig sig-object py" id="math.tau">1167<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">tau</span></span><a class="headerlink" href="#math.tau" title="Link to this definition">¶</a></dt>1168<dd><p>The mathematical constant <em>τ</em> = 6.283185…, to available precision.1169Tau is a circle constant equal to 2<em>π</em>, the ratio of a circle’s circumference to1170its radius. To learn more about Tau, check out Vi Hart’s video <a class="reference external" href="https://vimeo.com/147792667">Pi is (still)1171Wrong</a>, and start celebrating1172<a class="reference external" href="https://tauday.com/">Tau day</a> by eating twice as much pie!</p>1173<div class="versionadded">1174<p><span class="versionmodified added">Added in version 3.6.</span></p>1175</div>1176</dd></dl>1177 1178<dl class="py data">1179<dt class="sig sig-object py" id="math.inf">1180<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">inf</span></span><a class="headerlink" href="#math.inf" title="Link to this definition">¶</a></dt>1181<dd><p>A floating-point positive infinity. (For negative infinity, use1182<code class="docutils literal notranslate"><span class="pre">-math.inf</span></code>.) Equivalent to the output of <code class="docutils literal notranslate"><span class="pre">float('inf')</span></code>.</p>1183<div class="versionadded">1184<p><span class="versionmodified added">Added in version 3.5.</span></p>1185</div>1186</dd></dl>1187 1188<dl class="py data">1189<dt class="sig sig-object py" id="math.nan">1190<span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">nan</span></span><a class="headerlink" href="#math.nan" title="Link to this definition">¶</a></dt>1191<dd><p>A floating-point “not a number” (NaN) value. Equivalent to the output of1192<code class="docutils literal notranslate"><span class="pre">float('nan')</span></code>. Due to the requirements of the <a class="reference external" href="https://en.wikipedia.org/wiki/IEEE_754">IEEE-754 standard</a>, <code class="docutils literal notranslate"><span class="pre">math.nan</span></code> and <code class="docutils literal notranslate"><span class="pre">float('nan')</span></code> are1193not considered to equal to any other numeric value, including themselves. To check1194whether a number is a NaN, use the <a class="reference internal" href="#math.isnan" title="math.isnan"><code class="xref py py-func docutils literal notranslate"><span class="pre">isnan()</span></code></a> function to test1195for NaNs instead of <code class="docutils literal notranslate"><span class="pre">is</span></code> or <code class="docutils literal notranslate"><span class="pre">==</span></code>.1196Example:</p>1197<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">math</span>1198<span class="gp">>>> </span><span class="n">math</span><span class="o">.</span><span class="n">nan</span> <span class="o">==</span> <span class="n">math</span><span class="o">.</span><span class="n">nan</span>1199<span class="go">False</span>1200<span class="gp">>>> </span><span class="nb">float</span><span class="p">(</span><span class="s1">'nan'</span><span class="p">)</span> <span class="o">==</span> <span class="nb">float</span><span class="p">(</span><span class="s1">'nan'</span><span class="p">)</span>