1. FUZZY decimal-loops and comparison
- Posted by a.tammer at hetnet.nl
Mar 29, 2002
Although I never (inf-1) use decimal loops I tried to cock-up a
solution for those who NEED to perform decimal-loops, by
introducing fuzziness to allow for FP-overaccuracy.
Put it in an .e(w/u)-file and off you go
Your test-lines should look like:
sequence fuzzyresult fuzzyresult={}
fuzzyresult=fuzzy(myvar,mylevel)
if testvar>=fuzzyresult[1] and testvar<=fuzzyresult[2] then
< mycode >
end if
**** CODE starts here *****
atom testvar,level
testvar=0 level=0
sequence result result={}
function fuzzy(atom testvar, atom level)
sequence fuzz
fuzz={}
if level=0 then
-- shouldn't occur !! ERROR !!
-- program your own ERR-handling here
else
if level < 0 then level = -level end if
fuzz &= testvar - level
fuzz &= testvar+level
return fuzz
end if
end function
--*************Testing .1 loop with fuzziness 1e-12*********************
-- FUZZ your value to the needed digit+1 precision .
result= fuzzy(139.4,1e-2)
? result[1]=result[2]
? result[2]-result[1]
? 127.4+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+
.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+
.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+
.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+
.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+
.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1=139.4
? 127.4+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+
.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+
.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+
.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+
.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+
.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1>=result[1] and
127.4+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+
.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+
.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+
.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+
.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+
.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1+.1<=result[2]
? 127.4+120*.1>=result[1] and 127.4+120*.1<=result[2]
atom USED USED=0
for t=1 to 10000000 do
USED +=.1
end for
-- USED now should evaluate to 1000000 (10 million iterations)
-- in this case seems to be the limit. For almost all purposes
-- this will be sufficient I guess.
USED += 127.4
result=fuzzy(1000127.4,1e-3)
? USED>=result[1] and USED<=result[2]
-- Behold: still testing true .
--FUZZY decimal-loop
integer iterations iterations=0
atom var1,var2,var3
var1=.0001
var2=10000
var3=.0001
USED=0
for t=var1 to var2+var3/10 by var3 do
iterations+=1
USED+=var3
end for
? iterations
result=fuzzy(var2,.00001) -- one decimal position more than var3
? USED
? USED>=result[1] and USED<=result[2]
-- FUNNY how USED showing as < 9999.999999 > still qualifies
-- as `EQUAL` to fuzzy(10000) which in fact stands for any value
-- between 9999.99999 and 10000.00001
?1/0
--************ CODE and tests END here ***********************
EUrs antoine tammer