
LOGO-L> Floating point errors
All the differences you see are not due to the language but
simply the print width. If you "Narrow" the print precision the
language will round to the nearest value which so happens to
be the right thing. You can experiment with the "printed
precision" by playing with FORM in MSWLogo/UCBLogo (see example
below).
All the example below is doing is hiding the error, nothing magic.
Even though the Java looks the worst It's probably the best
because it does not hide the error no matter how small.
But again this credit is simple how the print width defaults.
Don't confuse this with arbitrary precision math that some
languages offer. Either the language is limited to the HIGH
SPEED performance of the floating point processor or does
expensive arbitrary floating point precision math.
Both have uses.
I've always heard great things about SmallTalk but never took
the time to learn it :-(
to atest
(local "times "number)
make "number 0
for [j 1 6 1 ]~
[ make "times power 10 :j
type [times -> ]
print :times
for[i 1 :times 1]~
[make "number sum :number 0.01]
type [result -> ]
print form :number 10 3
make "number 0
]
end
atest
times ->10
result -> 0.100
times ->100
result -> 1.000
times ->1000
result -> 10.000
times ->10000
result -> 100.000
times ->100000
result -> 1000.000
times ->1000000
result -> 10000.000
Quote:
> Before this was put to rest I thought it might be interesting to see how
> some other
> languages handled the situation. Here is the routine in MSWLogo,
> MicroWorlds, Java and
> Smalltalk. I also have it running on an hp48SX but it hasn't finished
> yet.
> This was all run on a Win95 Pentium P75 with 32 Meg Memory.
> MSW Logo
> to atest
> (local "times "number)
> make "number 0
> for [j 1 6 1 ]~
> [ make "times power 10 :j
> type [times -> ]
> print :times
> for[i 1 :times 1]~
> [make "number sum :number 0.01]
> type [result -> ]
> print :number
> make "number 0
> ]
> end
> The results:
> times ->10
> result ->0.1
> times ->100
> result ->1
> times ->1000
> result ->9.99999999999983
> times ->10000
> result ->100.000000000014
> times ->100000
> result ->999.999999999236
> times ->1000000
> result ->10000.0000001719
> ***************************************
> MicroWorlds Logo
> to atest
> local "times
> local "number
> make "number 0
> dotimes [j 6]
> [ make "times power 10 sum :j 1 ;need this dotimes is zero based.
> print (se "times-> :times)
> dotimes [i :times]
> [make "number sum :number 0.01]
> print (se "result-> :number)
> make "number 0
> ]
> end
> The results:
> times-> 10
> result-> 0.1
> times-> 100
> result-> 1
> times-> 1000
> result-> 10
> times-> 10000
> result-> 100
> times-> 100000
> result-> 999.999999999
> times-> 1000000
> result-> 10000.0000002
> ******************************************************
> Java
> public class Atest {
> public static void main (String argv[]) {
> int j, i;
> double times, number = 0.0;
> for (j = 1; j < 7; j++) {
> times = java.lang.Math.pow (10, j);
> System.out.print("times:" );
> System.out.println(times);
> for (i = 1; i <= times; i++) {
> number = number + .01;
> }
> System.out.print("result:");
> System.out.println(number);
> number = 0;
> }
> }
> }
> The results:
> java Atest
> times:10.0
> result:0.09999999999999999
> times:100.0
> result:1.0000000000000007
> times:1000.0
> result:9.999999999999831
> times:10000.0
> result:100.00000000001425
> times:100000.0
> result:999.9999999992356
> times:1000000.0
> result:10000.000000171856
> ***************************************************
> SmallTalk (SmallTalk express)
> | num pow |
> 1 to: 6 do: [:j | pow := (10 raisedTo: j). Transcript show: pow
> printString; cr.
> num := 0.
> 1 to: pow do: [ :i | num := num + 0.01].
> Transcript show: num printString; cr.]
> 10.0
> 0.1
> 100.0
> 1.0
> 1000.0
> 10.0
> 10000.0
> 100.0
> 100000.0
> 1000.0
> 1000000.0
> 10000.0
> (Hope you all won't mind a small plug for SmallTalk :-) in this group )
> If your interested in really learning what OOP is all about you should
> check out SmallTalk.
> SmallTalk express is a free, small but complete SmallTalk system from
> Digitalk (ParcPlace, whatever. They merge and name change so often it's
> hard to keep track.) It has a nice class library and class browser plus
> a complete GUI builder. It also allows you to make standalone images.
> It's avaliable at http://www.objectshare.com/se/seinfo.htm
> I believe the original poster said something along the lines that they
> expected the 'correct answer' from a computer. Of course in an analog
> world one always needs to be suspicious of the answers given by a
> digital god.
> regards
> --
> Frank Caggiano
> http://www.atlantic.net/~caggiano
> ---------------------------------------------------------------
--
===============================================================
http://www.softronix.com/
The www page contains some very powerful educational software.
Our single most important investment is our kids.
---------------------------------------------------------------