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

# Re: st: rounding the minimum of a negative number

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: rounding the minimum of a negative number Date Wed, 9 Jan 2013 21:57:52 +0000

```As should not surprise you, much of what you identify or circle around
has been often and lengthily discussed under the heading of precision.

The best starting point is now William Gould's blog entry

http://blog.stata.com/2012/04/02/the-penultimate-guide-to-precision/

and that, various Stata Journal articles and various FAQs are all locatable by

. search precision

in an up-to-date Stata.

The bottom lines here, which I put at the top, are that

1. At machine level Stata's calculations are necessarily in binary.
Stata and most modern software go to enormous lengths to make it seem
otherwise, but the fact is inescapable, ineluctable and inevitable.

2. Many surprises to users in handling of decimals turn out not to be
surprises when the consequences of #1 are thought through carefully.

3. In particular, exact binary approximations to numbers that
expressed in decimals have few decimal places are elusive and
exceptional.

In your case, you want to round down but not to integer results but to
exact decimal fractions. -floor()- and -ceil()- in Stata take only one
argument, the number to be rounded. Somewhere in StataCorp's files a
suggestion of mine that a second argument be allowed for these
functions -- matching -round()- -- is gathering dust undisturbed.

The best you can do, I believe, is indeed something like

gen double myresult = floor(100 * mydata) / 100

You mention r(min) in your example; presumably that arises from some
previous calculation. It is not a general solution.

Nick

P.S. Your last example brings in a different point. `r(min)' is
related to r(min) as local macros are to scalars. Macros hold less
precision than scalars. For maximum precision, avoid the numeric --
string -- numeric cycling required by putting a numeric result into a
local macro and then taking it out again. (This won't bite with small
or moderate integers.)

On Wed, Jan 9, 2013 at 7:59 PM, annoporci <annoporci@gmail.com> wrote:

> This is a problem for which I have found a solution. I am sending this to
> Statalist for the record and/or further discussions if warranted.
>
> I have data whose minimum is something like -1.9810 which I want to
> "transform" into -1.9900. By "transform" I mean that I want to "floor" it
> to the nearest smaller rational number with 2 non-zero decimal digits.
>
> The -round(x,y)- function takes 2 arguments that allow you to select the
> precision of the rounding. But the rounding is to the nearest greater
> rational number. The -floor- function takes only 1 argument and "floors"
> to a natural number without decimal digits. Or that is what I gather from
> Stata help. Perhaps there is some undocumented extension to floor or some
> more general function by another name?
>
> Thus, using -round- on -1.9810 yields -1.98. There does not seem to be an
> option to make -round- act like -floor-. Here is my workaround to this
> shortcoming (rather obvious, I know):
>
> scalar m = floor(100*r(min))/100
>
> Is that the state of the art?
>
> I must admit that along the way, I observed some unexpected behaviour,
> namely:
>
> set obs 10
> gen x = -1.98100
> sum(x), meanonly
> scalar min1 = "`r(min)'"
> scalar min2 = r(min)
> scalar list
>               min2 = -1.9809999
>               min1 = -1.980999946594238
>
> I did not expect -1.98100 to "become" (or appear so): -1.980999 or
> -1.980999946594238.
>
> It looks like something to do with the idea that -1.98100 and -1.98109 are
> treated as the same number at the level of precision Stata assumes I'm
> interested in.
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```