Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: rounding error


From   Maarten Buis <[email protected]>
To   [email protected]
Subject   Re: st: rounding error
Date   Tue, 20 Dec 2011 15:32:46 +0100

On Tue, Dec 20, 2011 at 2:57 PM, Richard Fox <[email protected]> wrote:
> The results are generally as expected but sometimes the rounding does not seem to work.
>
> I capture the information I need as in the following example (this is just a small extract from the program, obviously there are argument definitions and tabulations etc.);
>
> local acc = .1
> qui su `x' if New`v' == Tnam[`i',1] & New`w'==Wnam`i'[`j',1],d
> local Median_`i'_`j' = round(r(p50),`acc')
>
> The results I obtain (see 2 examples below) often have values that have many decimal places which appears to suggest that the rounding is not working. However if I run a basic summary of the particular variable the results I get do not have the numerous decimal places so I'm not sure if the issue is with the rounding statement. Any ideas?

The problem is that computers store numbers in binary, and a number
like 84.3 in binary is like 1/3 in decimal; you cannot store it in a
finite number of digits. There are nice discussions on this topic at
the Stata blog:

http://blog.stata.com/2011/02/02/how-to-read-the-percent-21x-format/
http://blog.stata.com/2011/02/10/how-to-read-the-percent-21x-format-part-2/
http://blog.stata.com/2011/06/17/precision-yet-again-part-i/
http://blog.stata.com/2011/06/23/precision-yet-again-part-ii/

There has also been a nice article in the Stata Journal:
William Gould (2006) Mata Matters: Precision. The Stata Journal. 6(4):550-560.
http://www.stata-journal.com/article.html?article=pr0025

One solution is to realize that macros contain strings, and a computer
has no trouble storing the string "84.3", even though it cannot
exactly store the numer that string represents. Here the extended
macro function -display- can be helpful, for more see -help
extended_fcn- (*). So if you want to "round" a number like 84.34 to to
84.3 and store it as a string you type:

local rounded: display %3.1f 84.34

Hope this helps,
Maarten

(*) In practice I always type -help macro- and than click on the link
to extended macro functions.

--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany


http://www.maartenbuis.nl
--------------------------

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index