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: precision problem using double
From
Nick Cox <[email protected]>
To
[email protected]
Subject
Re: st: precision problem using double
Date
Sat, 15 Oct 2011 08:49:58 +0100
Try using r(max).
`r(max)' is a string representation of r(max) and uses decimal
characters only. For a fraction you could lose important detail.
Emphasis on "could". For testing equality, r(min) and r(max) are
advisable rather than `r(min)' and `r(max)', as the difference between
them can bite in that context.
For your kind of problem, -sort-ing the non-missings and selecting the
highest is another way to do it.
. set obs 10
obs was 0, now 10
. gen foo = runiform()
. su foo
Variable | Obs Mean Std. Dev. Min Max
-------------+--------------------------------------------------------
foo | 10 .4841855 .2786673 .0610638 .8714491
. l if foo == `r(max)'
+----------+
| foo |
|----------|
10. | .8714491 |
+----------+
. l if foo == `r(min)'
. l if foo == r(min)
+----------+
| foo |
|----------|
8. | .0610638 |
+----------+
Nick
On Sat, Oct 15, 2011 at 7:37 AM, Leandro Brufman
<[email protected]> wrote:
> Been reading some stuff on the precision of double, and how Stata
> reads numbers for calculation (in particular this article
> http://www.stata-journal.com/sjpdf.html?articlenum=dm0022)
> But couldn't find a solution. Here's my problem:
>
> sum spread
> br if spread==`r(max)'
>
> * it returns that there are no observations.
>
> sum spread
> br if spread-`r(max)'<smallestdouble()
>
> *it also returns that there are no observations
>
> sum spread
> gen test=spread-`r(max)'
>
> * the minimum number of variable test is -2.84217094e-14
>
> any ideas on how to fix this code? i don't need a lot of precision,
> but I do need to have these two lines working fine:
> sum spread
> br if spread==`r(max)'
*
* 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/