Statalist


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

Re: st: Binary representation and number precision


From   Maarten buis <[email protected]>
To   [email protected]
Subject   Re: st: Binary representation and number precision
Date   Sun, 13 Apr 2008 11:33:19 +0100 (BST)

--- Nicolas Van de Sijpe <[email protected]> wrote:
> I�m using Stata for a relatively elaborate data construction effort.
> To guard against mistakes, at various stages in the data construction
> I want to check whether certain combinations of variables 
> (constructed from the raw data) yield zero. But when doing so I run
> into problems because of Stata�s way of storing numbers. Consider the
> following example:> 
> . set obs 1
> obs was 0, now 1
> 
> . gen var1 = 6.1
> 
> . gen var2 = 6
> 
> . gen var3 = 0.1
<snip>
> Basically my question is: what is the best way to get around this?

If your variables all have a fixed number of digits behind the decimal
point, like in your example they all are known upto one digit behind
the decimmal point, then you can multiply all variables by 10, 100,
1000, depending on the number of digits, to make them integers.
Integers are stored precisely. Once you have finished your data
construction you can turn them back into your original metric. I
learned this trick from (Gould 2006).

drop _all
set obs 1
gen var1 = 61
gen var2 = 60
gen var3 = 1
assert var1 - var2 - var3 == 0

Also note the use of -assert- as a way to check if a claim about your
data is actually true. For more on -assert- see: (Gould 2001, 2003).

Hope this helps,
Maarten

William Gould (2001) "Statistical software certification", The Stata
Journal, 1(1): 29--50.
http://www.stata-journal.com/article.html?article=pr0001

William Gould (2003) "Stata tip 3: How to be assertive", The Stata
Journal, 3(4): 448.
http://www.stata-journal.com/article.html?article=dm0003

William Gould (2006) "Mata matters: Precision", The Stata Journal,
6(4):550--560
http://www.stata-journal.com/article.html?article=pr0025

-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology
Vrije Universiteit Amsterdam
Boelelaan 1081
1081 HV Amsterdam
The Netherlands

visiting address:
Buitenveldertselaan 3 (Metropolitan), room Z434

+31 20 5986715

http://home.fsw.vu.nl/m.buis/
-----------------------------------------


      ___________________________________________________________ 
Yahoo! For Good helps you make a difference  

http://uk.promotions.yahoo.com/forgood/
*
*   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