Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #11193
| 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> (permalink) |
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Reply-To | Thomas 'PointedEars' Lahn <cljs@PointedEars.de> |
| 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 | <v3rmi7l1i7ji615ltsn7mncfsh2tf6ip2k@4ax.com> <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 |
Followups directed to: comp.lang.javascript
Show key headers only | View raw
Tom de Neef wrote:
> "Gene Wirchenko" <genew@ocis.net>
>> 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:
<http://docs.oracle.com/javase/1.5.0/docs/api/java/math/BigDecimal.html>
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).
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Fun With Numbers Gene Wirchenko <genew@ocis.net> - 2012-02-02 21:23 -0800
Re: Fun With Numbers "Tom de Neef" <tdeneef@qolor.nl> - 2012-02-03 10:29 +0100
Re: Fun With Numbers Gene Wirchenko <genew@ocis.net> - 2012-02-03 11:57 -0800
Re: Fun With Numbers "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-02-03 20:25 +0000
Re: Fun With Numbers Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-04 15:12 +0100
Re: Fun With Numbers Dr J R Stockton <reply1205@merlyn.demon.co.uk> - 2012-02-04 23:42 +0000
csiph-web