At 10:11 AM Friday 9/22/2006, Klaus Stock wrote:

OTOH, consider the following Smalltalk code:
   x := 1 / 3.
   x := 3 * x.
   x inspect.

Common sense tells us that the result is 0.9999999 - but Smalltalk insists
on 1.


Funny, that's exactly the example many books used 30-odd years ago to illustrate why round-off error is a problem programmers have to keep in mind, as mathematically division by three and multiplication by three should be inverse operations, so

        X = 1
        Y= X/3
        Z = 3*Y
        IF (Z=X) THEN GOTO 10
        PRINT (Z " IS NOT EQUAL TO " X)
        GOTO 20
10      PRINT (Z " IS EQUAL TO " X)
20      END

would always return something like "0.999999 IS NOT EQUAL TO 1" so if you expected X and Z to be equal (as it would be in mathematics or infinite-precision arithmetic) and were testing for that, it would never be equal.

(In FORTRAN¹, it would be even worse if you forgot the difference between integer arithmetic and real arithmetic, as I = 1/3 would set I = 0 and then J = 3*I would make J = 0.)

_____
¹Yes, this dates me to the same era as Himself. (Even though I recently re-installed the Fortran 90 package on this machine.) I probably still have some boxes of cards from those days somewhere in the storage shed . . . another benefit (?) of inheriting the house you lived in back then from your parents . . . )


-- Ronn!  :)



_______________________________________________
http://www.mccmedia.com/mailman/listinfo/brin-l

Reply via email to