float.html 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
  6. <meta property="og:title" content="Floating Point Objects" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/c-api/float.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Pack and Unpack functions: The pack and unpack functions provide an efficient platform-independent way to store floating-point values as byte strings. The Pack routines produce a bytes string from ..." />
  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="Pack and Unpack functions: The pack and unpack functions provide an efficient platform-independent way to store floating-point values as byte strings. The Pack routines produce a bytes string from ..." />
  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>Floating Point Objects &#8212; Python 3.12.0 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?digest=b37c26da2f7529d09fe70b41c4b2133fe4931a90" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css" />
  21. <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
  22. <script src="../_static/jquery.js"></script>
  23. <script src="../_static/underscore.js"></script>
  24. <script src="../_static/doctools.js"></script>
  25. <script src="../_static/sidebar.js"></script>
  26. <link rel="search" type="application/opensearchdescription+xml"
  27. title="Search within Python 3.12.0 documentation"
  28. href="../_static/opensearch.xml"/>
  29. <link rel="author" title="About these documents" href="../about.html" />
  30. <link rel="index" title="Index" href="../genindex.html" />
  31. <link rel="search" title="Search" href="../search.html" />
  32. <link rel="copyright" title="Copyright" href="../copyright.html" />
  33. <link rel="next" title="Complex Number Objects" href="complex.html" />
  34. <link rel="prev" title="Boolean Objects" href="bool.html" />
  35. <link rel="canonical" href="https://docs.python.org/3/c-api/float.html" />
  36. <style>
  37. @media only screen {
  38. table.full-width-table {
  39. width: 100%;
  40. }
  41. }
  42. </style>
  43. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  44. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  45. <script type="text/javascript" src="../_static/copybutton.js"></script>
  46. <script type="text/javascript" src="../_static/menu.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#">Floating Point Objects</a><ul>
  86. <li><a class="reference internal" href="#pack-and-unpack-functions">Pack and Unpack functions</a><ul>
  87. <li><a class="reference internal" href="#pack-functions">Pack functions</a></li>
  88. <li><a class="reference internal" href="#unpack-functions">Unpack functions</a></li>
  89. </ul>
  90. </li>
  91. </ul>
  92. </li>
  93. </ul>
  94. </div>
  95. <div>
  96. <h4>Previous topic</h4>
  97. <p class="topless"><a href="bool.html"
  98. title="previous chapter">Boolean Objects</a></p>
  99. </div>
  100. <div>
  101. <h4>Next topic</h4>
  102. <p class="topless"><a href="complex.html"
  103. title="next chapter">Complex Number Objects</a></p>
  104. </div>
  105. <div role="note" aria-label="source link">
  106. <h3>This Page</h3>
  107. <ul class="this-page-menu">
  108. <li><a href="../bugs.html">Report a Bug</a></li>
  109. <li>
  110. <a href="https://github.com/python/cpython/blob/main/Doc/c-api/float.rst"
  111. rel="nofollow">Show Source
  112. </a>
  113. </li>
  114. </ul>
  115. </div>
  116. </nav>
  117. </div>
  118. </div>
  119. <div class="related" role="navigation" aria-label="related navigation">
  120. <h3>Navigation</h3>
  121. <ul>
  122. <li class="right" style="margin-right: 10px">
  123. <a href="../genindex.html" title="General Index"
  124. accesskey="I">index</a></li>
  125. <li class="right" >
  126. <a href="../py-modindex.html" title="Python Module Index"
  127. >modules</a> |</li>
  128. <li class="right" >
  129. <a href="complex.html" title="Complex Number Objects"
  130. accesskey="N">next</a> |</li>
  131. <li class="right" >
  132. <a href="bool.html" title="Boolean Objects"
  133. accesskey="P">previous</a> |</li>
  134. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  135. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  136. <li class="switchers">
  137. <div class="language_switcher_placeholder"></div>
  138. <div class="version_switcher_placeholder"></div>
  139. </li>
  140. <li>
  141. </li>
  142. <li id="cpython-language-and-version">
  143. <a href="../index.html">3.12.0 Documentation</a> &#187;
  144. </li>
  145. <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &#187;</li>
  146. <li class="nav-item nav-item-2"><a href="concrete.html" accesskey="U">Concrete Objects Layer</a> &#187;</li>
  147. <li class="nav-item nav-item-this"><a href="">Floating Point Objects</a></li>
  148. <li class="right">
  149. <div class="inline-search" role="search">
  150. <form class="inline-search" action="../search.html" method="get">
  151. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  152. <input type="submit" value="Go" />
  153. </form>
  154. </div>
  155. |
  156. </li>
  157. <li class="right">
  158. <label class="theme-selector-label">
  159. Theme
  160. <select class="theme-selector" oninput="activateTheme(this.value)">
  161. <option value="auto" selected>Auto</option>
  162. <option value="light">Light</option>
  163. <option value="dark">Dark</option>
  164. </select>
  165. </label> |</li>
  166. </ul>
  167. </div>
  168. <div class="document">
  169. <div class="documentwrapper">
  170. <div class="bodywrapper">
  171. <div class="body" role="main">
  172. <section id="floating-point-objects">
  173. <span id="floatobjects"></span><h1>Floating Point Objects<a class="headerlink" href="#floating-point-objects" title="Permalink to this headline">¶</a></h1>
  174. <span class="target" id="index-0"></span><dl class="c type">
  175. <dt class="sig sig-object c" id="c.PyFloatObject">
  176. <span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyFloatObject</span></span></span><a class="headerlink" href="#c.PyFloatObject" title="Permalink to this definition">¶</a><br /></dt>
  177. <dd><p>This subtype of <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyObject</span></code></a> represents a Python floating point object.</p>
  178. </dd></dl>
  179. <dl class="c var">
  180. <dt class="sig sig-object c" id="c.PyFloat_Type">
  181. <a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject"><span class="n"><span class="pre">PyTypeObject</span></span></a><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyFloat_Type</span></span></span><a class="headerlink" href="#c.PyFloat_Type" title="Permalink to this definition">¶</a><br /></dt>
  182. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>This instance of <a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyTypeObject</span></code></a> represents the Python floating point
  183. type. This is the same object as <a class="reference internal" href="../library/functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a> in the Python layer.</p>
  184. </dd></dl>
  185. <dl class="c function">
  186. <dt class="sig sig-object c" id="c.PyFloat_Check">
  187. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyFloat_Check</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">p</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyFloat_Check" title="Permalink to this definition">¶</a><br /></dt>
  188. <dd><p>Return true if its argument is a <a class="reference internal" href="#c.PyFloatObject" title="PyFloatObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyFloatObject</span></code></a> or a subtype of
  189. <a class="reference internal" href="#c.PyFloatObject" title="PyFloatObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyFloatObject</span></code></a>. This function always succeeds.</p>
  190. </dd></dl>
  191. <dl class="c function">
  192. <dt class="sig sig-object c" id="c.PyFloat_CheckExact">
  193. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyFloat_CheckExact</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">p</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyFloat_CheckExact" title="Permalink to this definition">¶</a><br /></dt>
  194. <dd><p>Return true if its argument is a <a class="reference internal" href="#c.PyFloatObject" title="PyFloatObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyFloatObject</span></code></a>, but not a subtype of
  195. <a class="reference internal" href="#c.PyFloatObject" title="PyFloatObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyFloatObject</span></code></a>. This function always succeeds.</p>
  196. </dd></dl>
  197. <dl class="c function">
  198. <dt class="sig sig-object c" id="c.PyFloat_FromString">
  199. <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyFloat_FromString</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">str</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyFloat_FromString" title="Permalink to this definition">¶</a><br /></dt>
  200. <dd><em class="refcount">Return value: New reference.</em><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Create a <a class="reference internal" href="#c.PyFloatObject" title="PyFloatObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyFloatObject</span></code></a> object based on the string value in <em>str</em>, or
  201. <code class="docutils literal notranslate"><span class="pre">NULL</span></code> on failure.</p>
  202. </dd></dl>
  203. <dl class="c function">
  204. <dt class="sig sig-object c" id="c.PyFloat_FromDouble">
  205. <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyFloat_FromDouble</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">double</span></span><span class="w"> </span><span class="n"><span class="pre">v</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyFloat_FromDouble" title="Permalink to this definition">¶</a><br /></dt>
  206. <dd><em class="refcount">Return value: New reference.</em><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Create a <a class="reference internal" href="#c.PyFloatObject" title="PyFloatObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyFloatObject</span></code></a> object from <em>v</em>, or <code class="docutils literal notranslate"><span class="pre">NULL</span></code> on failure.</p>
  207. </dd></dl>
  208. <dl class="c function">
  209. <dt class="sig sig-object c" id="c.PyFloat_AsDouble">
  210. <span class="kt"><span class="pre">double</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyFloat_AsDouble</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">pyfloat</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyFloat_AsDouble" title="Permalink to this definition">¶</a><br /></dt>
  211. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Return a C <span class="c-expr sig sig-inline c"><span class="kt">double</span></span> representation of the contents of <em>pyfloat</em>. If
  212. <em>pyfloat</em> is not a Python floating point object but has a <a class="reference internal" href="../reference/datamodel.html#object.__float__" title="object.__float__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__float__()</span></code></a>
  213. method, this method will first be called to convert <em>pyfloat</em> into a float.
  214. If <code class="xref py py-meth docutils literal notranslate"><span class="pre">__float__()</span></code> is not defined then it falls back to <a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__index__()</span></code></a>.
  215. This method returns <code class="docutils literal notranslate"><span class="pre">-1.0</span></code> upon failure, so one should call
  216. <a class="reference internal" href="exceptions.html#c.PyErr_Occurred" title="PyErr_Occurred"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyErr_Occurred()</span></code></a> to check for errors.</p>
  217. <div class="versionchanged">
  218. <p><span class="versionmodified changed">Changed in version 3.8: </span>Use <a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__index__()</span></code></a> if available.</p>
  219. </div>
  220. </dd></dl>
  221. <dl class="c function">
  222. <dt class="sig sig-object c" id="c.PyFloat_AS_DOUBLE">
  223. <span class="kt"><span class="pre">double</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyFloat_AS_DOUBLE</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">pyfloat</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyFloat_AS_DOUBLE" title="Permalink to this definition">¶</a><br /></dt>
  224. <dd><p>Return a C <span class="c-expr sig sig-inline c"><span class="kt">double</span></span> representation of the contents of <em>pyfloat</em>, but
  225. without error checking.</p>
  226. </dd></dl>
  227. <dl class="c function">
  228. <dt class="sig sig-object c" id="c.PyFloat_GetInfo">
  229. <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyFloat_GetInfo</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">void</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyFloat_GetInfo" title="Permalink to this definition">¶</a><br /></dt>
  230. <dd><em class="refcount">Return value: New reference.</em><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Return a structseq instance which contains information about the
  231. precision, minimum and maximum values of a float. It’s a thin wrapper
  232. around the header file <code class="file docutils literal notranslate"><span class="pre">float.h</span></code>.</p>
  233. </dd></dl>
  234. <dl class="c function">
  235. <dt class="sig sig-object c" id="c.PyFloat_GetMax">
  236. <span class="kt"><span class="pre">double</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyFloat_GetMax</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyFloat_GetMax" title="Permalink to this definition">¶</a><br /></dt>
  237. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Return the maximum representable finite float <em>DBL_MAX</em> as C <span class="c-expr sig sig-inline c"><span class="kt">double</span></span>.</p>
  238. </dd></dl>
  239. <dl class="c function">
  240. <dt class="sig sig-object c" id="c.PyFloat_GetMin">
  241. <span class="kt"><span class="pre">double</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyFloat_GetMin</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyFloat_GetMin" title="Permalink to this definition">¶</a><br /></dt>
  242. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Return the minimum normalized positive float <em>DBL_MIN</em> as C <span class="c-expr sig sig-inline c"><span class="kt">double</span></span>.</p>
  243. </dd></dl>
  244. <section id="pack-and-unpack-functions">
  245. <h2>Pack and Unpack functions<a class="headerlink" href="#pack-and-unpack-functions" title="Permalink to this headline">¶</a></h2>
  246. <p>The pack and unpack functions provide an efficient platform-independent way to
  247. store floating-point values as byte strings. The Pack routines produce a bytes
  248. string from a C <span class="c-expr sig sig-inline c"><span class="kt">double</span></span>, and the Unpack routines produce a C
  249. <span class="c-expr sig sig-inline c"><span class="kt">double</span></span> from such a bytes string. The suffix (2, 4 or 8) specifies the
  250. number of bytes in the bytes string.</p>
  251. <p>On platforms that appear to use IEEE 754 formats these functions work by
  252. copying bits. On other platforms, the 2-byte format is identical to the IEEE
  253. 754 binary16 half-precision format, the 4-byte format (32-bit) is identical to
  254. the IEEE 754 binary32 single precision format, and the 8-byte format to the
  255. IEEE 754 binary64 double precision format, although the packing of INFs and
  256. NaNs (if such things exist on the platform) isn’t handled correctly, and
  257. attempting to unpack a bytes string containing an IEEE INF or NaN will raise an
  258. exception.</p>
  259. <p>On non-IEEE platforms with more precision, or larger dynamic range, than IEEE
  260. 754 supports, not all values can be packed; on non-IEEE platforms with less
  261. precision, or smaller dynamic range, not all values can be unpacked. What
  262. happens in such cases is partly accidental (alas).</p>
  263. <div class="versionadded">
  264. <p><span class="versionmodified added">New in version 3.11.</span></p>
  265. </div>
  266. <section id="pack-functions">
  267. <h3>Pack functions<a class="headerlink" href="#pack-functions" title="Permalink to this headline">¶</a></h3>
  268. <p>The pack routines write 2, 4 or 8 bytes, starting at <em>p</em>. <em>le</em> is an
  269. <span class="c-expr sig sig-inline c"><span class="kt">int</span></span> argument, non-zero if you want the bytes string in little-endian
  270. format (exponent last, at <code class="docutils literal notranslate"><span class="pre">p+1</span></code>, <code class="docutils literal notranslate"><span class="pre">p+3</span></code>, or <code class="docutils literal notranslate"><span class="pre">p+6</span></code> <code class="docutils literal notranslate"><span class="pre">p+7</span></code>), zero if you
  271. want big-endian format (exponent first, at <em>p</em>). The <code class="xref c c-macro docutils literal notranslate"><span class="pre">PY_BIG_ENDIAN</span></code>
  272. constant can be used to use the native endian: it is equal to <code class="docutils literal notranslate"><span class="pre">1</span></code> on big
  273. endian processor, or <code class="docutils literal notranslate"><span class="pre">0</span></code> on little endian processor.</p>
  274. <p>Return value: <code class="docutils literal notranslate"><span class="pre">0</span></code> if all is OK, <code class="docutils literal notranslate"><span class="pre">-1</span></code> if error (and an exception is set,
  275. most likely <a class="reference internal" href="../library/exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a>).</p>
  276. <p>There are two problems on non-IEEE platforms:</p>
  277. <ul class="simple">
  278. <li><p>What this does is undefined if <em>x</em> is a NaN or infinity.</p></li>
  279. <li><p><code class="docutils literal notranslate"><span class="pre">-0.0</span></code> and <code class="docutils literal notranslate"><span class="pre">+0.0</span></code> produce the same bytes string.</p></li>
  280. </ul>
  281. <dl class="c function">
  282. <dt class="sig sig-object c" id="c.PyFloat_Pack2">
  283. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyFloat_Pack2</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">double</span></span><span class="w"> </span><span class="n"><span class="pre">x</span></span>, <span class="kt"><span class="pre">unsigned</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">p</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">le</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyFloat_Pack2" title="Permalink to this definition">¶</a><br /></dt>
  284. <dd><p>Pack a C double as the IEEE 754 binary16 half-precision format.</p>
  285. </dd></dl>
  286. <dl class="c function">
  287. <dt class="sig sig-object c" id="c.PyFloat_Pack4">
  288. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyFloat_Pack4</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">double</span></span><span class="w"> </span><span class="n"><span class="pre">x</span></span>, <span class="kt"><span class="pre">unsigned</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">p</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">le</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyFloat_Pack4" title="Permalink to this definition">¶</a><br /></dt>
  289. <dd><p>Pack a C double as the IEEE 754 binary32 single precision format.</p>
  290. </dd></dl>
  291. <dl class="c function">
  292. <dt class="sig sig-object c" id="c.PyFloat_Pack8">
  293. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyFloat_Pack8</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">double</span></span><span class="w"> </span><span class="n"><span class="pre">x</span></span>, <span class="kt"><span class="pre">unsigned</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">p</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">le</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyFloat_Pack8" title="Permalink to this definition">¶</a><br /></dt>
  294. <dd><p>Pack a C double as the IEEE 754 binary64 double precision format.</p>
  295. </dd></dl>
  296. </section>
  297. <section id="unpack-functions">
  298. <h3>Unpack functions<a class="headerlink" href="#unpack-functions" title="Permalink to this headline">¶</a></h3>
  299. <p>The unpack routines read 2, 4 or 8 bytes, starting at <em>p</em>. <em>le</em> is an
  300. <span class="c-expr sig sig-inline c"><span class="kt">int</span></span> argument, non-zero if the bytes string is in little-endian format
  301. (exponent last, at <code class="docutils literal notranslate"><span class="pre">p+1</span></code>, <code class="docutils literal notranslate"><span class="pre">p+3</span></code> or <code class="docutils literal notranslate"><span class="pre">p+6</span></code> and <code class="docutils literal notranslate"><span class="pre">p+7</span></code>), zero if big-endian
  302. (exponent first, at <em>p</em>). The <code class="xref c c-macro docutils literal notranslate"><span class="pre">PY_BIG_ENDIAN</span></code> constant can be used to
  303. use the native endian: it is equal to <code class="docutils literal notranslate"><span class="pre">1</span></code> on big endian processor, or <code class="docutils literal notranslate"><span class="pre">0</span></code>
  304. on little endian processor.</p>
  305. <p>Return value: The unpacked double. On error, this is <code class="docutils literal notranslate"><span class="pre">-1.0</span></code> and
  306. <a class="reference internal" href="exceptions.html#c.PyErr_Occurred" title="PyErr_Occurred"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyErr_Occurred()</span></code></a> is true (and an exception is set, most likely
  307. <a class="reference internal" href="../library/exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a>).</p>
  308. <p>Note that on a non-IEEE platform this will refuse to unpack a bytes string that
  309. represents a NaN or infinity.</p>
  310. <dl class="c function">
  311. <dt class="sig sig-object c" id="c.PyFloat_Unpack2">
  312. <span class="kt"><span class="pre">double</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyFloat_Unpack2</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">unsigned</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">p</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">le</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyFloat_Unpack2" title="Permalink to this definition">¶</a><br /></dt>
  313. <dd><p>Unpack the IEEE 754 binary16 half-precision format as a C double.</p>
  314. </dd></dl>
  315. <dl class="c function">
  316. <dt class="sig sig-object c" id="c.PyFloat_Unpack4">
  317. <span class="kt"><span class="pre">double</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyFloat_Unpack4</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">unsigned</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">p</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">le</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyFloat_Unpack4" title="Permalink to this definition">¶</a><br /></dt>
  318. <dd><p>Unpack the IEEE 754 binary32 single precision format as a C double.</p>
  319. </dd></dl>
  320. <dl class="c function">
  321. <dt class="sig sig-object c" id="c.PyFloat_Unpack8">
  322. <span class="kt"><span class="pre">double</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyFloat_Unpack8</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">unsigned</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">p</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">le</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyFloat_Unpack8" title="Permalink to this definition">¶</a><br /></dt>
  323. <dd><p>Unpack the IEEE 754 binary64 double precision format as a C double.</p>
  324. </dd></dl>
  325. </section>
  326. </section>
  327. </section>
  328. <div class="clearer"></div>
  329. </div>
  330. </div>
  331. </div>
  332. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  333. <div class="sphinxsidebarwrapper">
  334. <div>
  335. <h3><a href="../contents.html">Table of Contents</a></h3>
  336. <ul>
  337. <li><a class="reference internal" href="#">Floating Point Objects</a><ul>
  338. <li><a class="reference internal" href="#pack-and-unpack-functions">Pack and Unpack functions</a><ul>
  339. <li><a class="reference internal" href="#pack-functions">Pack functions</a></li>
  340. <li><a class="reference internal" href="#unpack-functions">Unpack functions</a></li>
  341. </ul>
  342. </li>
  343. </ul>
  344. </li>
  345. </ul>
  346. </div>
  347. <div>
  348. <h4>Previous topic</h4>
  349. <p class="topless"><a href="bool.html"
  350. title="previous chapter">Boolean Objects</a></p>
  351. </div>
  352. <div>
  353. <h4>Next topic</h4>
  354. <p class="topless"><a href="complex.html"
  355. title="next chapter">Complex Number Objects</a></p>
  356. </div>
  357. <div role="note" aria-label="source link">
  358. <h3>This Page</h3>
  359. <ul class="this-page-menu">
  360. <li><a href="../bugs.html">Report a Bug</a></li>
  361. <li>
  362. <a href="https://github.com/python/cpython/blob/main/Doc/c-api/float.rst"
  363. rel="nofollow">Show Source
  364. </a>
  365. </li>
  366. </ul>
  367. </div>
  368. </div>
  369. </div>
  370. <div class="clearer"></div>
  371. </div>
  372. <div class="related" role="navigation" aria-label="related navigation">
  373. <h3>Navigation</h3>
  374. <ul>
  375. <li class="right" style="margin-right: 10px">
  376. <a href="../genindex.html" title="General Index"
  377. >index</a></li>
  378. <li class="right" >
  379. <a href="../py-modindex.html" title="Python Module Index"
  380. >modules</a> |</li>
  381. <li class="right" >
  382. <a href="complex.html" title="Complex Number Objects"
  383. >next</a> |</li>
  384. <li class="right" >
  385. <a href="bool.html" title="Boolean Objects"
  386. >previous</a> |</li>
  387. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  388. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  389. <li class="switchers">
  390. <div class="language_switcher_placeholder"></div>
  391. <div class="version_switcher_placeholder"></div>
  392. </li>
  393. <li>
  394. </li>
  395. <li id="cpython-language-and-version">
  396. <a href="../index.html">3.12.0 Documentation</a> &#187;
  397. </li>
  398. <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &#187;</li>
  399. <li class="nav-item nav-item-2"><a href="concrete.html" >Concrete Objects Layer</a> &#187;</li>
  400. <li class="nav-item nav-item-this"><a href="">Floating Point Objects</a></li>
  401. <li class="right">
  402. <div class="inline-search" role="search">
  403. <form class="inline-search" action="../search.html" method="get">
  404. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  405. <input type="submit" value="Go" />
  406. </form>
  407. </div>
  408. |
  409. </li>
  410. <li class="right">
  411. <label class="theme-selector-label">
  412. Theme
  413. <select class="theme-selector" oninput="activateTheme(this.value)">
  414. <option value="auto" selected>Auto</option>
  415. <option value="light">Light</option>
  416. <option value="dark">Dark</option>
  417. </select>
  418. </label> |</li>
  419. </ul>
  420. </div>
  421. <div class="footer">
  422. &copy; <a href="../copyright.html">Copyright</a> 2001-2023, Python Software Foundation.
  423. <br />
  424. This page is licensed under the Python Software Foundation License Version 2.
  425. <br />
  426. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  427. <br />
  428. See <a href="/license.html">History and License</a> for more information.<br />
  429. <br />
  430. The Python Software Foundation is a non-profit corporation.
  431. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  432. <br />
  433. <br />
  434. Last updated on Oct 02, 2023.
  435. <a href="/bugs.html">Found a bug</a>?
  436. <br />
  437. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
  438. </div>
  439. </body>
  440. </html>