Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down on April 23, and its replacement, **statalist.org** is already up and running.

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

From |
Nick Cox <njcoxstata@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: rounding error |

Date |
Tue, 20 Dec 2011 16:52:55 +0000 |

Maarten makes the main point, which is a VFAQ on this list. In addition, a detail with -display- is that if no format is specified, -display- tends to round fairly intelligently. This is usually a feature, but conversely -display- without a format is not a reliable guide to what a number "really" is. -round()- can have two arguments, and it is certainly useful on occasion to be able to do things like -round(x, 5)- or -round(x, 10)-. But it is hard to see that rounding to a fractional argument serves _any_ useful purpose. Certainly there have many posts over several years in which it is evident that people think that -round(x, 0.1)- is, or should be, the way to get display to 1 decimal place. That is the job of formats, not rounding functions. Nick On Tue, Dec 20, 2011 at 2:32 PM, Maarten Buis <maartenlbuis@gmail.com> wrote: > On Tue, Dec 20, 2011 at 2:57 PM, Richard Fox <r.p.fox@bham.ac.uk> 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. * * 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/

**References**:**st: rounding error***From:*Richard Fox <r.p.fox@bham.ac.uk>

**Re: st: rounding error***From:*Maarten Buis <maartenlbuis@gmail.com>

- Prev by Date:
**st: RE: Printing viewer contents without cutting off the right-hand-side of text** - Next by Date:
**Re: st: Ordering a varlist according to "parent" and "child" variables** - Previous by thread:
**Re: st: rounding error** - Next by thread:
**st: Dynamic predictions with panel data?** - Index(es):