{"id":19240,"date":"2022-11-05T23:27:12","date_gmt":"2022-11-05T17:57:12","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-ieee-754-binary-floating-point-numbers-imprecise-for-money\/"},"modified":"2022-11-05T23:27:12","modified_gmt":"2022-11-05T17:57:12","slug":"solved-ieee-754-binary-floating-point-numbers-imprecise-for-money","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-ieee-754-binary-floating-point-numbers-imprecise-for-money\/","title":{"rendered":"[Solved] IEEE 754 binary floating-point numbers imprecise for money"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-58135053\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"58135053\" data-parentid=\"58127957\" data-score=\"3\" 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>The original question:<\/p>\n<hr>\n<blockquote>\n<p>Incorrect floor number in golang<\/p>\n<p>I have problem when use Math.Floor with float variable (round<br \/>\n  down\/truncate the precision part). How can i do it correctly?<\/p>\n<pre><code>package main\n\nimport (\n    \"fmt\"\n    \"math\"\n)\n\nfunc main() {\n    var st float64 = 1980\n    var salePrice1 = st * 0.1 \/ 1.1\n    fmt.Printf(\"%T:%v\\n\", salePrice1, salePrice1)\n    var salePrice2 = math.Floor(st * 0.1 \/ 1.1)\n    fmt.Printf(\"%T:%v\\n\", salePrice2, salePrice2)\n}\n<\/code><\/pre>\n<p>I expect the output of 1980 * 0.1 \/ 1.1 to be 180, but the actual<br \/>\n  output is 179.\u201d<\/p>\n<p>Playground:<\/p>\n<p>Output:<\/p>\n<pre><code>float64:179.99999999999997\nfloat64:179\n<\/code><\/pre>\n<\/blockquote>\n<hr>\n<p>The XY problem is asking about your attempted solution rather than your actual problem: <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/xyproblem.info\/\">The XY Problem<\/a>.<\/p>\n<hr>\n<p>Clearly, this is a money calculation for <code>salePrice1<\/code>. Money calculations use precise decimal calculations, not imprecise binary floating-point calculations.<\/p>\n<p>For money calculations use integers. For example,<\/p>\n<pre><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    var st int64 = 198000 \/\/ $1980.00 as cents\n\n    fmt.Printf(\"%[1]T:%[1]v\\n\", st)\n    fmt.Printf(\"$%d.%02d\\n\", st\/100, st%100)\n\n    var n, d int64 = 1, 11\n    fmt.Printf(\"%d, %d\\n\", n, d)\n\n    var salePrice1 int64 = (st * n) \/ d \/\/ round down\n\n    fmt.Printf(\"%[1]T:%[1]v\\n\", salePrice1)\n    fmt.Printf(\"$%d.%02d\\n\", salePrice1\/100, salePrice1%100)\n\n    var salePrice2 int64 = ((st*n)*10\/d + 5) \/ 10 \/\/ round half up\n\n    fmt.Printf(\"%[1]T:%[1]v\\n\", salePrice2)\n    fmt.Printf(\"$%d.%02d\\n\", salePrice2\/100, salePrice2%100)\n\n    var salePrice3 int64 = (st*n + (d - 1)) \/ d \/\/ round up\n\n    fmt.Printf(\"%[1]T:%[1]v\\n\", salePrice1)\n    fmt.Printf(\"$%d.%02d\\n\", salePrice3\/100, salePrice3%100)\n}\n<\/code><\/pre>\n<p>Playground: <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/play.golang.org\/p\/HbqVJUXXR-N\">https:\/\/play.golang.org\/p\/HbqVJUXXR-N<\/a><\/p>\n<p>Output:<\/p>\n<pre><code>int64:198000\n$1980.00\n1, 11\nint64:18000\n$180.00\nint64:18000\n$180.00\nint64:18000\n$180.00\n<\/code><\/pre>\n<hr>\n<p>References:<\/p>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.oracle.com\/cd\/E19957-01\/806-3568\/ncg_goldberg.html\">What Every Computer Scientist Should Know About Floating-Point Arithmetic<\/a><\/p>\n<p>How should we calc money (decimal, big.Float)<\/p>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/speleotrove.com\/decimal\/\">General Decimal Arithmetic<\/a><\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">6<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved IEEE 754 binary floating-point numbers imprecise for money <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] The original question: Incorrect floor number in golang I have problem when use Math.Floor with float variable (round down\/truncate the precision part). How can i do it correctly? package main import ( &#8220;fmt&#8221; &#8220;math&#8221; ) func main() { var st float64 = 1980 var salePrice1 = st * 0.1 \/ 1.1 fmt.Printf(&#8220;%T:%v\\n&#8221;, salePrice1, salePrice1) &#8230; <a title=\"[Solved] IEEE 754 binary floating-point numbers imprecise for money\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-ieee-754-binary-floating-point-numbers-imprecise-for-money\/\" aria-label=\"More on [Solved] IEEE 754 binary floating-point numbers imprecise for money\">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":[596],"class_list":["post-19240","post","type-post","status-publish","format-standard","hentry","category-solved","tag-go"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] IEEE 754 binary floating-point numbers imprecise for money - 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-ieee-754-binary-floating-point-numbers-imprecise-for-money\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] IEEE 754 binary floating-point numbers imprecise for money - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] The original question: Incorrect floor number in golang I have problem when use Math.Floor with float variable (round down\/truncate the precision part). How can i do it correctly? package main import ( &quot;fmt&quot; &quot;math&quot; ) func main() { var st float64 = 1980 var salePrice1 = st * 0.1 \/ 1.1 fmt.Printf(&quot;%T:%vn&quot;, salePrice1, salePrice1) ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-ieee-754-binary-floating-point-numbers-imprecise-for-money\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-05T17:57: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=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-ieee-754-binary-floating-point-numbers-imprecise-for-money\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-ieee-754-binary-floating-point-numbers-imprecise-for-money\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] IEEE 754 binary floating-point numbers imprecise for money\",\"datePublished\":\"2022-11-05T17:57:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-ieee-754-binary-floating-point-numbers-imprecise-for-money\/\"},\"wordCount\":133,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"go\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-ieee-754-binary-floating-point-numbers-imprecise-for-money\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-ieee-754-binary-floating-point-numbers-imprecise-for-money\/\",\"name\":\"[Solved] IEEE 754 binary floating-point numbers imprecise for money - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-11-05T17:57:12+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-ieee-754-binary-floating-point-numbers-imprecise-for-money\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-ieee-754-binary-floating-point-numbers-imprecise-for-money\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-ieee-754-binary-floating-point-numbers-imprecise-for-money\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] IEEE 754 binary floating-point numbers imprecise for money\"}]},{\"@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=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] IEEE 754 binary floating-point numbers imprecise for money - 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-ieee-754-binary-floating-point-numbers-imprecise-for-money\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] IEEE 754 binary floating-point numbers imprecise for money - JassWeb","og_description":"[ad_1] The original question: Incorrect floor number in golang I have problem when use Math.Floor with float variable (round down\/truncate the precision part). How can i do it correctly? package main import ( \"fmt\" \"math\" ) func main() { var st float64 = 1980 var salePrice1 = st * 0.1 \/ 1.1 fmt.Printf(\"%T:%vn\", salePrice1, salePrice1) ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-ieee-754-binary-floating-point-numbers-imprecise-for-money\/","og_site_name":"JassWeb","article_published_time":"2022-11-05T17:57:12+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-ieee-754-binary-floating-point-numbers-imprecise-for-money\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-ieee-754-binary-floating-point-numbers-imprecise-for-money\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] IEEE 754 binary floating-point numbers imprecise for money","datePublished":"2022-11-05T17:57:12+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-ieee-754-binary-floating-point-numbers-imprecise-for-money\/"},"wordCount":133,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["go"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-ieee-754-binary-floating-point-numbers-imprecise-for-money\/","url":"https:\/\/jassweb.com\/solved\/solved-ieee-754-binary-floating-point-numbers-imprecise-for-money\/","name":"[Solved] IEEE 754 binary floating-point numbers imprecise for money - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-05T17:57:12+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-ieee-754-binary-floating-point-numbers-imprecise-for-money\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-ieee-754-binary-floating-point-numbers-imprecise-for-money\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-ieee-754-binary-floating-point-numbers-imprecise-for-money\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] IEEE 754 binary floating-point numbers imprecise for money"}]},{"@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=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","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\/19240","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=19240"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/19240\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=19240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=19240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=19240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}