{"id":2238,"date":"2021-02-20T02:38:51","date_gmt":"2021-02-20T02:38:51","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=2238"},"modified":"2022-09-10T04:13:52","modified_gmt":"2022-09-10T04:13:52","slug":"python-float","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/advanced-python\/python-float\/","title":{"rendered":"Python float"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the Python float type, how Python represents floating-point numbers, and how to test the floating-point number for equality.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-float-type'>Introduction to the Python float type <a href=\"#introduction-to-the-python-float-type\" class=\"anchor\" id=\"introduction-to-the-python-float-type\" title=\"Anchor for Introduction to the Python float type\">#<\/a><\/h2>\n\n\n\n<p>Python uses the <code>float<\/code> class to represent real numbers.<\/p>\n\n\n\n<p>CPython implements float using C double type. The C double type usually implements <a href=\"https:\/\/en.wikipedia.org\/wiki\/Double-precision_floating-point_format\" target=\"_blank\" rel=\"noreferrer noopener\">IEEE 754 double-precision binary float<\/a>, which is also called binary64.<\/p>\n\n\n\n<p>Python float uses 8 bytes (or 64 bits) to represent real numbers. Unlike the <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-integers\/\" target=\"_blank\" rel=\"noreferrer noopener\">integer type<\/a>, the float type uses a fixed number of bytes.<\/p>\n\n\n\n<p>Technically, Python uses 64 bits as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>1 bit for sign (positive or negative)<\/li><li>11 bits for exponent 1.5e-5 1.5 x 10<sup>-5<\/sup> (exponent is <code>-5<\/code>) the range is <code>[-1022, 1023]<\/code>.<\/li><li>52 bits for significant digits<\/li><\/ul>\n\n\n\n<p>For the sake of simplicity, significant digits are all digits except leading and trailing zeros.<\/p>\n\n\n\n<p>For example, 0.25 has two significant digits, 0.125 has three significant digits, and 12.25 has four significant digits.<\/p>\n\n\n\n<p>(1.25)<sub>10<\/sub> = (1&#215;2<sup>0<\/sup> + 0x2<sup>-1<\/sup> + 1&#215;2<sup>-2<\/sup>)<sub>10<\/sub> = (1.01)<sub>2<\/sub><\/p>\n\n\n\n<p>Some numbers have a finite binary representation, but some don&#8217;t, e.g., <code>0.1<\/code>. It&#8217;s <code>01.0001100110011...<\/code> in binary.<\/p>\n\n\n\n<p>Because of this, Python can only use approximate float representations for those numbers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='python-float-class'>Python float class <a href=\"#python-float-class\" class=\"anchor\" id=\"python-float-class\" title=\"Anchor for Python float class\">#<\/a><\/h2>\n\n\n\n<p>The <code><a href=\"https:\/\/www.pythontutorial.net\/python-built-in-functions\/python-float\/\">float()<\/a><\/code> returns a floating-point number based on a number or a string. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">&gt;&gt;&gt; float(<span class=\"hljs-number\">0.1<\/span>)\n<span class=\"hljs-number\">0.1<\/span>\n&gt;&gt;&gt; float(<span class=\"hljs-string\">'1.25'<\/span>)\n<span class=\"hljs-number\">1.25<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you pass an object (<code>obj<\/code>) to the <code>float(obj)<\/code>, it&#8217;ll delegate to the <code>obj.__float__()<\/code>. If the <code>__float__()<\/code> is not defined, it&#8217;ll fall back to <code>__index__()<\/code>.<\/p>\n\n\n\n<p>If you don&#8217;t pass any argument to the <code>float()<\/code>, it&#8217;ll return <code>0.0<\/code><\/p>\n\n\n\n<p>When you use the <code>print()<\/code> function, you&#8217;ll see that the number <code>0.1<\/code> is represented as <code>0.1<\/code> exactly.<\/p>\n\n\n\n<p>Internally, Python can only represent <code>0.1<\/code> approximately.<\/p>\n\n\n\n<p>To see how Python represents the <code>0.1<\/code> internally, you can use the <code>format()<\/code> function.<\/p>\n\n\n\n<p>The following shows how Python represents the number 0.1 using 20 digits:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">&gt;&gt;&gt; format(<span class=\"hljs-number\">0.1<\/span>, <span class=\"hljs-string\">'.20f'<\/span>)\n<span class=\"hljs-string\">'0.10000000000000000555'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>As you can see, <code>0.1<\/code> is not exactly <code>0.1<\/code> but <code>0.10000000000000000555...<\/code><\/p>\n\n\n\n<p>Because Python can represent some floats approximately, it will cause many problems when you compare two floating-point numbers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='equality-testing'>Equality testing <a href=\"#equality-testing\" class=\"anchor\" id=\"equality-testing\" title=\"Anchor for Equality testing\">#<\/a><\/h2>\n\n\n\n<p>Let&#8217;s take a look at the following example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">x = <span class=\"hljs-number\">0.1<\/span> + <span class=\"hljs-number\">0.1<\/span> + <span class=\"hljs-number\">0.1<\/span>\ny = <span class=\"hljs-number\">0.3<\/span>\n\n<span class=\"hljs-keyword\">print<\/span>(x == y)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">False<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Internally, Python cannot use a finite number of digits to represent the numbers <code>x<\/code> and <code>y<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">print<\/span>(format(x, <span class=\"hljs-string\">'.20f'<\/span>))\n<span class=\"hljs-keyword\">print<\/span>(format(y, <span class=\"hljs-string\">'.20f'<\/span>))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\">0<span class=\"hljs-selector-class\">.30000000000000004441<\/span>\n0<span class=\"hljs-selector-class\">.29999999999999998890<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that the number of digits is infinite. We just show the first 20 digits.<\/p>\n\n\n\n<p>One way to work around this problem is to round both sides of the equality expression to a number of significant digits. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">x = <span class=\"hljs-number\">0.1<\/span> + <span class=\"hljs-number\">0.1<\/span> + <span class=\"hljs-number\">0.1<\/span>\ny = <span class=\"hljs-number\">0.3<\/span>\n<span class=\"hljs-keyword\">print<\/span>(round(x, <span class=\"hljs-number\">3<\/span>) == round(y, <span class=\"hljs-number\">3<\/span>))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">True<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This workaround doesn&#8217;t work in all cases.<\/p>\n\n\n\n<p><a href=\"https:\/\/www.python.org\/dev\/peps\/pep-0485\/\" target=\"_blank\" rel=\"noreferrer noopener\">PEP485<\/a> provides a solution that fixes this problem by using relative and absolute tolerances.<\/p>\n\n\n\n<p>It provides the <code>isclose()<\/code> function from the <code>math<\/code> module returns <code>True<\/code> if two numbers are relatively close to each other.<\/p>\n\n\n\n<p>The following shows the <code>isclose()<\/code> function signature:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">isclose(a, b, rel_tol=1e-9, abs_tol=0.0)<\/code><\/span><\/pre>\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">from<\/span> math <span class=\"hljs-keyword\">import<\/span> isclose\n\nx = <span class=\"hljs-number\">0.1<\/span> + <span class=\"hljs-number\">0.1<\/span> + <span class=\"hljs-number\">0.1<\/span>\ny = <span class=\"hljs-number\">0.3<\/span>\n\nprint(isclose(x,y))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">True<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Python uses <code>float<\/code> class to represent real numbers.<\/li><li>Python uses a fixed number of bytes (8 bytes) to represent floats. Therefore, it can represent some numbers in binary approximately.<\/li><li>Use the <code>isclose()<\/code> function from the math module to test equality for floating-point numbers.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2238\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-float\/\"\n\t\t\t\tdata-post-title=\"Python float\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2238\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-float\/\"\n\t\t\t\tdata-post-title=\"Python float\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about the Python float type, how Python represents the floating-point numbers, and how to test the floating-point number for equality.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":757,"menu_order":30,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2238","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=2238"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2238\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/757"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=2238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}