{"id":609,"date":"2025-04-21T19:45:17","date_gmt":"2025-04-21T17:45:17","guid":{"rendered":"https:\/\/tronche.com\/blog\/?p=609"},"modified":"2025-04-21T19:45:17","modified_gmt":"2025-04-21T17:45:17","slug":"python2-round-isnt-what-you-think","status":"publish","type":"post","link":"https:\/\/tronche.com\/blog\/2025\/04\/python2-round-isnt-what-you-think\/","title":{"rendered":"Python2 round isn&#8217;t what you think"},"content":{"rendered":"\n<p>The usual wisdom is that python2 &#8220;rounding of halfway cases was away from zero&#8221;.<\/p>\n\n\n\n<p>Does that mean that <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Python 2.7.12 (default, Mar  1 2021, 11:38:31) \n&#91;GCC 5.4.0 20160609] on linux2\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n&gt;&gt;&gt; round(100.215, 2)\n100.22\n<\/code><\/pre>\n\n\n\n<p>Right ? Away from zero.<\/p>\n\n\n\n<p>Then, explain this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>>> round(2.675, 2)\n2.67\n<\/code><\/pre>\n\n\n\n<p>The problem seems to come from the underlying floating point representation (no way to represent exactly 2.675 for example). <\/p>\n\n\n\n<p>Utterly annoying when porting code from python2 to python3, especially with a ton of tests that will break.<\/p>\n\n\n\n<p>By the way<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>>>> round(56.805, 2)\n56.8\n>>> round(-2.675, 2)\n-2.67\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>The usual wisdom is that python2 &#8220;rounding of halfway cases was away from zero&#8221;. Does that mean that Right ? Away from zero. Then, explain <a class=\"mh-excerpt-more\" href=\"https:\/\/tronche.com\/blog\/2025\/04\/python2-round-isnt-what-you-think\/\" title=\"Python2 round isn&#8217;t what you think\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[247],"tags":[],"class_list":["post-609","post","type-post","status-publish","format-standard","hentry","category-hack"],"_links":{"self":[{"href":"https:\/\/tronche.com\/blog\/wp-json\/wp\/v2\/posts\/609","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tronche.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tronche.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tronche.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tronche.com\/blog\/wp-json\/wp\/v2\/comments?post=609"}],"version-history":[{"count":1,"href":"https:\/\/tronche.com\/blog\/wp-json\/wp\/v2\/posts\/609\/revisions"}],"predecessor-version":[{"id":610,"href":"https:\/\/tronche.com\/blog\/wp-json\/wp\/v2\/posts\/609\/revisions\/610"}],"wp:attachment":[{"href":"https:\/\/tronche.com\/blog\/wp-json\/wp\/v2\/media?parent=609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tronche.com\/blog\/wp-json\/wp\/v2\/categories?post=609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tronche.com\/blog\/wp-json\/wp\/v2\/tags?post=609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}