[clean-list] Functions that look the same

Arjen arjenw@sci.kun.nl
Thu, 19 Jul 2001 10:05:37 +0200 (MET DST)


| Hi all,
| I have a question about why these two expressions give different
| results?
|
| reduce:: (Real -> Real)
| reduce = \x->x/10.0
|
| Start = until ((==)0.0 o \x->x/10.0) reduce 1.0
| will evaluate to 0
| while
| Start = until ((==)0.0 o reduce) (reduce) 1.0
| will evaluate to  0e-324
|
| This was done on win98 using Clean 1.3.3
|
| TIA
| Lloyd Smith
|
Such problems with precision are usually caused by the limited precision of
floating point numbers on computers in general and the Real datatype in
Clean specifically, which is only 8 bytes in size. Compiler optimizations
can also influence the result by changing a division by a multiplication
with the reciprocal, which could be the case in the above example.
Given the imprecision of Reals 0e-324 equals 0, or the program would
not have stopped with 0e-324.

I have not tested this, but if you use x/2.0 this problem will probably
not occur since 0.5 can be perfectly represented by most floating point
formats, the same goes for any other power of two. 0.1 can only be
approximated.

Overall, one should be aware of these imprecision issues when using
floating point calculations. I recommend you use (abs x) < epsilon
with epsilon sufficiently small instead of x == 0, regardless of which
programming language you use.

hope this helps,
	Arjen van Weelden.