Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: -forvalues- fails to reach upper limit of range


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: -forvalues- fails to reach upper limit of range
Date   Wed, 3 May 2006 20:30:55 +0100

This is the old precision issue again in 
another guise. Search for precision in
the FAQs and the manuals. 

Once more from the top: 

	You think decimal. 
	Stata thinks binary. 
	Sometimes, they just can't match exactly. 

(We need a rhyming version of this, and
a catchy tune, so we can all sing it at
users' meetings. On the other hand, perhaps
not.) 

I tried this: 

. forval i = 1(.1)2 { 
  2. di %21.18f `i'
  3. }
 1.000000000000000000
 1.100000000000000100
 1.200000000000000000
 1.300000000000000000
 1.399999999999999900
 1.500000000000000000
 1.600000000000001000
 1.700000000000001100
 1.800000000000000900
 1.900000000000001000

. forval i = 1(.1)2 { 
  2. di %21x `i'
  3. }
+1.0000000000000X+000
+1.199999999999aX+000
+1.3333333333333X+000
+1.4cccccccccccdX+000
+1.6666666666666X+000
+1.8000000000000X+000
+1.999999999999eX+000
+1.b333333333338X+000
+1.cccccccccccd1X+000
+1.e66666666666bX+000
 
Onnly when your macro is 1.0 and 1.5 
can Stata hold the value you want exactly. 
This is a consequence of the fact that 
10 is not a power of 2. 

In general, you will be better off cycling 
over integers, and then stepping towards
what you want _inside_ the loop. 

Nick 
[email protected] 

Richard Sherman
 
> Using Stata 8.2, I am having trouble repeating a process the correct  
> number of times using -forvalues-.  The range is specified as (e.g.)
> 
> forval i = 1(.1)2
> 
> Here, 2 is the upper limit of the range.  But the final operation  
> involving `i' ends at 1.9 rather than 2.
> 
> A simple example follows.  The second -forval- encloses a command to  
> put the value of the counter `i' in a variable repeatedly.  
> The first  
> -forval- causes this to repeat for different upper limits of the  
> inner -forval- loop.  The output is a summary of the resulting  
> variables.  Note the maximum values.  I expect the maximum value to  
> be 1, then 2, then 3, etc.  

> 
> . forval u = 1/10 {
>    2.         clear
>    3.         set obs 100
>    4.         gen x = .
>    5.         forval i = 1(.1)`u'             {
>    6.                 local n = float(10*(`i'))
>    7.                 qui replace x = `i' in `n'
>    8.                 }
>    9.         su
> 10. }
> obs was 0, now 100
> (100 missing values generated)
> 
>      Variable |       Obs        Mean    Std. Dev.       Min  
>       Max
> -------------+--------------------------------------------------------
>             x |         1           1           .          1  
>         1
> obs was 0, now 100
> (100 missing values generated)
> 
>      Variable |       Obs        Mean    Std. Dev.       Min  
>       Max
> -------------+--------------------------------------------------------
>             x |        10        1.45     .302765          1  
>       1.9
> obs was 0, now 100
> (100 missing values generated)
> 
>      Variable |       Obs        Mean    Std. Dev.       Min  
>       Max
> -------------+--------------------------------------------------------
>             x |        20        1.95     .591608          1  
>       2.9
> obs was 0, now 100
> (100 missing values generated)
> 
>      Variable |       Obs        Mean    Std. Dev.       Min  
>       Max
> -------------+--------------------------------------------------------
>             x |        30        2.45    .8803408          1  
>       3.9
> obs was 0, now 100
> (100 missing values generated)
> 
>      Variable |       Obs        Mean    Std. Dev.       Min  
>       Max
> -------------+--------------------------------------------------------
>             x |        41           3    1.197915          1  
>         5
> obs was 0, now 100
> (100 missing values generated)
> 
>      Variable |       Obs        Mean    Std. Dev.       Min  
>       Max
> -------------+--------------------------------------------------------
>             x |        51         3.5    1.486607          1  
>         6
> obs was 0, now 100
> (100 missing values generated)
> 
>      Variable |       Obs        Mean    Std. Dev.       Min  
>       Max
> -------------+--------------------------------------------------------
>             x |        61           4    1.775293          1  
>         7
> obs was 0, now 100
> (100 missing values generated)
> 
>      Variable |       Obs        Mean    Std. Dev.       Min  
>       Max
> -------------+--------------------------------------------------------
>             x |        71         4.5    2.063977          1  
>         8
> obs was 0, now 100
> (100 missing values generated)
> 
>      Variable |       Obs        Mean    Std. Dev.       Min  
>       Max
> -------------+--------------------------------------------------------
>             x |        81           5    2.352658          1  
>         9
> obs was 0, now 100
> (100 missing values generated)
> 
>      Variable |       Obs        Mean    Std. Dev.       Min  
>       Max
> -------------+--------------------------------------------------------
>             x |        91         5.5    2.641338          1  
>        10
> 
> .
> end of do-file

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index