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

1091 lines
91KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="math — Mathematical functions" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/math.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="This module provides access to the mathematical functions defined by the C standard. These functions cannot be used with complex numbers; use the functions of the same name from the cmath module if..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="This module provides access to the mathematical functions defined by the C standard. These functions cannot be used with complex numbers; use the functions of the same name from the cmath module if..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>math — Mathematical functions &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="cmath — Mathematical functions for complex numbers" href="cmath.html" />
  33. <link rel="prev" title="numbers — Numeric abstract base classes" href="numbers.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/math.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">math</span></code> — Mathematical functions</a><ul>
  86. <li><a class="reference internal" href="#number-theoretic-and-representation-functions">Number-theoretic and representation functions</a></li>
  87. <li><a class="reference internal" href="#power-and-logarithmic-functions">Power and logarithmic functions</a></li>
  88. <li><a class="reference internal" href="#trigonometric-functions">Trigonometric functions</a></li>
  89. <li><a class="reference internal" href="#angular-conversion">Angular conversion</a></li>
  90. <li><a class="reference internal" href="#hyperbolic-functions">Hyperbolic functions</a></li>
  91. <li><a class="reference internal" href="#special-functions">Special functions</a></li>
  92. <li><a class="reference internal" href="#constants">Constants</a></li>
  93. </ul>
  94. </li>
  95. </ul>
  96. </div>
  97. <div>
  98. <h4>Previous topic</h4>
  99. <p class="topless"><a href="numbers.html"
  100. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">numbers</span></code> — Numeric abstract base classes</a></p>
  101. </div>
  102. <div>
  103. <h4>Next topic</h4>
  104. <p class="topless"><a href="cmath.html"
  105. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">cmath</span></code> — Mathematical functions for complex numbers</a></p>
  106. </div>
  107. <div role="note" aria-label="source link">
  108. <h3>This Page</h3>
  109. <ul class="this-page-menu">
  110. <li><a href="../bugs.html">Report a Bug</a></li>
  111. <li>
  112. <a href="https://github.com/python/cpython/blob/main/Doc/library/math.rst"
  113. rel="nofollow">Show Source
  114. </a>
  115. </li>
  116. </ul>
  117. </div>
  118. </nav>
  119. </div>
  120. </div>
  121. <div class="related" role="navigation" aria-label="related navigation">
  122. <h3>Navigation</h3>
  123. <ul>
  124. <li class="right" style="margin-right: 10px">
  125. <a href="../genindex.html" title="General Index"
  126. accesskey="I">index</a></li>
  127. <li class="right" >
  128. <a href="../py-modindex.html" title="Python Module Index"
  129. >modules</a> |</li>
  130. <li class="right" >
  131. <a href="cmath.html" title="cmath — Mathematical functions for complex numbers"
  132. accesskey="N">next</a> |</li>
  133. <li class="right" >
  134. <a href="numbers.html" title="numbers — Numeric abstract base classes"
  135. accesskey="P">previous</a> |</li>
  136. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  137. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  138. <li class="switchers">
  139. <div class="language_switcher_placeholder"></div>
  140. <div class="version_switcher_placeholder"></div>
  141. </li>
  142. <li>
  143. </li>
  144. <li id="cpython-language-and-version">
  145. <a href="../index.html">3.12.3 Documentation</a> &#187;
  146. </li>
  147. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  148. <li class="nav-item nav-item-2"><a href="numeric.html" accesskey="U">Numeric and Mathematical Modules</a> &#187;</li>
  149. <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>
  150. <li class="right">
  151. <div class="inline-search" role="search">
  152. <form class="inline-search" action="../search.html" method="get">
  153. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  154. <input type="submit" value="Go" />
  155. </form>
  156. </div>
  157. |
  158. </li>
  159. <li class="right">
  160. <label class="theme-selector-label">
  161. Theme
  162. <select class="theme-selector" oninput="activateTheme(this.value)">
  163. <option value="auto" selected>Auto</option>
  164. <option value="light">Light</option>
  165. <option value="dark">Dark</option>
  166. </select>
  167. </label> |</li>
  168. </ul>
  169. </div>
  170. <div class="document">
  171. <div class="documentwrapper">
  172. <div class="bodywrapper">
  173. <div class="body" role="main">
  174. <section id="module-math">
  175. <span id="math-mathematical-functions"></span><h1><a class="reference internal" href="#module-math" title="math: Mathematical functions (sin() etc.)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">math</span></code></a> — Mathematical functions<a class="headerlink" href="#module-math" title="Link to this heading">¶</a></h1>
  176. <hr class="docutils" />
  177. <p>This module provides access to the mathematical functions defined by the C
  178. standard.</p>
  179. <p>These functions cannot be used with complex numbers; use the functions of the
  180. same 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 complex
  181. numbers. The distinction between functions which support complex numbers and
  182. those which don’t is made since most users do not want to learn quite as much
  183. mathematics as required to understand complex numbers. Receiving an exception
  184. instead of a complex result allows earlier detection of the unexpected complex
  185. number used as a parameter, so that the programmer can determine how and why it
  186. was generated in the first place.</p>
  187. <p>The following functions are provided by this module. Except when explicitly
  188. noted otherwise, all return values are floats.</p>
  189. <section id="number-theoretic-and-representation-functions">
  190. <h2>Number-theoretic and representation functions<a class="headerlink" href="#number-theoretic-and-representation-functions" title="Link to this heading">¶</a></h2>
  191. <dl class="py function">
  192. <dt class="sig sig-object py" id="math.ceil">
  193. <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>
  194. <dd><p>Return the ceiling of <em>x</em>, the smallest integer greater than or equal to <em>x</em>.
  195. If <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>,
  196. 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>
  197. </dd></dl>
  198. <dl class="py function">
  199. <dt class="sig sig-object py" id="math.comb">
  200. <span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">comb</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">k</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#math.comb" title="Link to this definition">¶</a></dt>
  201. <dd><p>Return the number of ways to choose <em>k</em> items from <em>n</em> items without repetition
  202. and without order.</p>
  203. <p>Evaluates to <code class="docutils literal notranslate"><span class="pre">n!</span> <span class="pre">/</span> <span class="pre">(k!</span> <span class="pre">*</span> <span class="pre">(n</span> <span class="pre">-</span> <span class="pre">k)!)</span></code> when <code class="docutils literal notranslate"><span class="pre">k</span> <span class="pre">&lt;=</span> <span class="pre">n</span></code> and evaluates
  204. to zero when <code class="docutils literal notranslate"><span class="pre">k</span> <span class="pre">&gt;</span> <span class="pre">n</span></code>.</p>
  205. <p>Also called the binomial coefficient because it is equivalent
  206. to the coefficient of k-th term in polynomial expansion of
  207. <code class="docutils literal notranslate"><span class="pre">(1</span> <span class="pre">+</span> <span class="pre">x)ⁿ</span></code>.</p>
  208. <p>Raises <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> if either of the arguments are not integers.
  209. 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 either of the arguments are negative.</p>
  210. <div class="versionadded">
  211. <p><span class="versionmodified added">New in version 3.8.</span></p>
  212. </div>
  213. </dd></dl>
  214. <dl class="py function">
  215. <dt class="sig sig-object py" id="math.copysign">
  216. <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>
  217. <dd><p>Return a float with the magnitude (absolute value) of <em>x</em> but the sign of
  218. <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>
  219. returns <em>-1.0</em>.</p>
  220. </dd></dl>
  221. <dl class="py function">
  222. <dt class="sig sig-object py" id="math.fabs">
  223. <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>
  224. <dd><p>Return the absolute value of <em>x</em>.</p>
  225. </dd></dl>
  226. <dl class="py function">
  227. <dt class="sig sig-object py" id="math.factorial">
  228. <span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">factorial</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#math.factorial" title="Link to this definition">¶</a></dt>
  229. <dd><p>Return <em>n</em> factorial as an integer. 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 <em>n</em> is not integral or
  230. is negative.</p>
  231. <div class="deprecated">
  232. <p><span class="versionmodified deprecated">Deprecated since version 3.9: </span>Accepting floats with integral values (like <code class="docutils literal notranslate"><span class="pre">5.0</span></code>) is deprecated.</p>
  233. </div>
  234. </dd></dl>
  235. <dl class="py function">
  236. <dt class="sig sig-object py" id="math.floor">
  237. <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>
  238. <dd><p>Return the floor of <em>x</em>, the largest integer less than or equal to <em>x</em>. If
  239. <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>, which
  240. 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>
  241. </dd></dl>
  242. <dl class="py function">
  243. <dt class="sig sig-object py" id="math.fmod">
  244. <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>
  245. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">fmod(x,</span> <span class="pre">y)</span></code>, as defined by the platform C library. Note that the
  246. Python 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 C
  247. standard is that <code class="docutils literal notranslate"><span class="pre">fmod(x,</span> <span class="pre">y)</span></code> be exactly (mathematically; to infinite
  248. precision) 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 has
  249. the 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>
  250. returns a result with the sign of <em>y</em> instead, and may not be exactly computable
  251. for 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>, but
  252. the 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 be
  253. represented exactly as a float, and rounds to the surprising <code class="docutils literal notranslate"><span class="pre">1e100</span></code>. For
  254. this reason, function <a class="reference internal" href="#math.fmod" title="math.fmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">fmod()</span></code></a> is generally preferred when working with
  255. floats, 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>
  256. </dd></dl>
  257. <dl class="py function">
  258. <dt class="sig sig-object py" id="math.frexp">
  259. <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>
  260. <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 float
  261. and <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,
  262. returns <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">&lt;=</span> <span class="pre">abs(m)</span> <span class="pre">&lt;</span> <span class="pre">1</span></code>. This is used to “pick
  263. apart” the internal representation of a float in a portable way.</p>
  264. </dd></dl>
  265. <dl class="py function">
  266. <dt class="sig sig-object py" id="math.fsum">
  267. <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>
  268. <dd><p>Return an accurate floating point sum of values in the iterable. Avoids
  269. loss of precision by tracking multiple intermediate partial sums.</p>
  270. <p>The algorithm’s accuracy depends on IEEE-754 arithmetic guarantees and the
  271. typical case where the rounding mode is half-even. On some non-Windows
  272. builds, the underlying C library uses extended precision addition and may
  273. occasionally double-round an intermediate sum causing it to be off in its
  274. least significant bit.</p>
  275. <p>For further discussion and two alternative approaches, see the <a class="reference external" href="https://code.activestate.com/recipes/393090/">ASPN cookbook
  276. recipes for accurate floating point summation</a>.</p>
  277. </dd></dl>
  278. <dl class="py function">
  279. <dt class="sig sig-object py" id="math.gcd">
  280. <span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">gcd</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">integers</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#math.gcd" title="Link to this definition">¶</a></dt>
  281. <dd><p>Return the greatest common divisor of the specified integer arguments.
  282. If any of the arguments is nonzero, then the returned value is the largest
  283. positive integer that is a divisor of all arguments. If all arguments
  284. are zero, then the returned value is <code class="docutils literal notranslate"><span class="pre">0</span></code>. <code class="docutils literal notranslate"><span class="pre">gcd()</span></code> without arguments
  285. returns <code class="docutils literal notranslate"><span class="pre">0</span></code>.</p>
  286. <div class="versionadded">
  287. <p><span class="versionmodified added">New in version 3.5.</span></p>
  288. </div>
  289. <div class="versionchanged">
  290. <p><span class="versionmodified changed">Changed in version 3.9: </span>Added support for an arbitrary number of arguments. Formerly, only two
  291. arguments were supported.</p>
  292. </div>
  293. </dd></dl>
  294. <dl class="py function">
  295. <dt class="sig sig-object py" id="math.isclose">
  296. <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="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">rel_tol</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1e-09</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">abs_tol</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0.0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#math.isclose" title="Link to this definition">¶</a></dt>
  297. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the values <em>a</em> and <em>b</em> are close to each other and
  298. <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>
  299. <p>Whether or not two values are considered close is determined according to
  300. given absolute and relative tolerances.</p>
  301. <p><em>rel_tol</em> is the relative tolerance – it is the maximum allowed difference
  302. between <em>a</em> and <em>b</em>, relative to the larger absolute value of <em>a</em> or <em>b</em>.
  303. For example, to set a tolerance of 5%, pass <code class="docutils literal notranslate"><span class="pre">rel_tol=0.05</span></code>. The default
  304. tolerance is <code class="docutils literal notranslate"><span class="pre">1e-09</span></code>, which assures that the two values are the same
  305. within about 9 decimal digits. <em>rel_tol</em> must be greater than zero.</p>
  306. <p><em>abs_tol</em> is the minimum absolute tolerance – useful for comparisons near
  307. zero. <em>abs_tol</em> must be at least zero.</p>
  308. <p>If no errors occur, the result will be:
  309. <code class="docutils literal notranslate"><span class="pre">abs(a-b)</span> <span class="pre">&lt;=</span> <span class="pre">max(rel_tol</span> <span class="pre">*</span> <span class="pre">max(abs(a),</span> <span class="pre">abs(b)),</span> <span class="pre">abs_tol)</span></code>.</p>
  310. <p>The IEEE 754 special values of <code class="docutils literal notranslate"><span class="pre">NaN</span></code>, <code class="docutils literal notranslate"><span class="pre">inf</span></code>, and <code class="docutils literal notranslate"><span class="pre">-inf</span></code> will be
  311. handled according to IEEE rules. Specifically, <code class="docutils literal notranslate"><span class="pre">NaN</span></code> is not considered
  312. close to any other value, including <code class="docutils literal notranslate"><span class="pre">NaN</span></code>. <code class="docutils literal notranslate"><span class="pre">inf</span></code> and <code class="docutils literal notranslate"><span class="pre">-inf</span></code> are only
  313. considered close to themselves.</p>
  314. <div class="versionadded">
  315. <p><span class="versionmodified added">New in version 3.5.</span></p>
  316. </div>
  317. <div class="admonition seealso">
  318. <p class="admonition-title">See also</p>
  319. <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>
  320. </div>
  321. </dd></dl>
  322. <dl class="py function">
  323. <dt class="sig sig-object py" id="math.isfinite">
  324. <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>
  325. <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, and
  326. <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>
  327. <div class="versionadded">
  328. <p><span class="versionmodified added">New in version 3.2.</span></p>
  329. </div>
  330. </dd></dl>
  331. <dl class="py function">
  332. <dt class="sig sig-object py" id="math.isinf">
  333. <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>
  334. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if <em>x</em> is a positive or negative infinity, and
  335. <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>
  336. </dd></dl>
  337. <dl class="py function">
  338. <dt class="sig sig-object py" id="math.isnan">
  339. <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>
  340. <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>
  341. </dd></dl>
  342. <dl class="py function">
  343. <dt class="sig sig-object py" id="math.isqrt">
  344. <span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">isqrt</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#math.isqrt" title="Link to this definition">¶</a></dt>
  345. <dd><p>Return the integer square root of the nonnegative integer <em>n</em>. This is the
  346. floor of the exact square root of <em>n</em>, or equivalently the greatest integer
  347. <em>a</em> such that <em>a</em>² ≤ <em>n</em>.</p>
  348. <p>For some applications, it may be more convenient to have the least integer
  349. <em>a</em> such that <em>n</em> ≤ <em>a</em>², or in other words the ceiling of
  350. the exact square root of <em>n</em>. For positive <em>n</em>, this can be computed using
  351. <code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">=</span> <span class="pre">1</span> <span class="pre">+</span> <span class="pre">isqrt(n</span> <span class="pre">-</span> <span class="pre">1)</span></code>.</p>
  352. <div class="versionadded">
  353. <p><span class="versionmodified added">New in version 3.8.</span></p>
  354. </div>
  355. </dd></dl>
  356. <dl class="py function">
  357. <dt class="sig sig-object py" id="math.lcm">
  358. <span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">lcm</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">integers</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#math.lcm" title="Link to this definition">¶</a></dt>
  359. <dd><p>Return the least common multiple of the specified integer arguments.
  360. If all arguments are nonzero, then the returned value is the smallest
  361. positive integer that is a multiple of all arguments. If any of the arguments
  362. is zero, then the returned value is <code class="docutils literal notranslate"><span class="pre">0</span></code>. <code class="docutils literal notranslate"><span class="pre">lcm()</span></code> without arguments
  363. returns <code class="docutils literal notranslate"><span class="pre">1</span></code>.</p>
  364. <div class="versionadded">
  365. <p><span class="versionmodified added">New in version 3.9.</span></p>
  366. </div>
  367. </dd></dl>
  368. <dl class="py function">
  369. <dt class="sig sig-object py" id="math.ldexp">
  370. <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>
  371. <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 function
  372. <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>
  373. </dd></dl>
  374. <dl class="py function">
  375. <dt class="sig sig-object py" id="math.modf">
  376. <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>
  377. <dd><p>Return the fractional and integer parts of <em>x</em>. Both results carry the sign
  378. of <em>x</em> and are floats.</p>
  379. </dd></dl>
  380. <dl class="py function">
  381. <dt class="sig sig-object py" id="math.nextafter">
  382. <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>
  383. <dd><p>Return the floating-point value <em>steps</em> steps after <em>x</em> towards <em>y</em>.</p>
  384. <p>If <em>x</em> is equal to <em>y</em>, return <em>y</em>, unless <em>steps</em> is zero.</p>
  385. <p>Examples:</p>
  386. <ul class="simple">
  387. <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>
  388. <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>
  389. <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>
  390. <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>
  391. </ul>
  392. <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>
  393. <div class="versionadded">
  394. <p><span class="versionmodified added">New in version 3.9.</span></p>
  395. </div>
  396. <div class="versionchanged">
  397. <p><span class="versionmodified changed">Changed in version 3.12: </span>Added the <em>steps</em> argument.</p>
  398. </div>
  399. </dd></dl>
  400. <dl class="py function">
  401. <dt class="sig sig-object py" id="math.perm">
  402. <span class="sig-prename descclassname"><span class="pre">math.</span></span><span class="sig-name descname"><span class="pre">perm</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">k</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#math.perm" title="Link to this definition">¶</a></dt>
  403. <dd><p>Return the number of ways to choose <em>k</em> items from <em>n</em> items
  404. without repetition and with order.</p>
  405. <p>Evaluates to <code class="docutils literal notranslate"><span class="pre">n!</span> <span class="pre">/</span> <span class="pre">(n</span> <span class="pre">-</span> <span class="pre">k)!</span></code> when <code class="docutils literal notranslate"><span class="pre">k</span> <span class="pre">&lt;=</span> <span class="pre">n</span></code> and evaluates
  406. to zero when <code class="docutils literal notranslate"><span class="pre">k</span> <span class="pre">&gt;</span> <span class="pre">n</span></code>.</p>
  407. <p>If <em>k</em> is not specified or is None, then <em>k</em> defaults to <em>n</em>
  408. and the function returns <code class="docutils literal notranslate"><span class="pre">n!</span></code>.</p>
  409. <p>Raises <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> if either of the arguments are not integers.
  410. 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 either of the arguments are negative.</p>
  411. <div class="versionadded">
  412. <p><span class="versionmodified added">New in version 3.8.</span></p>
  413. </div>
  414. </dd></dl>
  415. <dl class="py function">
  416. <dt class="sig sig-object py" id="math.prod">
  417. <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="o"><span class="pre">*</span></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>
  418. <dd><p>Calculate the product of all the elements in the input <em>iterable</em>.
  419. The default <em>start</em> value for the product is <code class="docutils literal notranslate"><span class="pre">1</span></code>.</p>
  420. <p>When the iterable is empty, return the start value. This function is
  421. intended specifically for use with numeric values and may reject
  422. non-numeric types.</p>
  423. <div class="versionadded">
  424. <p><span class="versionmodified added">New in version 3.8.</span></p>
  425. </div>
  426. </dd></dl>
  427. <dl class="py function">
  428. <dt class="sig sig-object py" id="math.remainder">
  429. <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>
  430. <dd><p>Return the IEEE 754-style remainder of <em>x</em> with respect to <em>y</em>. For
  431. finite <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>,
  432. where <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>
  433. <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, the
  434. nearest <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>
  435. <span class="pre">y)</span></code> thus always satisfies <code class="docutils literal notranslate"><span class="pre">abs(r)</span> <span class="pre">&lt;=</span> <span class="pre">0.5</span> <span class="pre">*</span> <span class="pre">abs(y)</span></code>.</p>
  436. <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> is
  437. <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> and
  438. <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>.
  439. If the result of the remainder operation is zero, that zero will have
  440. the same sign as <em>x</em>.</p>
  441. <p>On platforms using IEEE 754 binary floating-point, the result of this
  442. operation is always exactly representable: no rounding error is introduced.</p>
  443. <div class="versionadded">
  444. <p><span class="versionmodified added">New in version 3.7.</span></p>
  445. </div>
  446. </dd></dl>
  447. <dl class="py function">
  448. <dt class="sig sig-object py" id="math.sumprod">
  449. <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>
  450. <dd><p>Return the sum of products of values from two iterables <em>p</em> and <em>q</em>.</p>
  451. <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>
  452. <p>Roughly equivalent to:</p>
  453. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">sum</span><span class="p">(</span><span class="n">itertools</span><span class="o">.</span><span class="n">starmap</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="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>
  454. </pre></div>
  455. </div>
  456. <p>For float and mixed int/float inputs, the intermediate products
  457. and sums are computed with extended precision.</p>
  458. <div class="versionadded">
  459. <p><span class="versionmodified added">New in version 3.12.</span></p>
  460. </div>
  461. </dd></dl>
  462. <dl class="py function">
  463. <dt class="sig sig-object py" id="math.trunc">
  464. <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>
  465. <dd><p>Return <em>x</em> with the fractional part
  466. removed, leaving the integer part. This rounds toward 0: <code class="docutils literal notranslate"><span class="pre">trunc()</span></code> is
  467. equivalent 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>
  468. for 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>
  469. </dd></dl>
  470. <dl class="py function">
  471. <dt class="sig sig-object py" id="math.ulp">
  472. <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>
  473. <dd><p>Return the value of the least significant bit of the float <em>x</em>:</p>
  474. <ul class="simple">
  475. <li><p>If <em>x</em> is a NaN (not a number), return <em>x</em>.</p></li>
  476. <li><p>If <em>x</em> is negative, return <code class="docutils literal notranslate"><span class="pre">ulp(-x)</span></code>.</p></li>
  477. <li><p>If <em>x</em> is a positive infinity, return <em>x</em>.</p></li>
  478. <li><p>If <em>x</em> is equal to zero, return the smallest positive
  479. <em>denormalized</em> representable float (smaller than the minimum positive
  480. <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>
  481. <li><p>If <em>x</em> is equal to the largest positive representable float,
  482. return the value of the least significant bit of <em>x</em>, such that the first
  483. float 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>
  484. <li><p>Otherwise (<em>x</em> is a positive finite number), return the value of the least
  485. significant bit of <em>x</em>, such that the first float bigger than <em>x</em>
  486. is <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">+</span> <span class="pre">ulp(x)</span></code>.</p></li>
  487. </ul>
  488. <p>ULP stands for “Unit in the Last Place”.</p>
  489. <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>
  490. <div class="versionadded">
  491. <p><span class="versionmodified added">New in version 3.9.</span></p>
  492. </div>
  493. </dd></dl>
  494. <p>Note that <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> 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> have a different call/return pattern
  495. than their C equivalents: they take a single argument and return a pair of
  496. values, rather than returning their second return value through an ‘output
  497. parameter’ (there is no such thing in Python).</p>
  498. <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>
  499. floating-point numbers of sufficiently large magnitude are exact integers.
  500. Python floats typically carry no more than 53 bits of precision (the same as the
  501. platform 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">&gt;=</span> <span class="pre">2**52</span></code>
  502. necessarily has no fractional bits.</p>
  503. </section>
  504. <section id="power-and-logarithmic-functions">
  505. <h2>Power and logarithmic functions<a class="headerlink" href="#power-and-logarithmic-functions" title="Link to this heading">¶</a></h2>
  506. <dl class="py function">
  507. <dt class="sig sig-object py" id="math.cbrt">
  508. <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>
  509. <dd><p>Return the cube root of <em>x</em>.</p>
  510. <div class="versionadded">
  511. <p><span class="versionmodified added">New in version 3.11.</span></p>
  512. </div>
  513. </dd></dl>
  514. <dl class="py function">
  515. <dt class="sig sig-object py" id="math.exp">
  516. <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>
  517. <dd><p>Return <em>e</em> raised to the power <em>x</em>, where <em>e</em> = 2.718281… is the base
  518. of 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>
  519. or <code class="docutils literal notranslate"><span class="pre">pow(math.e,</span> <span class="pre">x)</span></code>.</p>
  520. </dd></dl>
  521. <dl class="py function">
  522. <dt class="sig sig-object py" id="math.exp2">
  523. <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>
  524. <dd><p>Return <em>2</em> raised to the power <em>x</em>.</p>
  525. <div class="versionadded">
  526. <p><span class="versionmodified added">New in version 3.11.</span></p>
  527. </div>
  528. </dd></dl>
  529. <dl class="py function">
  530. <dt class="sig sig-object py" id="math.expm1">
  531. <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>
  532. <dd><p>Return <em>e</em> raised to the power <em>x</em>, minus 1. Here <em>e</em> is the base of natural
  533. logarithms. 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>
  534. can result in a <a class="reference external" href="https://en.wikipedia.org/wiki/Loss_of_significance">significant loss of precision</a>; the <a class="reference internal" href="#math.expm1" title="math.expm1"><code class="xref py py-func docutils literal notranslate"><span class="pre">expm1()</span></code></a>
  535. function provides a way to compute this quantity to full precision:</p>
  536. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">math</span> <span class="kn">import</span> <span class="n">exp</span><span class="p">,</span> <span class="n">expm1</span>
  537. <span class="gp">&gt;&gt;&gt; </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>
  538. <span class="go">1.0000050000069649e-05</span>
  539. <span class="gp">&gt;&gt;&gt; </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>
  540. <span class="go">1.0000050000166668e-05</span>
  541. </pre></div>
  542. </div>
  543. <div class="versionadded">
  544. <p><span class="versionmodified added">New in version 3.2.</span></p>
  545. </div>
  546. </dd></dl>
  547. <dl class="py function">
  548. <dt class="sig sig-object py" id="math.log">
  549. <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>
  550. <dd><p>With one argument, return the natural logarithm of <em>x</em> (to base <em>e</em>).</p>
  551. <p>With two arguments, return the logarithm of <em>x</em> to the given <em>base</em>,
  552. calculated as <code class="docutils literal notranslate"><span class="pre">log(x)/log(base)</span></code>.</p>
  553. </dd></dl>
  554. <dl class="py function">
  555. <dt class="sig sig-object py" id="math.log1p">
  556. <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>
  557. <dd><p>Return the natural logarithm of <em>1+x</em> (base <em>e</em>). The
  558. result is calculated in a way which is accurate for <em>x</em> near zero.</p>
  559. </dd></dl>
  560. <dl class="py function">
  561. <dt class="sig sig-object py" id="math.log2">
  562. <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>
  563. <dd><p>Return the base-2 logarithm of <em>x</em>. This is usually more accurate than
  564. <code class="docutils literal notranslate"><span class="pre">log(x,</span> <span class="pre">2)</span></code>.</p>
  565. <div class="versionadded">
  566. <p><span class="versionmodified added">New in version 3.3.</span></p>
  567. </div>
  568. <div class="admonition seealso">
  569. <p class="admonition-title">See also</p>
  570. <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 represent
  571. an integer in binary, excluding the sign and leading zeros.</p>
  572. </div>
  573. </dd></dl>
  574. <dl class="py function">
  575. <dt class="sig sig-object py" id="math.log10">
  576. <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>
  577. <dd><p>Return the base-10 logarithm of <em>x</em>. This is usually more accurate
  578. than <code class="docutils literal notranslate"><span class="pre">log(x,</span> <span class="pre">10)</span></code>.</p>
  579. </dd></dl>
  580. <dl class="py function">
  581. <dt class="sig sig-object py" id="math.pow">
  582. <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>
  583. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">x</span></code> raised to the power <code class="docutils literal notranslate"><span class="pre">y</span></code>. Exceptional cases follow
  584. the IEEE 754 standard as far as possible. In particular,
  585. <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>, even
  586. when <code class="docutils literal notranslate"><span class="pre">x</span></code> is a zero or a NaN. If both <code class="docutils literal notranslate"><span class="pre">x</span></code> and <code class="docutils literal notranslate"><span class="pre">y</span></code> are finite,
  587. <code class="docutils literal notranslate"><span class="pre">x</span></code> is negative, and <code class="docutils literal notranslate"><span class="pre">y</span></code> is not an integer then <code class="docutils literal notranslate"><span class="pre">pow(x,</span> <span class="pre">y)</span></code>
  588. is 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>
  589. <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 both
  590. its 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-in
  591. <a class="reference internal" href="functions.html#pow" title="pow"><code class="xref py py-func docutils literal notranslate"><span class="pre">pow()</span></code></a> function for computing exact integer powers.</p>
  592. <div class="versionchanged">
  593. <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> were
  594. changed 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>,
  595. for consistency with IEEE 754.</p>
  596. </div>
  597. </dd></dl>
  598. <dl class="py function">
  599. <dt class="sig sig-object py" id="math.sqrt">
  600. <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>
  601. <dd><p>Return the square root of <em>x</em>.</p>
  602. </dd></dl>
  603. </section>
  604. <section id="trigonometric-functions">
  605. <h2>Trigonometric functions<a class="headerlink" href="#trigonometric-functions" title="Link to this heading">¶</a></h2>
  606. <dl class="py function">
  607. <dt class="sig sig-object py" id="math.acos">
  608. <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>
  609. <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> and
  610. <code class="docutils literal notranslate"><span class="pre">pi</span></code>.</p>
  611. </dd></dl>
  612. <dl class="py function">
  613. <dt class="sig sig-object py" id="math.asin">
  614. <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>
  615. <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> and
  616. <code class="docutils literal notranslate"><span class="pre">pi/2</span></code>.</p>
  617. </dd></dl>
  618. <dl class="py function">
  619. <dt class="sig sig-object py" id="math.atan">
  620. <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>
  621. <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> and
  622. <code class="docutils literal notranslate"><span class="pre">pi/2</span></code>.</p>
  623. </dd></dl>
  624. <dl class="py function">
  625. <dt class="sig sig-object py" id="math.atan2">
  626. <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>
  627. <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>.
  628. The 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 angle
  629. with the positive X axis. The point of <a class="reference internal" href="#math.atan2" title="math.atan2"><code class="xref py py-func docutils literal notranslate"><span class="pre">atan2()</span></code></a> is that the signs of both
  630. inputs are known to it, so it can compute the correct quadrant for the angle.
  631. For 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>
  632. <span class="pre">-1)</span></code> is <code class="docutils literal notranslate"><span class="pre">-3*pi/4</span></code>.</p>
  633. </dd></dl>
  634. <dl class="py function">
  635. <dt class="sig sig-object py" id="math.cos">
  636. <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>
  637. <dd><p>Return the cosine of <em>x</em> radians.</p>
  638. </dd></dl>
  639. <dl class="py function">
  640. <dt class="sig sig-object py" id="math.dist">
  641. <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>
  642. <dd><p>Return the Euclidean distance between two points <em>p</em> and <em>q</em>, each
  643. given as a sequence (or iterable) of coordinates. The two points
  644. must have the same dimension.</p>
  645. <p>Roughly equivalent to:</p>
  646. <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>
  647. </pre></div>
  648. </div>
  649. <div class="versionadded">
  650. <p><span class="versionmodified added">New in version 3.8.</span></p>
  651. </div>
  652. </dd></dl>
  653. <dl class="py function">
  654. <dt class="sig sig-object py" id="math.hypot">
  655. <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>
  656. <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>.
  657. This is the length of the vector from the origin to the point
  658. given by the coordinates.</p>
  659. <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 computing
  660. the hypotenuse of a right triangle using the Pythagorean theorem,
  661. <code class="docutils literal notranslate"><span class="pre">sqrt(x*x</span> <span class="pre">+</span> <span class="pre">y*y)</span></code>.</p>
  662. <div class="versionchanged">
  663. <p><span class="versionmodified changed">Changed in version 3.8: </span>Added support for n-dimensional points. Formerly, only the two
  664. dimensional case was supported.</p>
  665. </div>
  666. <div class="versionchanged">
  667. <p><span class="versionmodified changed">Changed in version 3.10: </span>Improved the algorithm’s accuracy so that the maximum error is
  668. under 1 ulp (unit in the last place). More typically, the result
  669. is almost always correctly rounded to within 1/2 ulp.</p>
  670. </div>
  671. </dd></dl>
  672. <dl class="py function">
  673. <dt class="sig sig-object py" id="math.sin">
  674. <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>
  675. <dd><p>Return the sine of <em>x</em> radians.</p>
  676. </dd></dl>
  677. <dl class="py function">
  678. <dt class="sig sig-object py" id="math.tan">
  679. <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>
  680. <dd><p>Return the tangent of <em>x</em> radians.</p>
  681. </dd></dl>
  682. </section>
  683. <section id="angular-conversion">
  684. <h2>Angular conversion<a class="headerlink" href="#angular-conversion" title="Link to this heading">¶</a></h2>
  685. <dl class="py function">
  686. <dt class="sig sig-object py" id="math.degrees">
  687. <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>
  688. <dd><p>Convert angle <em>x</em> from radians to degrees.</p>
  689. </dd></dl>
  690. <dl class="py function">
  691. <dt class="sig sig-object py" id="math.radians">
  692. <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>
  693. <dd><p>Convert angle <em>x</em> from degrees to radians.</p>
  694. </dd></dl>
  695. </section>
  696. <section id="hyperbolic-functions">
  697. <h2>Hyperbolic functions<a class="headerlink" href="#hyperbolic-functions" title="Link to this heading">¶</a></h2>
  698. <p><a class="reference external" href="https://en.wikipedia.org/wiki/Hyperbolic_functions">Hyperbolic functions</a>
  699. are analogs of trigonometric functions that are based on hyperbolas
  700. instead of circles.</p>
  701. <dl class="py function">
  702. <dt class="sig sig-object py" id="math.acosh">
  703. <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>
  704. <dd><p>Return the inverse hyperbolic cosine of <em>x</em>.</p>
  705. </dd></dl>
  706. <dl class="py function">
  707. <dt class="sig sig-object py" id="math.asinh">
  708. <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>
  709. <dd><p>Return the inverse hyperbolic sine of <em>x</em>.</p>
  710. </dd></dl>
  711. <dl class="py function">
  712. <dt class="sig sig-object py" id="math.atanh">
  713. <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>
  714. <dd><p>Return the inverse hyperbolic tangent of <em>x</em>.</p>
  715. </dd></dl>
  716. <dl class="py function">
  717. <dt class="sig sig-object py" id="math.cosh">
  718. <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>
  719. <dd><p>Return the hyperbolic cosine of <em>x</em>.</p>
  720. </dd></dl>
  721. <dl class="py function">
  722. <dt class="sig sig-object py" id="math.sinh">
  723. <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>
  724. <dd><p>Return the hyperbolic sine of <em>x</em>.</p>
  725. </dd></dl>
  726. <dl class="py function">
  727. <dt class="sig sig-object py" id="math.tanh">
  728. <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>
  729. <dd><p>Return the hyperbolic tangent of <em>x</em>.</p>
  730. </dd></dl>
  731. </section>
  732. <section id="special-functions">
  733. <h2>Special functions<a class="headerlink" href="#special-functions" title="Link to this heading">¶</a></h2>
  734. <dl class="py function">
  735. <dt class="sig sig-object py" id="math.erf">
  736. <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>
  737. <dd><p>Return the <a class="reference external" href="https://en.wikipedia.org/wiki/Error_function">error function</a> at
  738. <em>x</em>.</p>
  739. <p>The <a class="reference internal" href="#math.erf" title="math.erf"><code class="xref py py-func docutils literal notranslate"><span class="pre">erf()</span></code></a> function can be used to compute traditional statistical
  740. functions such as the <a class="reference external" href="https://en.wikipedia.org/wiki/Cumulative_distribution_function">cumulative standard normal distribution</a>:</p>
  741. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">phi</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
  742. <span class="s1">&#39;Cumulative distribution function for the standard normal distribution&#39;</span>
  743. <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>
  744. </pre></div>
  745. </div>
  746. <div class="versionadded">
  747. <p><span class="versionmodified added">New in version 3.2.</span></p>
  748. </div>
  749. </dd></dl>
  750. <dl class="py function">
  751. <dt class="sig sig-object py" id="math.erfc">
  752. <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>
  753. <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 error
  754. function</a> is defined as
  755. <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 subtraction
  756. from one would cause a <a class="reference external" href="https://en.wikipedia.org/wiki/Loss_of_significance">loss of significance</a>.</p>
  757. <div class="versionadded">
  758. <p><span class="versionmodified added">New in version 3.2.</span></p>
  759. </div>
  760. </dd></dl>
  761. <dl class="py function">
  762. <dt class="sig sig-object py" id="math.gamma">
  763. <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>
  764. <dd><p>Return the <a class="reference external" href="https://en.wikipedia.org/wiki/Gamma_function">Gamma function</a> at
  765. <em>x</em>.</p>
  766. <div class="versionadded">
  767. <p><span class="versionmodified added">New in version 3.2.</span></p>
  768. </div>
  769. </dd></dl>
  770. <dl class="py function">
  771. <dt class="sig sig-object py" id="math.lgamma">
  772. <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>
  773. <dd><p>Return the natural logarithm of the absolute value of the Gamma
  774. function at <em>x</em>.</p>
  775. <div class="versionadded">
  776. <p><span class="versionmodified added">New in version 3.2.</span></p>
  777. </div>
  778. </dd></dl>
  779. </section>
  780. <section id="constants">
  781. <h2>Constants<a class="headerlink" href="#constants" title="Link to this heading">¶</a></h2>
  782. <dl class="py data">
  783. <dt class="sig sig-object py" id="math.pi">
  784. <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>
  785. <dd><p>The mathematical constant <em>π</em> = 3.141592…, to available precision.</p>
  786. </dd></dl>
  787. <dl class="py data">
  788. <dt class="sig sig-object py" id="math.e">
  789. <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>
  790. <dd><p>The mathematical constant <em>e</em> = 2.718281…, to available precision.</p>
  791. </dd></dl>
  792. <dl class="py data">
  793. <dt class="sig sig-object py" id="math.tau">
  794. <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>
  795. <dd><p>The mathematical constant <em>τ</em> = 6.283185…, to available precision.
  796. Tau is a circle constant equal to 2<em>π</em>, the ratio of a circle’s circumference to
  797. its radius. To learn more about Tau, check out Vi Hart’s video <a class="reference external" href="https://www.youtube.com/watch?v=jG7vhMMXagQ">Pi is (still)
  798. Wrong</a>, and start celebrating
  799. <a class="reference external" href="https://tauday.com/">Tau day</a> by eating twice as much pie!</p>
  800. <div class="versionadded">
  801. <p><span class="versionmodified added">New in version 3.6.</span></p>
  802. </div>
  803. </dd></dl>
  804. <dl class="py data">
  805. <dt class="sig sig-object py" id="math.inf">
  806. <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>
  807. <dd><p>A floating-point positive infinity. (For negative infinity, use
  808. <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>
  809. <div class="versionadded">
  810. <p><span class="versionmodified added">New in version 3.5.</span></p>
  811. </div>
  812. </dd></dl>
  813. <dl class="py data">
  814. <dt class="sig sig-object py" id="math.nan">
  815. <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>
  816. <dd><p>A floating-point “not a number” (NaN) value. Equivalent to the output of
  817. <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> are
  818. not considered to equal to any other numeric value, including themselves. To check
  819. whether 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 test
  820. for 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>.
  821. Example:</p>
  822. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">math</span>
  823. <span class="gp">&gt;&gt;&gt; </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>
  824. <span class="go">False</span>
  825. <span class="gp">&gt;&gt;&gt; </span><span class="nb">float</span><span class="p">(</span><span class="s1">&#39;nan&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="nb">float</span><span class="p">(</span><span class="s1">&#39;nan&#39;</span><span class="p">)</span>
  826. <span class="go">False</span>
  827. <span class="gp">&gt;&gt;&gt; </span><span class="n">math</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">nan</span><span class="p">)</span>
  828. <span class="go">True</span>
  829. <span class="gp">&gt;&gt;&gt; </span><span class="n">math</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="s1">&#39;nan&#39;</span><span class="p">))</span>
  830. <span class="go">True</span>
  831. </pre></div>
  832. </div>
  833. <div class="versionadded">
  834. <p><span class="versionmodified added">New in version 3.5.</span></p>
  835. </div>
  836. <div class="versionchanged">
  837. <p><span class="versionmodified changed">Changed in version 3.11: </span>It is now always available.</p>
  838. </div>
  839. </dd></dl>
  840. <div class="impl-detail compound">
  841. <p><strong>CPython implementation detail:</strong> The <a class="reference internal" href="#module-math" title="math: Mathematical functions (sin() etc.)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">math</span></code></a> module consists mostly of thin wrappers around the platform C
  842. math library functions. Behavior in exceptional cases follows Annex F of
  843. the C99 standard where appropriate. The current implementation will raise
  844. <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 invalid operations like <code class="docutils literal notranslate"><span class="pre">sqrt(-1.0)</span></code> or <code class="docutils literal notranslate"><span class="pre">log(0.0)</span></code>
  845. (where C99 Annex F recommends signaling invalid operation or divide-by-zero),
  846. and <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a> for results that overflow (for example,
  847. <code class="docutils literal notranslate"><span class="pre">exp(1000.0)</span></code>). A NaN will not be returned from any of the functions
  848. above unless one or more of the input arguments was a NaN; in that case,
  849. most functions will return a NaN, but (again following C99 Annex F) there
  850. are some exceptions to this rule, for example <code class="docutils literal notranslate"><span class="pre">pow(float('nan'),</span> <span class="pre">0.0)</span></code> or
  851. <code class="docutils literal notranslate"><span class="pre">hypot(float('nan'),</span> <span class="pre">float('inf'))</span></code>.</p>
  852. <p>Note that Python makes no effort to distinguish signaling NaNs from
  853. quiet NaNs, and behavior for signaling NaNs remains unspecified.
  854. Typical behavior is to treat all NaNs as though they were quiet.</p>
  855. </div>
  856. <div class="admonition seealso">
  857. <p class="admonition-title">See also</p>
  858. <dl class="simple">
  859. <dt>Module <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></dt><dd><p>Complex number versions of many of these functions.</p>
  860. </dd>
  861. </dl>
  862. </div>
  863. </section>
  864. </section>
  865. <div class="clearer"></div>
  866. </div>
  867. </div>
  868. </div>
  869. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  870. <div class="sphinxsidebarwrapper">
  871. <div>
  872. <h3><a href="../contents.html">Table of Contents</a></h3>
  873. <ul>
  874. <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>
  875. <li><a class="reference internal" href="#number-theoretic-and-representation-functions">Number-theoretic and representation functions</a></li>
  876. <li><a class="reference internal" href="#power-and-logarithmic-functions">Power and logarithmic functions</a></li>
  877. <li><a class="reference internal" href="#trigonometric-functions">Trigonometric functions</a></li>
  878. <li><a class="reference internal" href="#angular-conversion">Angular conversion</a></li>
  879. <li><a class="reference internal" href="#hyperbolic-functions">Hyperbolic functions</a></li>
  880. <li><a class="reference internal" href="#special-functions">Special functions</a></li>
  881. <li><a class="reference internal" href="#constants">Constants</a></li>
  882. </ul>
  883. </li>
  884. </ul>
  885. </div>
  886. <div>
  887. <h4>Previous topic</h4>
  888. <p class="topless"><a href="numbers.html"
  889. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">numbers</span></code> — Numeric abstract base classes</a></p>
  890. </div>
  891. <div>
  892. <h4>Next topic</h4>
  893. <p class="topless"><a href="cmath.html"
  894. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">cmath</span></code> — Mathematical functions for complex numbers</a></p>
  895. </div>
  896. <div role="note" aria-label="source link">
  897. <h3>This Page</h3>
  898. <ul class="this-page-menu">
  899. <li><a href="../bugs.html">Report a Bug</a></li>
  900. <li>
  901. <a href="https://github.com/python/cpython/blob/main/Doc/library/math.rst"
  902. rel="nofollow">Show Source
  903. </a>
  904. </li>
  905. </ul>
  906. </div>
  907. </div>
  908. <div id="sidebarbutton" title="Collapse sidebar">
  909. <span>«</span>
  910. </div>
  911. </div>
  912. <div class="clearer"></div>
  913. </div>
  914. <div class="related" role="navigation" aria-label="related navigation">
  915. <h3>Navigation</h3>
  916. <ul>
  917. <li class="right" style="margin-right: 10px">
  918. <a href="../genindex.html" title="General Index"
  919. >index</a></li>
  920. <li class="right" >
  921. <a href="../py-modindex.html" title="Python Module Index"
  922. >modules</a> |</li>
  923. <li class="right" >
  924. <a href="cmath.html" title="cmath — Mathematical functions for complex numbers"
  925. >next</a> |</li>
  926. <li class="right" >
  927. <a href="numbers.html" title="numbers — Numeric abstract base classes"
  928. >previous</a> |</li>
  929. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  930. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  931. <li class="switchers">
  932. <div class="language_switcher_placeholder"></div>
  933. <div class="version_switcher_placeholder"></div>
  934. </li>
  935. <li>
  936. </li>
  937. <li id="cpython-language-and-version">
  938. <a href="../index.html">3.12.3 Documentation</a> &#187;
  939. </li>
  940. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  941. <li class="nav-item nav-item-2"><a href="numeric.html" >Numeric and Mathematical Modules</a> &#187;</li>
  942. <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>
  943. <li class="right">
  944. <div class="inline-search" role="search">
  945. <form class="inline-search" action="../search.html" method="get">
  946. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  947. <input type="submit" value="Go" />
  948. </form>
  949. </div>
  950. |
  951. </li>
  952. <li class="right">
  953. <label class="theme-selector-label">
  954. Theme
  955. <select class="theme-selector" oninput="activateTheme(this.value)">
  956. <option value="auto" selected>Auto</option>
  957. <option value="light">Light</option>
  958. <option value="dark">Dark</option>
  959. </select>
  960. </label> |</li>
  961. </ul>
  962. </div>
  963. <div class="footer">
  964. &copy;
  965. <a href="../copyright.html">
  966. Copyright
  967. </a>
  968. 2001-2024, Python Software Foundation.
  969. <br />
  970. This page is licensed under the Python Software Foundation License Version 2.
  971. <br />
  972. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  973. <br />
  974. See <a href="/license.html">History and License</a> for more information.<br />
  975. <br />
  976. The Python Software Foundation is a non-profit corporation.
  977. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  978. <br />
  979. <br />
  980. Last updated on Apr 09, 2024 (13:47 UTC).
  981. <a href="/bugs.html">Found a bug</a>?
  982. <br />
  983. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  984. </div>
  985. </body>
  986. </html>
上海开阖软件有限公司 沪ICP备12045867号-1