Increment bug in repeat loops
Jacques Hausser
jacques.hausser at unil.ch
Sat Nov 21 02:39:24 EST 2009
Le 21 nov. 2009 à 05:13, stephen barncard a écrit :
> but it works if you set the numberformat to match
Yes Stephen, if the computation stays inside the loop, but not for the computation on the counter itself ! Try
on mouseUp
put "" into fld "Result"
set the numberformat to "#.##################"
repeat with i = 0 to 1 step 0.1
put i & return after fld "Result"
end repeat
set the numberformat to "#.#"
repeat with i = 0 to 1 step 0.1
put i & return after fld "Result"
end repeat
end mouseUp
One step too much in each loop ! The 12th and last one gives 1.099999999999999867 for the first loop, 1.1 for the second one.
I guessed it is because the counter computation and test are never subject to computation (outside the repeat process) and put into a container, so the numberformat has no effect on it. I tried to modify i inside the second loop:
set the numberformat to "#.#"
repeat with i = 0 to 1 step 0.1
put i + 1 - 1 into dummy
put dummy & return after field "Result"
put dummy into i
end repeat
and with this solution it works as expected, 11 steps, the last one giving 1 ! But still, it is usually better to limit counter and increment to integers...
Happy WE
Jacques
******************************************
Prof. Jacques Hausser
Department of Ecology and Evolution
Biophore / Sorge
University of Lausanne
CH 1015 Lausanne
please use my private address:
6 route de Burtigny
CH-1269 Bassins
tel/fax: ++ 41 22 366 19 40
mobile: ++ 41 79 757 05 24
E-Mail: jacques.hausser at unil.ch
*******************************************
More information about the use-livecode
mailing list