{"id":101,"date":"2009-08-12T15:39:18","date_gmt":"2009-08-12T19:39:18","guid":{"rendered":"http:\/\/www.sofastatistics.com\/blog\/?p=101"},"modified":"2009-08-12T15:44:16","modified_gmt":"2009-08-12T19:44:16","slug":"the-decimal-module-in-python","status":"publish","type":"post","link":"https:\/\/www.sofastatistics.com\/blog\/the-decimal-module-in-python\/","title":{"rendered":"The decimal module in Python"},"content":{"rendered":"<p>Python has a brilliant decimal module (<a href=\"http:\/\/docs.python.org\/library\/decimal.html\" target=\"_blank\">http:\/\/docs.python.org\/library\/decimal.html<\/a>) you may need if you want to avoid floating point errors.\u00a0 This may be necessary if you are faced with compounding errors under special circumstances e.g. if testing a statistical routine against a purpose-built test dataset (e.g. <a href=\"http:\/\/www.itl.nist.gov\/div898\/strd\/anova\/SmLs09_cv.html\" target=\"_blank\">http:\/\/www.itl.nist.gov\/div898\/strd\/anova\/SmLs09_cv.html<\/a>).\u00a0 The performance hit is substantial, however, so it has to be used judiciously.\u00a0 Anyway, here is an example:<\/p>\n<p><code>import decimal<br \/>\nD = decimal.Decimal<br \/>\ndecimal.getcontext().prec = 120<br \/>\nd1 = D(\"1.1\")<br \/>\nf1 = 1.1<br \/>\nprint \"Decimal result is: %s\" % round((d1**1000 - D(\"2.46993291801e+41\")),3)<br \/>\nprint \"Floating point result is: %s\" % round((f1**1000 - 2.46993291801e+41),3)<br \/>\n&gt;&gt;&gt;<\/code><\/p>\n<p>Decimal result is: -4.17366587591e+29<br \/>\nFloating point result is: -3.97456123863e+29<\/p>\n<p>Usually, floating point is good enough &#8211; but not under all circumstances.\u00a0 In which case, it pays to be familiar with the decimal module.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python has a brilliant decimal module (http:\/\/docs.python.org\/library\/decimal.html) you may need if you want to avoid floating point errors.\u00a0 This may be necessary if you are faced with compounding errors under special circumstances e.g. if testing a statistical routine against a purpose-built test dataset (e.g. http:\/\/www.itl.nist.gov\/div898\/strd\/anova\/SmLs09_cv.html).\u00a0 The performance hit is substantial, however, so it has to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,5],"tags":[],"class_list":["post-101","post","type-post","status-publish","format-standard","hentry","category-developers","category-python"],"_links":{"self":[{"href":"https:\/\/www.sofastatistics.com\/blog\/wp-json\/wp\/v2\/posts\/101","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sofastatistics.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sofastatistics.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sofastatistics.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sofastatistics.com\/blog\/wp-json\/wp\/v2\/comments?post=101"}],"version-history":[{"count":6,"href":"https:\/\/www.sofastatistics.com\/blog\/wp-json\/wp\/v2\/posts\/101\/revisions"}],"predecessor-version":[{"id":105,"href":"https:\/\/www.sofastatistics.com\/blog\/wp-json\/wp\/v2\/posts\/101\/revisions\/105"}],"wp:attachment":[{"href":"https:\/\/www.sofastatistics.com\/blog\/wp-json\/wp\/v2\/media?parent=101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sofastatistics.com\/blog\/wp-json\/wp\/v2\/categories?post=101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sofastatistics.com\/blog\/wp-json\/wp\/v2\/tags?post=101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}