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

# Re: st: Why the square of a negative number is still negative

 From "Joao Ricardo F. Lima" To statalist@hsphsun2.harvard.edu Subject Re: st: Why the square of a negative number is still negative Date Fri, 16 Dec 2011 13:38:50 -0300

```Tiago,

from the NC151 - "An introduction to Stata Programming":

"Because, when Stata is executing lines, it does the following:

1. substitutes for any and all macros
2. executes the line

If the value contained in the macro b happened to be negative, the
operator precedence would have evaluated the power operator first and
then the negation operator. So, macros are not the same as variables
in other programming languages. They are exactly like macros in the C
language in this sense. Ensuring the proper order of algebraic
operations is the responsibility of the programmer (you). So, none of
you should now be surprised if you type in the following:

local b = -2
di `b'^2
"

HTH,

Joao Lima

Em 16 de dezembro de 2011 13:31, Tiago V. Pereira
<tiago.pereira@mbe.bio.br> escreveu:
> Dear statalisters,
>
> Can someone explain why is this happening?:
>
> local x = -2
> */Approach A:
> dis (`x'^2)*(5)
> */Approach B:
> dis (`x'*`x')*(5)
> */Approach C:
>  dis ((`x')^2)*(5)
>
>
> My output is:
>
> . dis (`x'^2)*(5)
> -20
>
> . */Approach B:
>
> . dis (`x'*`x')*(5)
> 20
>
> . */Approach C:
>
> .  dis ((`x')^2)*(5)
> 20
>
>
> Do I need vacations? (Am I forgetting something very simple from
> mathematics?)
>
> All the best,
>
> Tiago
>
>
> *
> *   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/

--
---------------------------------------------------------
João Ricardo F. de Lima
Embrapa Semiárido
e-mail: joao.ricardo@cpatsa.embrapa.br
fone: +55 (87) 3866-3667
home-page: www.cpatsa.embrapa.br
----------------------------------------------------------

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