{"id":714,"date":"2014-07-10T21:58:31","date_gmt":"2014-07-10T21:58:31","guid":{"rendered":"http:\/\/blog.silverterra.net\/?p=714"},"modified":"2014-07-10T21:58:31","modified_gmt":"2014-07-10T21:58:31","slug":"javascript-float-strange-behavior","status":"publish","type":"post","link":"https:\/\/blog.silverterra.net\/?p=714","title":{"rendered":"javascript float strange behavior"},"content":{"rendered":"<blockquote><p>Goal: to discover some black hole<\/p><\/blockquote>\n<p>Javascript is perfect but&#8230; check this small piece of code<br \/>\n<code data-enlighter-language=\"js\" class=\"EnlighterJSRAW\"><\/code><br \/>\n  for(var i = 0;i < 5; i+=.1){console.log(i);}\n[\/js]\nThis is a simple iteration but the result is terrible: (with other arguments like .2, .3 ... the result will be different but bad as well - try)\n[js]\n0\n0.1\n0.2\n0.30000000000000004\n0.4\n0.5\n0.6\n0.7\n0.7999999999999999 \n0.8999999999999999 \n0.9999999999999999 \n1.0999999999999999 \n1.2\n1.3\n1.4000000000000001 \n1.5000000000000002 \n1.6000000000000003\n1.7000000000000004\n1.8000000000000005\n1.9000000000000006\n2.0000000000000004\n2.1000000000000005\n2.2000000000000006\n2.3000000000000007\n2.400000000000001 \n2.500000000000001 \n2.600000000000001 \n2.700000000000001 \n2.800000000000001 \n2.9000000000000012\n3.0000000000000013\n3.1000000000000014\n3.2000000000000015\n3.3000000000000016\n3.4000000000000017\n3.5000000000000018\n3.600000000000002 \n3.700000000000002 \n3.800000000000002 \n3.900000000000002 \n4.000000000000002 \n4.100000000000001 \n4.200000000000001 \n4.300000000000001 \n4.4\n4.5\n4.6\n4.699999999999999 \n4.799999999999999 \n4.899999999999999 \n4.999999999999998 \n[\/js]\nThe picture is the same in the main browsers. Nice supprise and we have a simple way to get the correct result. Just use the [js]toFixed()[\/js] function.\n[js]\n  for(var i = 0;i < 5; i+=.1){console.log(i.toFixed(1));}\n\/\/ and the result is correct now:\n0.0\n0.1\n0.2\n0.3\n0.4\n0.5\n0.6\n0.7\n0.8\n0.9\n1.0\n1.1\n1.2\n1.3\n1.4\n1.5\n1.6\n1.7\n1.8\n1.9\n2.0\n2.1\n2.2\n2.3\n2.4\n2.5\n2.6\n2.7\n2.8\n2.9\n3.0\n3.1\n3.2\n3.3\n3.4\n3.5\n3.6\n3.7\n3.8\n3.9\n4.0\n4.1\n4.2\n4.3\n4.4\n4.5\n4.6\n4.7\n4.8\n4.9\n5.0\n[\/js]\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Goal: to discover some black hole Javascript is perfect but&#8230; check this small piece of code for(var i = 0;i < 5; i+=.1){console.log(i);} [\/js] This is a simple iteration but the result is terrible: (with other arguments like .2, .3 ... the result will be different but bad as well - try) [js] 0 0.1 [&hellip;]\n<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,11],"tags":[],"class_list":["post-714","post","type-post","status-publish","format-standard","hentry","category-javascript","category-tricky"],"_links":{"self":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/714","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=714"}],"version-history":[{"count":1,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/714\/revisions"}],"predecessor-version":[{"id":715,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/714\/revisions\/715"}],"wp:attachment":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}