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: trying to round values and store in a macro, problem with double/floating precision number storage


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: trying to round values and store in a macro, problem with double/floating precision number storage
Date   Thu, 5 May 2011 19:22:04 +0100

There is yet a further bug, although it has been removed for other
reasons. If the sd is put into a variable and then -summarize-d, it
will not be returned in r(sd), but rather in r(mean). Woolton should
look out for such bugs elsewhere in his code.

Nick

On Thu, May 5, 2011 at 7:19 PM, Nick Cox <[email protected]> wrote:
> Maarten gave positive advice but some pitfalls here deserve more explanation.
>
> First, although -round(,)- allows non-integer second arguments, most
> are useless or in practice misused.
>
> In fact I would say that the Stata documentation is very misleading on
> this point. Tthe help for -round()- gives as an example (in Stata
> 10.1; I am away from my main machine)
>
> "The rounding definition is generalized for y != 1.  With y = .01, for
> instance, x is rounded to two decimal places; round(sqrt(2),.01) is
> 1.41."
>
> This just perpetuates a fallacy that is elsewhere painfully explained.
> There can be no sense in which sqrt(2) _is_ 1.41 as such a number
> cannot be held exactly in Stata or indeed any machine based on binary
> arithmetic.
>
> It is true that e.g.
>
> . di round(sqrt(2),.01)
> 1.41
>
> but that is at least in part because the default display format works
> as you want here.
>
> When people want to show a given number of decimal places, there is
> only one way to do it directly and reliably: use an appropriate
> display format. -round()- works poorly if at all for such purposes.
>
> Second, putting a numeric constant into a variable and then using
> -summarize- to get it out again is unnecessary in two senses. Maarten
> gave alternative code, but even setting aside the misuse of -round()-
> these seven lines can be reduced to two:
>
>               local mn`v'_snh`i' = round(r(mean),0.01);
>               local std`v'_snh`i' = round(r(sd),0.01);
>
> On Thu, May 5, 2011 at 4:44 PM, Woolton Lee <[email protected]> wrote:
>
> [snip]
>               gen float mn = round(r(mean),0.01);
>               gen float sd = round(r(sd),0.01);
>               sum mn;
>               local mn`v'_snh`i'= r(mean);
>               sum sd;
>               local std`v'_snh`i' = r(sd);
>               drop mn sd;
>
> [snip]
>
>> This loop creates the variables mn and sd and rounds the result of the
>> numbers I want to two decimal places then uses summarize to store
>> these values into macros.  I wonder if there is another more efficient
>> way to do this?  I originally was doing all of the computations with
>> only macros but I found that I could not round to two decimals without
>> running into problems where a number like 55.87000000000000000001
>> would show up in my tables.  Is there anyway to make this work doing
>> the computations with just macros?
>

*
*   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