Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.straub-nv.de!uucp.gnuu.de!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <1785564.ahg88INnCG@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Sat, 04 Feb 2012 15:12:27 +0100 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 8Bit Subject: Re: Fun With Numbers Newsgroups: comp.lang.javascript Followup-To: comp.lang.javascript References: <4f2ba914$0$6858$e4fe514c@news2.news.xs4all.nl> Supersedes: <1935665.aqSPkdTlGX@PointedEars.de> MIME-Version: 1.0 Lines: 55 NNTP-Posting-Date: 04 Feb 2012 15:12:27 CET NNTP-Posting-Host: 9e9bd3c6.newsspool1.arcor-online.net X-Trace: DXC=\F2AG32ZVejOKO]LCQ@0g`ic==]BZ:afn4Fo<]lROoRa<`=YMgDjhgb5EF936Ae=FdDZm8W4\YJNlb@mF9jNikdePcfN;IC;G;cnQEATOK^Taf X-Complaints-To: usenet-abuse@arcor.de Xref: x330-a1.tempe.blueboxinc.net comp.lang.javascript:11193 Tom de Neef wrote: > "Gene Wirchenko" >> I need to do work with fixed-decimal quantities (mainly dollar >> amounts but others as well). I need to be able to do reliable >> arithmetic with them. >> >> 0.1 + 0.1 + 0.1 equals 0.15 + 0.15 mathematically, but not with >> floating point. I need to have it equal. >> >> So I am cheating. I am storing fixed-decimal amounts internally >> as integers. When I need to output a value, I scale it, but any >> arithmetic or comparison operations will be on the integers. > > Just wondering: how do you calculate 3,62% of ($4.56 + $14.13) and equal > it to 3,62% of $4.56 plus 3,62% of $14.13 ? > Or go from euro to dollar, etc ? You store the floating-point number n as an integer value (i.e., this.value % 1 = 0) and store the number of significant decimal digits of n (this.scale) along with it in an object. Then n = this.value × 10^(−this.scale). Of course, in order to do basic arithmetic with the object, you need special methods that account for the scale. That is, a.b + c.de ~ ab:scale1 + cde:scale2 ~ (ab × 10^(scale2 − scale1) + cde)):max(scale1, scale2) ~ (ab × 10^(scale2 − scale1) + cde) × 10^(−max(scale1, scale2)), and a.b × c.de ~ ab:scale1 × cde:scale2 ~ (ab × 10^(scale2 − scale1) × cde):(scale1 × scale2) ~ (ab × 10^(scale2 − scale1) × cde) × 10^(−scale1 × scale2) where scale1 <= scale2 (here: scale1 = 1, scale2 = 2). The idea is anything but new. For example, Java has had this for many years: The precision of an implementation of this in an ECMAScript implementation is limited by the precision for integer values (number values `i' with i % 1 == 0), as the unscaled value and the scale are still IEEE-754 floating-point values. But the precision of computation is indeed better than with plain floating-point values, that is 1:1 + 1:1 + 1:1 ~ 3:1 ~ 0.3. PointedEars -- When all you know is jQuery, every problem looks $(olvable).