Bookmark and Share

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]

Re: st: programming error with -generate-


From   wgould@stata.com (William Gould, StataCorp LP)
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: programming error with -generate-
Date   Tue, 09 Mar 2010 09:17:45 -0600

Richard Williams writes, 

> At 04:31 PM 3/8/2010, Visintainer, Paul wrote:
> >. gen y = 1.2x + 6   <----- why isn't there an error here?
> 
> When I do this, y = 72 for all cases.  So 1.2x must be getting 
> interpreted as 1.2 * 10 = 12.  I don't know why, but just follow 
> Martin's advice and make it
> 
> gen y = 1.2*x + 6
> 
> Maybe this is a "feature" but if so it doesn't seem like a good one, 
> since this could be an easy mistake to make.

That is, one should type 

        . gen y = 1.2*x + 6

but if one mistakenly omits the multiplication sign and types 

        . gen y = 1.2x + 6

then 72 is stored in y!

It has been noted that this odd behavior seemingly happens only when the
variable is named -x-, but that's not true.  Something similar is true of 
variables named -e-.  You mean to type 

        . gen y = 1.2*e + 6 

but you mistakenly omit the multiplication sign and type 

       . gen y = 1.2e + 6 

Make that mistake and 1,200,000 will be stored in y!

Well of course it will, you say.  1.2e+6 means 1.2*10^6.  

Well, I reply, 1.2x+6 means (1.2)-base-16 times 2^6, and that works out 
to (1+2/16)*2^6 = 72.  

That is, just as Stata has an e-notation for typing base-10 numbers, Stata has 
an x-notation for typing base-16 numbers.  I have mentioned before that Stata
has an output format %21x which will show you a floating-point number exactly
as the computer stores it.  I've used it in other posts, too, in explaining 
unexpected numeric results, usually caused by round-off error because some 
base-10 numbers such as 1.1 have no exact base-2 or base-16 representation.
Let's see what %21x shows us for the number 72:

        . display %21x 72
        +1.2000000000000X+006

It is documented that this x-notation is both an output and input format of 
Stata.   Thus, Stata understands that 1.2x+6 is 72, just as it understands 
that 1e+2 is 100.  By the way, it is a property of x-notation that the 
lead digit must be 1 unless the number is 0.  That's because x-notation is 
really a binary format.  Remember, the x+6 part means "multiply by 2^6.  The 
first part is typed in hex, but that's just for convenience.  What is out
front is interpreted as a binary number.

At StataCorp, we use x-notation to enter constants precisely and to ensure that
the constant will be the same constant across computers.  For instance, 
_pi is in fact defined as +1.921fb54442d18X+001.  We also use x-notation 
in the certification process to ensure that Stata converts base-10 numbers 
to base-2 correctly.  And sometimes we use it in certification to make sure 
that an answer is exactly, digit-by-digit, what we expected.

-- Bill
wgould@stata.com
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index