<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Sep 12, 2016, at 1:26 PM, Jens Alfke via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Sep 12, 2016, at 10:10 AM, Teej . via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="" style="font-family: Alegreya-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span class="Apple-tab-span" style="white-space: pre;">        </span>…in spite of the CPU’s quirks in handling floating point numbers in a maddening inaccurate manner.</div></div></blockquote><div class=""><br class=""></div><div class="">Well, in the CPU’s defense, it’s only inaccurate because the puny humans insist on dividing their currency into fractions of 1/10, which has no exact representation in binary. (Apparently this is an ancient tradition commemorating the number of bony outgrowths on certain extremities of their grotesque meat-bodies.) I could — I mean, <i class="">the computers </i>could — point out that if we divided our currency units into 7 pieces, our precious decimal numbers would quickly become inaccurate too. :)</div></div></div></div></blockquote><br class=""></div><div>Expanding a bit on what Jens wrote here: <b class="">decimal (unlike friendship) is not magic</b>. &nbsp;All computer models of real-number arithmetic are necessarily inexact, because (almost all) real numbers are not computable. &nbsp;There are a bunch of reasonable choices that one can make, however (this is not an exhaustive list, just a sampling of the design space):</div><div><br class=""></div><div><b class="">Binary floating point</b></div><div>Pro: represents modest integers exactly, extremely fast hardware implementations, fixed memory size, and rounding errors are extremely uniform—they don’t vary much with the number being represented.</div><div>Con: almost no decimal fractions have exact representations.</div><div><br class=""></div><div><b class="">Decimal floating point</b></div><div>Pro: represents modest integers and decimal fractions exactly, slower than binary but still faster than almost anything else, fixed memory size.</div><div>Con: at least an order of magnitude slower than binary floating-point, and rounding error is significantly less scale-invariant.</div><div><br class=""></div><div><b class="">Fixed-size rationals</b></div><div>Pro: represents all modestly-sized integers and fractions exactly, fixed memory size, four basic operations are exact until you hit the limits of representation.</div><div>Con: denominators quickly grow too quickly to be used for non-trivial computations (this is usually a deal-breaker).</div><div><br class=""></div><div><b class="">Arbitrary-precision rationals</b></div><div>Pro: closed under four basic operations, represents most numbers most people will use exactly.</div><div>Con: representations get extremely large extremely quickly, large memory footprint if you have more than a few numbers.</div><div><br class=""></div><div><b class="">Computable real numbers</b></div><div>Pro: any number you can describe, you can work with.</div><div>Con: your numbers are now computer programs, and you arithmetic system is Turing-complete. &nbsp;Testing for equality is equivalent to solving the halting problem.</div><div><br class=""></div><div><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><div class=""><blockquote type="cite" class="">Teej . via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</blockquote><blockquote type="cite" class=""><br class=""></blockquote></div></div></div></div><div><blockquote type="cite" class="">It still does not answer my question on why we can’t just provide a decimal data type.</blockquote><br class=""></div><div>The only problem here is the “just”. &nbsp;We can and will provide a decimal data type. &nbsp;Like many other language changes, that we can and will do, it requires engineering time, and there are finite engineering resources available to work on it.</div><div><br class=""></div><div>Keep in mind that decimal will not magically solve all accuracy problems, however. &nbsp;1/3 is just as inaccurate in decimal as it is in binary floating-point (actually, it’s less accurate in decimal than in a comparable binary format due to the aforementioned non-uniformity of decimal rounding error).</div><div><br class=""></div><div>– Steve</div></body></html>