Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #11195
| From | Dr J R Stockton <reply1205@merlyn.demon.co.uk> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Fun With Numbers |
| Date | 2012-02-04 23:42 +0000 |
| Organization | Home |
| Message-ID | <U45qnYbyJcLPFw+e@invalid.uk.co.demon.merlyn.invalid> (permalink) |
| References | <v3rmi7l1i7ji615ltsn7mncfsh2tf6ip2k@4ax.com> |
In comp.lang.javascript message <v3rmi7l1i7ji615ltsn7mncfsh2tf6ip2k@4ax.
com>, Thu, 2 Feb 2012 21:23:43, Gene Wirchenko <genew@ocis.net> posted:
> 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.
Dollars are integers. No problem, up to and including 2^53.
> 0.1 + 0.1 + 0.1 equals 0.15 + 0.15 mathematically, but not with
>floating point. I need to have it equal.
You only get assured exact results if all numbers, including
intermediates, can be expressed in fixed-point binary with the most
significant and least significant bits no more than about 53 buts apart.
But the PC FPU, and maybe others, can work with 64-bit mantissa numbers,
so "internal" intermediates could be more precise - which might or might
not be standards-compliant.
Aside : we should be able to think of a test for that/
If you want to do exact addition, subtraction, multiplication,
comparison with dollar-cent prices, work in cents.
> 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.
>
> My first thought was that I was safe for nine digits worth,
>because ECMAScript does a lot of 32-bit operations. That is on the
>edge of what I need. The non-JavaScript system that I maintain now
>has reports that generate nine digits worth in some reports.
The 32-bit operations are logical, not arithmetic. They are not needed
in calculating finance.
> I did some experimenting, and it appears that I might be able to
>get 15 digits of precision, but not 16. After determining this, I
>referred to the ECMAScript standard (ECMA-262 5.1 Edition of June
>2011) to see if this matched. That standard says that it uses the
>IEEE 754 floating point format, and some documentation on that says
>that it is good for 15.95 digits of precision.
Not entirely so. A single non-integer addition operation is good to
about that, depending on by how much the answer is below the nearest
power of two above. But subtraction of two inexactly-represented
quantities of similar magnitude loses precision.
Have you had the lesson on how to code the solutions of a potentially
ill-conditioned quadratic equation, concerning
-b +- root(b^2-4ac) / 2a // ???
> But am I safe?
>
> Can I count on exact arithmetic with integers of up to 15 digits
>of precision?
Only if you really understand what you are doing. And not always then,
since most arithmetic expressions involving division have ideal results
which cannot be exactly represented in an IEEE Double.
And if you're not using a P60's FPU.
> If yes, can you please point to a reference? If no, please give
>me a counterexample.
You could read my Web site, to reduce the frequency with which you
"discover" quite well-known wheels.
If it is necessary to reproduce the results which would be obtained by a
professional accountant using pre-computer equipment, then you are only
safe if you follow his methods exactly. That does not mean getting the
right answer, it means getting the same answer.
Happily, modern computers are so fast, and accounting is so simple, that
you^H^H^Hone can write an exact arbitrary-length decimal arithmetic
package without too much difficulty.
Via <http://www.merlyn.demon.co.uk/programs/00index.htm longcalc, for
example.
--
(c) John Stockton, nr London UK ?@merlyn.demon.co.uk IE8 FF8 Op11 Sf5 Cr15
news:comp.lang.javascript FAQ <http://www.jibbering.com/faq/index.html>.
<http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Back to comp.lang.javascript | Previous | Next — Previous 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