{"id":3837,"date":"2022-08-20T19:24:12","date_gmt":"2022-08-20T13:54:12","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-is-floating-point-math-broken\/"},"modified":"2022-08-20T19:24:12","modified_gmt":"2022-08-20T13:54:12","slug":"solved-is-floating-point-math-broken","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-is-floating-point-math-broken\/","title":{"rendered":"(Solved) Is floating point math broken?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-588014\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"588014\" data-parentid=\"588004\" data-score=\"2759\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Binary <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Double-precision_floating-point_format\">floating point<\/a> math is like this. In most programming languages, it is based on the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/IEEE_754#Basic_and_interchange_formats\">IEEE 754 standard<\/a>. The crux of the problem is that numbers are represented in this format as a whole number times a power of two; rational numbers (such as <code>0.1<\/code>, which is <code>1\/10<\/code>) whose denominator is not a power of two cannot be exactly represented.<\/p>\n<p>For <code>0.1<\/code> in the standard <code>binary64<\/code> format, the representation can be written exactly as<\/p>\n<ul>\n<li><code>0.1000000000000000055511151231257827021181583404541015625<\/code> in decimal, or<\/li>\n<li><code>0x1.999999999999ap-4<\/code> in <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.exploringbinary.com\/hexadecimal-floating-point-constants\/\">C99 hexfloat notation<\/a>.<\/li>\n<\/ul>\n<p>In contrast, the rational number <code>0.1<\/code>, which is <code>1\/10<\/code>, can be written exactly as<\/p>\n<ul>\n<li><code>0.1<\/code> in decimal, or<\/li>\n<li><code>0x1.99999999999999...p-4<\/code> in an analogue of C99 hexfloat notation, where the <code>...<\/code> represents an unending sequence of 9&#8217;s.<\/li>\n<\/ul>\n<p>The constants <code>0.2<\/code> and <code>0.3<\/code> in your program will also be approximations to their true values.  It happens that the closest <code>double<\/code> to <code>0.2<\/code> is larger than the rational number <code>0.2<\/code> but that the closest <code>double<\/code> to <code>0.3<\/code> is smaller than the rational number <code>0.3<\/code>.  The sum of <code>0.1<\/code> and <code>0.2<\/code> winds up being larger than the rational number <code>0.3<\/code> and hence disagreeing with the constant in your code.<\/p>\n<p>A fairly comprehensive treatment of floating-point arithmetic issues is <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/download.oracle.com\/docs\/cd\/E19957-01\/806-3568\/ncg_goldberg.html\"><em>What Every Computer Scientist Should Know About Floating-Point Arithmetic<\/em><\/a>. For an easier-to-digest explanation, see <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/floating-point-gui.de\">floating-point-gui.de<\/a>.<\/p>\n<p><strong>Side Note: All positional (base-N) number systems share this problem with precision<\/strong><\/p>\n<p>Plain old decimal (base 10) numbers have the same issues, which is why numbers like 1\/3 end up as 0.333333333&#8230;<\/p>\n<p>You&#8217;ve just stumbled on a number (3\/10) that happens to be easy to represent with the decimal system, but doesn&#8217;t fit the binary system. It goes both ways (to some small degree) as well: 1\/16 is an ugly number in decimal (0.0625), but in binary it looks as neat as a 10,000th does in decimal (0.0001)** &#8211; if we were in the habit of using a base-2 number system in our daily lives, you&#8217;d even look at that number and instinctively understand you could arrive there by halving something, halving it again, and again and again.<\/p>\n<p>** Of course, that&#8217;s not exactly how floating-point numbers are stored in memory (they use a form of scientific notation). However, it does illustrate the point that binary floating-point precision errors tend to crop up because the &#8220;real world&#8221; numbers we are usually interested in working with are so often powers of ten &#8211; but only because we use a decimal number system day-to-day. This is also why we&#8217;ll say things like 71% instead of &#8220;5 out of every 7&#8221; (71% is an approximation, since 5\/7 can&#8217;t be represented exactly with any decimal number).<\/p>\n<p>So no: binary floating point numbers are not broken, they just happen to be as imperfect as every other base-N number system \ud83d\ude42<\/p>\n<p><strong>Side Side Note: Working with Floats in Programming<\/strong><\/p>\n<p>In practice, this problem of precision means you need to use rounding functions to round your floating point numbers off to however many decimal places you&#8217;re interested in before you display them.<\/p>\n<p>You also need to replace equality tests with comparisons that allow some amount of tolerance, which means:<\/p>\n<p>Do <em>not<\/em> do <code>if (x == y) { ... }<\/code><\/p>\n<p>Instead do <code>if (abs(x - y) &lt; myToleranceValue) { ... }<\/code>.<\/p>\n<p>where <code>abs<\/code> is the absolute value. <code>myToleranceValue<\/code> needs to be chosen for your particular application &#8211; and it will have a lot to do with how much &#8220;wiggle room&#8221; you are prepared to allow, and what the largest number you are going to be comparing may be (due to loss of precision issues). Beware of &#8220;epsilon&#8221; style constants in your language of choice. These are <em>not<\/em> to be used as tolerance values.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">18<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Is floating point math broken? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Binary floating point math is like this. In most programming languages, it is based on the IEEE 754 standard. The crux of the problem is that numbers are represented in this format as a whole number times a power of two; rational numbers (such as 0.1, which is 1\/10) whose denominator is not a &#8230; <a title=\"(Solved) Is floating point math broken?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-is-floating-point-math-broken\/\" aria-label=\"More on (Solved) Is floating point math broken?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[360,359,358,357],"class_list":["post-3837","post","type-post","status-publish","format-standard","hentry","category-solved","tag-floating-accuracy","tag-floating-point","tag-language-agnostic","tag-math"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>(Solved) Is floating point math broken? - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-is-floating-point-math-broken\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"(Solved) Is floating point math broken? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Binary floating point math is like this. In most programming languages, it is based on the IEEE 754 standard. The crux of the problem is that numbers are represented in this format as a whole number times a power of two; rational numbers (such as 0.1, which is 1\/10) whose denominator is not a ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-is-floating-point-math-broken\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-20T13:54:12+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-is-floating-point-math-broken\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-is-floating-point-math-broken\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"(Solved) Is floating point math broken?\",\"datePublished\":\"2022-08-20T13:54:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-is-floating-point-math-broken\/\"},\"wordCount\":574,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"floating-accuracy\",\"floating-point\",\"language-agnostic\",\"math\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-is-floating-point-math-broken\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-is-floating-point-math-broken\/\",\"name\":\"(Solved) Is floating point math broken? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-08-20T13:54:12+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-is-floating-point-math-broken\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-is-floating-point-math-broken\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-is-floating-point-math-broken\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"(Solved) Is floating point math broken?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"(Solved) Is floating point math broken? - JassWeb","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":"https:\/\/jassweb.com\/solved\/solved-is-floating-point-math-broken\/","og_locale":"en_US","og_type":"article","og_title":"(Solved) Is floating point math broken? - JassWeb","og_description":"[ad_1] Binary floating point math is like this. In most programming languages, it is based on the IEEE 754 standard. The crux of the problem is that numbers are represented in this format as a whole number times a power of two; rational numbers (such as 0.1, which is 1\/10) whose denominator is not a ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-is-floating-point-math-broken\/","og_site_name":"JassWeb","article_published_time":"2022-08-20T13:54:12+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-is-floating-point-math-broken\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-is-floating-point-math-broken\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"(Solved) Is floating point math broken?","datePublished":"2022-08-20T13:54:12+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-is-floating-point-math-broken\/"},"wordCount":574,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["floating-accuracy","floating-point","language-agnostic","math"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-is-floating-point-math-broken\/","url":"https:\/\/jassweb.com\/solved\/solved-is-floating-point-math-broken\/","name":"(Solved) Is floating point math broken? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-08-20T13:54:12+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-is-floating-point-math-broken\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-is-floating-point-math-broken\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-is-floating-point-math-broken\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"(Solved) Is floating point math broken?"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/3837","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=3837"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/3837\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=3837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=3837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=3837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}