Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: Re: Problem with replace if - test condition works only for some values of test variable


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Re: Problem with replace if - test condition works only for some values of test variable
Date   Fri, 24 Oct 2008 15:10:12 +0100

The precision issue is elaborated at length in several sources already
referred to you. 

. findit precision 

And look at the FAQs and Stata Journal articles there listed. 

The key point is that deep, deep down Stata is necessarily using binary
representations of decimal numbers. Some numbers can be held exactly,
some can't. So for example it is not correct either that "x actually
equals .2599999". 

A secondary point is whenever you use -display- it uses a default
format, so -display- used in that way cannot be _guaranteed_ to show
exactly how Stata holds numbers.  

Rather than recapitulating what's already been written up in detail in
various places, I encourage you to read, as earlier recommended. 

Nick 
n.j.cox@ 

Naunihal Singh

I think I understand a bit better what's going on.
replace y=1 if x==.26 fails because x actually equals .2599999
. di x[27]
.25999999
(this is strange b/c x was generated with this command: generate x=
(_n/100)-0.01 in 1/101 )
So the problem wasn't that float had too few digits of precision (both
.25 and .26 fit in the same number of bytes), it's that stata was
storing (27/100)-0.01 as .25999999 instead of .26.

I've tried using the round command to tackle this, but I must be doing
something wrong in the syntax:
replace x=round(x,0.01)
(0 real changes made)
even though
di round(.25999999, 0.01)
.26
Is there a problem with using round in replace in that way?


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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