I want to loop over values d=0(0.01)0.2 and d2= 0.80(0.01)1
I already know that I should only loop over integers and then transform the looping local within the loop.
To identify outputs in my logfile, I call Stata to display the actual value of the looping local and thereby remarked that the loopin local does not always have the value it should have.
Code:
tempname d d2 forvalues dd = 0(1)20{ scalar `d' = `dd'/100 forvalues dd2 = 80(1)95{ scalar `d2' = `dd2'/100 di "*****************************************************************************" di "*** delta=`=`d'', delta2=`=`d2'' *********" di "--------------------------------------------------------------------------------------" } }
For the loop over dd2 = 80(1)100, I get two strange outputs displayed:
*** delta=`=`d'', delta2=.8100000000000001 *********"
*** delta=`=`d'', delta2=.9399999999999999 *********"
Though it obviously should be 0.81 and 0.94, respectively.
Therefore, I
(1) tried to reformulate the loop, mainly changing order of the loops and naming of the locals, with the same result.
(2) restarted the machine and tried again - same result
(3) took my other machine: I get slightly different output, but still not satisfactory:
delta2=.9300000000000001 and delta2=8100000000000001.
And again, no problems with the loop over dd=0(1)20.
(4) BUT when I differently call for displaying the value of the looping local, I get the right AND the wrong numbers.
Code:
tempname d d2 forvalues dd = 20(1)20{ scalar `d' = `dd'/100 forvalues dd2 = 80(1)95{ scalar `d2' = `dd2'/100 di "*****************************************************************************" di "*** delta=`=`d'', delta2=`=`d2'' *********" di "--------------------------------------------------------------------------------------" di "delta2= `d2' " di "delta2= `=`d2'' " di "delta2= `=`dd2'/100'" di "delta2= `dd2'/100 " } }
I would really love to know
(1) why do I get the odd numbers, though I loop over integers?
(2) why do I get the odd numbers always for the same value of the looping local, but depending on the machine I use?
(3) why do I get contradictory outputs for the same value of the looping local, depending on how I ask to display it?
(4) what could I do to solve this issue?
Thank you in advance for your precious time.
Susanne
NB: I use Stata 15.1
0 Response to forvalues: unexpected value of a looping local: 81/100=.8100000000000001
Post a Comment