[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

LOGO-L> Floating point errors



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
caggiano@atlantic.net
http://www.atlantic.net/~caggiano
---------------------------------------------------------------
Please post messages to the Logo forum to logo-l@gsn.org.  Mail
questions about the list administration to logofdn@gsn.org.  To
unsubscribe send    unsubscribe logo-l    to majordomo@gsn.org.



Global SchoolNet Foundation - Linking Kids Around the World!
Copyright GSN - All Rights Reserved - Comments & Questions
Visit GSN's Global Schoolhouse for more exciting learning resources!
Search our Site - Home