{"id":6225,"date":"2023-02-09T20:43:42","date_gmt":"2023-02-09T20:43:42","guid":{"rendered":"https:\/\/really.zonky.org\/?p=6225"},"modified":"2023-02-09T20:43:44","modified_gmt":"2023-02-09T20:43:44","slug":"why-do-computers-suck-at-maths","status":"publish","type":"post","link":"https:\/\/really.zonky.org\/?p=6225","title":{"rendered":"Why Do Computers Suck At Maths?"},"content":{"rendered":"\n<p>Sometimes, computers seem to suck at maths :-<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; 1.2 - 1.0\n0.19999999999999996<\/code><\/pre>\n\n\n\n<p>To be fair, that&#8217;s a raw Python interface; an application intended for use as a calculator works a bit better :-<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u00bb qalc \n&gt; 1.2 - 1.0\n\n  1.2 \u2212 1 = 0.2<\/code><\/pre>\n\n\n\n<p>The problem is related to low-level numeric types. Computers store numbers in a variety of different formats (called <em>types<\/em> by developers). Whole numbers (integers) are easy &#8211; just allocate a certain number of 8-bit bytes (more means you can store bigger numbers; but it takes more memory for each number) and you would have something that would store whole numbers with perfect accuracy.<\/p>\n\n\n\n<p>Floating point (i.e. numbers with a &#8216;decimal&#8217; point) on the other hand are much more a compromise between size and accuracy. Floating point in effect uses scientific notation for numbers &#8211; 1.23E23. So a number is split into two &#8211; the mantissa (effectively the bit before the &#8220;E&#8221;) and the exponent. Storing two numbers in 32-bits (single precision) limits the precision in which numbers are stored but is usually sufficient and allows a far larger range of numbers :-<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>>> print(\"{:10.4f}\".format(1.2 - 1.0)) \n    0.2000<\/code><\/pre>\n\n\n\n<p>In other words if you are using a low-level interface as a calculator, you can produce sensible output merely by writing your code properly. Or use a proper calculator program like qalc(ulator). <\/p>\n\n\n\n<p>This is of course an <a href=\"https:\/\/en.wikipedia.org\/wiki\/Single-precision_floating-point_format\">over simplification<\/a> and the Wikipedia article on <a href=\"https:\/\/en.wikipedia.org\/wiki\/Single-precision_floating-point_format\">single precision floating point<\/a> goes into far more detail than I want to understand. Amongst other things I&#8217;ve glossed over is the problem of performing calculations in base 2 (binary) rather than base 10 (decimal).<\/p>\n\n\n\n<p>Plus there are a whole bunch of other numeric types such as larger floating point types, decimal floating point, bignums (which use whatever memory is necessary to store a number), fixed point, etc. <\/p>\n\n\n\n<p>Computers aren&#8217;t bad at maths; it is just you can trick them into making themselves look bad. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"695\" height=\"521\" src=\"https:\/\/i0.wp.com\/really.zonky.org\/wp-content\/uploads\/2019-11-30-untitled.jpeg?resize=695%2C521&#038;ssl=1\" alt=\"Untitled Seascape\" class=\"wp-image-5678\" srcset=\"https:\/\/i0.wp.com\/really.zonky.org\/wp-content\/uploads\/2019-11-30-untitled.jpeg?resize=1024%2C768&amp;ssl=1 1024w, https:\/\/i0.wp.com\/really.zonky.org\/wp-content\/uploads\/2019-11-30-untitled.jpeg?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/really.zonky.org\/wp-content\/uploads\/2019-11-30-untitled.jpeg?resize=768%2C576&amp;ssl=1 768w, https:\/\/i0.wp.com\/really.zonky.org\/wp-content\/uploads\/2019-11-30-untitled.jpeg?resize=1536%2C1151&amp;ssl=1 1536w, https:\/\/i0.wp.com\/really.zonky.org\/wp-content\/uploads\/2019-11-30-untitled.jpeg?resize=2048%2C1535&amp;ssl=1 2048w, https:\/\/i0.wp.com\/really.zonky.org\/wp-content\/uploads\/2019-11-30-untitled.jpeg?w=1390&amp;ssl=1 1390w\" sizes=\"auto, (max-width: 695px) 100vw, 695px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, computers seem to suck at maths :- To be fair, that&#8217;s a raw Python interface; an application intended for use as a calculator works a bit better :- The problem is related to low-level numeric types. Computers store numbers in a variety of different formats (called types by developers). Whole numbers (integers) are easy <a href='https:\/\/really.zonky.org\/?p=6225' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_share_on_mastodon":"1"},"categories":[4,7],"tags":[2134,2135,2133,2132],"class_list":["post-6225","post","type-post","status-publish","format-standard","hentry","category-it","category-general","tag-binary","tag-decimal","tag-floating-point","tag-integers","category-4-id","category-7-id","post-seq-1","post-parity-odd","meta-position-corners","fix"],"share_on_mastodon":{"url":"https:\/\/mstdn.social\/@grumpygrimnir\/109836725286510308","error":""},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1f2KI-1Cp","_links":{"self":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts\/6225","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6225"}],"version-history":[{"count":3,"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts\/6225\/revisions"}],"predecessor-version":[{"id":6235,"href":"https:\/\/really.zonky.org\/index.php?rest_route=\/wp\/v2\/posts\/6225\/revisions\/6235"}],"wp:attachment":[{"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/really.zonky.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}