{"id":192,"date":"2017-07-20T02:55:45","date_gmt":"2017-07-20T01:55:45","guid":{"rendered":"http:\/\/192.168.1.18\/?page_id=192"},"modified":"2017-07-20T13:29:51","modified_gmt":"2017-07-20T12:29:51","slug":"python-numbers-arithmetic","status":"publish","type":"page","link":"http:\/\/www.bulis.co.uk\/?page_id=192","title":{"rendered":"Python: Numbers and Arithmetic"},"content":{"rendered":"<p>Addition and subtraction uses the same characters as used in mathematics i.e. <strong>+<\/strong> and <strong>\u2013<\/strong> whereas division and multiplication uses <strong>\/<\/strong> (forward-slash) and <strong>* <\/strong>(asterix) respectively. Use <strong>** <\/strong>to multiply a value to the power of (exponent) and <strong>%<\/strong> to return the remainder of a value (modulo). If you supply a formula with integers (whole numbers) the result returned will be an integer, if you want to do calculations\u00a0 with decimal values then supply decimal values for example 20\/100 = 0 whereas 20.0\/100.0 = 0.2<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&gt;&gt;&gt; 1080 + 1920          # Addition\r\n3000\r\n&gt;&gt;&gt; 1080 \u2013 1920          # Subtraction\r\n-840\r\n&gt;&gt;&gt; 1080 * 1920          # Multiplication\r\n2073600\r\n&gt;&gt;&gt; 1080\/1920            # Division of integer\r\n0\r\n&gt;&gt;&gt; 1080.0\/1920.0        # Division of floating point\r\n0.5625\r\n&gt;&gt;&gt; 2 ** 8               # Power (exponent)\r\n256\r\n&gt;&gt;&gt; 8.75 % 0.5           # Modulo (remainder)\r\n0.25\r\n&gt;&gt;&gt; 65536 \/ (8 * 8)      # Nested Arithmetic\r\n1024\r\n<\/pre>\n<p>Python stores decimal values in binary floating-point number format which can have some undesirable side effects.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&gt;&gt;&gt; # Floating Point rounding error\r\n&gt;&gt;&gt; 0.1 + 0.2             # Floating Point Addition\r\n0.300000000000000004      # Should be 0.3 !!!???\r\n<\/pre>\n<h2><strong>Floating-Point Numbers.<\/strong><\/h2>\n<p>To speed up calculations Python stores numerical values as binary floating point numbers and allocates 57 bits. A Floating Point number is a form of scientific notation so 300,000,000 could be represented as 3&#215;10 to the power of 8 and 0.00000015 would be 1.5&#215;10 to the power of minus 7. If we wanted to multiply these numbers together it would be much easier to multiply 3 by 1.5 and then add 10 to the power of 8 + 10 to the power of minues 7 or 8 minus 7 equals 1 therefore the answer = 4.5&#215;10 to the power of 1 or 45. Now for the same reason that \u2153 cannot be represented in decimal without an infinite number of digits i.e. 0.33333&#8230; fractions cannot be represented absolutely accurately in binary floating point numbers.<\/p>\n<p>As an example 0.1 * Base10 (decimal) = 1 with no remainder. If we do that same thing with binary we get a recurring number i.e. 0.1 * Base2 (binary) = 0 with remainder 2 using this remainder we 0.2 * Base2 = 0 remainder 4 and so on we will always have a remainder.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n0.1 * 2 = 0.2          # 0\r\n0.2 * 2 = 0.4          # 0\r\n0.4 * 2 = 0.8          # 0\r\n0.8 * 2 = 1.6          # 1\r\n0.6 * 2 = 1.2          # 1\r\n0.2 * 2 = 0.4          # 0\r\n0.4 * 2 = 0.8          # 0\r\n0.8 * 2 = 1.6          # 1\r\n...\r\n<\/pre>\n<p>So let\u2019s see what 0.1 looks like in binary when truncated to 57 significant bits:<\/p>\n<p>0.000110011001100110011001100110011001100110011001100110011001\u2026<\/p>\n<p>Bits 54 and beyond total to greater than half the value of bit position 53 thereby rounding up to:<\/p>\n<p>0.0001100110011001100110011001100110011001100110011001101<\/p>\n<p>In decimal this is:<\/p>\n<p>0.1000000000000000055511151231257827021181583404541015625<\/p>\n<p>Now if we round to 17 significant bits it would be 0.10000000000000001<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Addition and subtraction uses the same characters as used in mathematics i.e. + and \u2013 whereas division and multiplication uses \/ (forward-slash) and * (asterix) respectively. Use ** to multiply a value to the power of (exponent) and % to return the remainder of a value (modulo). If you supply a formula with integers (whole [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":73,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.12 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python: Numbers and Arithmetic - Phantom Raspberry Blower<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.bulis.co.uk\/?page_id=192\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python: Numbers and Arithmetic - Phantom Raspberry Blower\" \/>\n<meta property=\"og:description\" content=\"Addition and subtraction uses the same characters as used in mathematics i.e. + and \u2013 whereas division and multiplication uses \/ (forward-slash) and * (asterix) respectively. Use ** to multiply a value to the power of (exponent) and % to return the remainder of a value (modulo). If you supply a formula with integers (whole [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.bulis.co.uk\/?page_id=192\" \/>\n<meta property=\"og:site_name\" content=\"Phantom Raspberry Blower\" \/>\n<meta property=\"article:modified_time\" content=\"2017-07-20T12:29:51+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\/\/www.bulis.co.uk\/?page_id=192\",\"url\":\"http:\/\/www.bulis.co.uk\/?page_id=192\",\"name\":\"Python: Numbers and Arithmetic - Phantom Raspberry Blower\",\"isPartOf\":{\"@id\":\"http:\/\/www.bulis.co.uk\/#website\"},\"datePublished\":\"2017-07-20T01:55:45+00:00\",\"dateModified\":\"2017-07-20T12:29:51+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/www.bulis.co.uk\/?page_id=192#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.bulis.co.uk\/?page_id=192\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.bulis.co.uk\/?page_id=192#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/www.bulis.co.uk\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tutorials\",\"item\":\"http:\/\/www.bulis.co.uk\/?page_id=57\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python Tutorials\",\"item\":\"http:\/\/www.bulis.co.uk\/?page_id=73\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Python: Numbers and Arithmetic\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/www.bulis.co.uk\/#website\",\"url\":\"http:\/\/www.bulis.co.uk\/\",\"name\":\"Phantom Raspberry Blower\",\"description\":\"Blowing Raspberrys since 1989\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/www.bulis.co.uk\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python: Numbers and Arithmetic - Phantom Raspberry Blower","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/www.bulis.co.uk\/?page_id=192","og_locale":"en_GB","og_type":"article","og_title":"Python: Numbers and Arithmetic - Phantom Raspberry Blower","og_description":"Addition and subtraction uses the same characters as used in mathematics i.e. + and \u2013 whereas division and multiplication uses \/ (forward-slash) and * (asterix) respectively. Use ** to multiply a value to the power of (exponent) and % to return the remainder of a value (modulo). If you supply a formula with integers (whole [&hellip;]","og_url":"http:\/\/www.bulis.co.uk\/?page_id=192","og_site_name":"Phantom Raspberry Blower","article_modified_time":"2017-07-20T12:29:51+00:00","twitter_card":"summary_large_image","twitter_misc":{"Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/www.bulis.co.uk\/?page_id=192","url":"http:\/\/www.bulis.co.uk\/?page_id=192","name":"Python: Numbers and Arithmetic - Phantom Raspberry Blower","isPartOf":{"@id":"http:\/\/www.bulis.co.uk\/#website"},"datePublished":"2017-07-20T01:55:45+00:00","dateModified":"2017-07-20T12:29:51+00:00","breadcrumb":{"@id":"http:\/\/www.bulis.co.uk\/?page_id=192#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.bulis.co.uk\/?page_id=192"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/www.bulis.co.uk\/?page_id=192#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/www.bulis.co.uk\/"},{"@type":"ListItem","position":2,"name":"Tutorials","item":"http:\/\/www.bulis.co.uk\/?page_id=57"},{"@type":"ListItem","position":3,"name":"Python Tutorials","item":"http:\/\/www.bulis.co.uk\/?page_id=73"},{"@type":"ListItem","position":4,"name":"Python: Numbers and Arithmetic"}]},{"@type":"WebSite","@id":"http:\/\/www.bulis.co.uk\/#website","url":"http:\/\/www.bulis.co.uk\/","name":"Phantom Raspberry Blower","description":"Blowing Raspberrys since 1989","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.bulis.co.uk\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"}]}},"_links":{"self":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/192"}],"collection":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=192"}],"version-history":[{"count":0,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/192\/revisions"}],"up":[{"embeddable":true,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/73"}],"wp:attachment":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}