Floating Point , Wide zero etc
- From: "Peter McMurray" <excalibur21@xxxxxxxxxxx>
- Date: Fri, 03 Jul 2009 20:54:46 GMT
Hi
I have separated this explanation out of the previous thread for clarity.
Pick Basic may not be the language of choice for
calculating a lunar modules trajectory, it is however excellent for Fra Luca
Pacioli's double entry book-keeping which has an
absolute requirement for arithmetic accuracy.
There are three things that affect this accuracy in a Pick program. They
are ICONV/OCONV, Precision and Floating point
arithmetic.
A short example demonstrates the first two.
001 EXECUTE "CLEAR-FILE DATA TEST"
002 OPEN '','TEST' TO TESTF ELSE CRT "BOOM NO TEST";STOP
003 PI = 22/7
004 FOR A = 1 TO 4
005 REC = "MD":A
006 REC<2> = ICONV(PI,REC)
007 REC<3> = OCONV(REC<2>,REC)
008 WRITE REC ON TESTF,A
009 NEXT A
010 REC = "VANILLA"
011 REC<2> = PI
012 WRITE REC ON TESTF,"VANILLA"
013 EXECUTE "SORT TEST A1 A2 A3"
:RUN MMBP TRY
Page 1 TEST 06:10:05 04 Jul 2009
TEST...... A1................... A2................... A3...................
1 MD1 31 3.1
2 MD2 314 3.14
3 MD3 3143 3.143
4 MD4 31428 3.1428
VANILLA VANILLA 3.1428
[405] 5 items listed out of 5 items.
The default precision of 4 is used. If we now insert a PRECISION 6
statement
Page 1 TEST 06:20:11 04 Jul 2009
TEST...... A1................... A2................... A3...................
1 MD1 31 3.1
2 MD2 314 3.14
3 MD3 3143 3.143
4 MD4 31429 3.1429
VANILLA VANILLA 3.142857
[405] 5 items listed out of 5 items.
We have now introduced an accounting anomaly, the same code has produced a
different answer in raw data mode amd a slightly
different answer in MD4. The latter is very important to me as oil pricing
is done at 4 decimal places and the tax office
require 6 places for GST calculation. All of which have to be rounded to
decimal 2 for the final invoice value.
The simple rule is never store unconverted data for accounting. My
preference is to use ICONV for all dates and accounting
data; whatever one does, and it is normally decimal 2 for invoice values, be
consistent.
The next step is Floating Point, an area fraught with danger that can
produce the infamous wide zero and has some known
anomalies in the basic algorithms. Please use it for actuarial calculations
of the "value" of derivatives but steer well
clear for normal accounting.
Floating Point forces the use of large binary numbers for the calculations.
If we consider a normal file with a 2 decimal component there are 100
possible values for that component. All well and good
in decimal but there is no direct binary equivalent for many of the values
0.01 + 0.02 + 0.03 is fine but if we take
o.66
..10101000111101011100001010001111 the binary approximation converts back to
..659999999916181
and with
o.77
..11000101000111101011100001010001 the binary approximation converts back to
..769999999785796
it is not difficult to see how over a large data set a simple addition can
rapidly introduce a decimal component slightly
smaller than anticipated.
Say we are expecting nnnnnnn.13 and we have nnnnnnnn.1297 All that has
happened is that the default PRECISION of 4 has taken
a number that looks something like nnnnnnnnn.1297399999etc and given us the
precise figure requested. If we had converted
the figure with ICONV(RESULT,MD2) we would have what we required.
The simple solution is always store results in the converted format and to
avoid anomalies creeping in over a large data set
always do the conversion at each step of the calculation
Peter McMurray
.
- Follow-Ups:
- Re: Floating Point , Wide zero etc
- From: Albert D. Kallal
- Re: Floating Point , Wide zero etc
- From: Albert D. Kallal
- Re: Floating Point , Wide zero etc
- From: Tony Gravagno
- Re: Floating Point , Wide zero etc
- Prev by Date: Re: D3/Linux and Flash Basic
- Next by Date: Re: D3/Windows 7.5.5 Released
- Previous by thread: your perception of class
- Next by thread: Re: Floating Point , Wide zero etc
- Index(es):
Relevant Pages
|