{"id":1262,"date":"2021-06-25T23:30:44","date_gmt":"2021-06-25T23:30:44","guid":{"rendered":"https:\/\/www.ishygddt.xyz\/~blog\/?p=1262"},"modified":"2022-03-08T15:54:07","modified_gmt":"2022-03-08T21:54:07","slug":"python-rounding-fractions-half-up","status":"publish","type":"post","link":"http:\/\/www.ishygddt.xyz\/~blog\/2021\/06\/python-rounding-fractions-half-up","title":{"rendered":"Python: Rounding fractions half-up"},"content":{"rendered":"<p>By default, Python's <code class=\"language-python\" data-line=\"\">fractions.Fraction<\/code> rounds halves to the nearest <em>even<\/em> number. If you, instead, want to round a fraction but send halves\u00a0<em>up<\/em>, here's how that's done:<\/p>\n<pre><code class=\"language-python\" data-line=\"\">from math import floor\nfrom fractions import Fraction\n\n_ONEHALF = Fraction(1, 2)\n\ndef roundhalfup(x: Fraction) -&gt; int:\n\t&quot;&quot;&quot;\n\tRounds x to the nearest integer, with ties being rounded towards positive infinity\n\t&quot;&quot;&quot;\n\treturn floor(x + _ONEHALF)<\/code><\/pre>\n<p>Since <code class=\"language-python\" data-line=\"\">fractions.Fraction<\/code> implements <code class=\"language-python\" data-line=\"\">.__floor__<\/code> itself, this <em>does<\/em> give precise results. At no point is the number converted into a <code class=\"language-python\" data-line=\"\">float<\/code> or other approximation.<\/p>\n<hr \/>\n<p>Even if you're not dealing directly in <code class=\"language-python\" data-line=\"\">Fraction<\/code>s, the library can still be useful:<\/p>\n<pre><code class=\"language-python\" data-line=\"\">def divroundhalfup(a: int, b: int) -&gt; int:\n\t&quot;&quot;&quot;\n\tReturns the nearest integer to exactly a\/b, with ties rounded up\n\t&quot;&quot;&quot;\n\treturn floor(Fraction(2*a + b, 2*b))<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>By default, Python's <code class=\"language-python\" data-line=\"\">fractions<\/code> rounds halves to the nearest <em>even<\/em> number. If you, instead, want to round a fraction but send halves <em>up<\/em>, here's how that's done:<\/p>\n<pre><code class=\"language-python\" data-line=\"\">def roundhalfup(x: Fraction) -&gt; int:\n\t&quot;&quot;&quot;\n\tRounds x to the nearest integer, with ties being rounded towards positive infinity\n\t&quot;&quot;&quot;\n\treturn floor(x + Fraction(1, 2))<\/code><\/pre>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[91,44],"class_list":["post-1262","post","type-post","status-publish","format-standard","hentry","category-writeups","tag-math","tag-python"],"_links":{"self":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts\/1262","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/comments?post=1262"}],"version-history":[{"count":18,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts\/1262\/revisions"}],"predecessor-version":[{"id":1275,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts\/1262\/revisions\/1275"}],"wp:attachment":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/media?parent=1262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/categories?post=1262"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/tags?post=1262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}