Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Problem with power operator


From   Finne Håkon <Hakon.Finne@sintef.no>
To   statalist@hsphsun2.harvard.edu
Subject   st: RE: Problem with power operator
Date   Wed, 28 Aug 2002 15:06:28 +0200

`n'^2
is substituted
-2^2
and when calculated, the operator ^ is applied before the operator -
as if you had
-(2^2)
but you intend
(-2)^2
for which case your program line 3 could read
local nsquared1=(`n')^2
to force the intended order of operator precedence.

--Håkon
hakon.finne@sintef.no

> -----Original Message-----
> From: Matthias Greiner [mailto:mgr@vetinst.dk]
> Sent: 28. august 2002 14:55
> To: 'statalist@hsphsun2.harvard.edu'
> Subject: st: Problem with power operator
> 
> 
> Dear Stata users
> 
> Using 
> 
> . do "... \square.do"
> 
> . program define square
>   1. version 7.0
>   2. gettoken n 0 : 0, parse(" ,")
>   3. local nsquared1=`n'^2
>   4. local nsquared2=(`n'*`n')
>   5. di "`nsquared1'   `nsquared2'"
>   6. end
> . 
> end of do-file
> 
> I observe the following misbehaviour of the power operator "^2"
> 
> . square -2
> -4   4
> 
> The example is for illustration -- the problem occurs in a 
> real programme,
> where the negative value n is a result of internal (and correct)
> calculations. I am puzzled because I never observed this before. 
> 
> Thanks in advance for your help.
> Matthias 
> ---
> Matthias Greiner, Dr. med. vet.
> Head of the International EpiLab
> Danish Veterinary Institute
> Bülowsvej 27, DK-1790 Copenhagen V
> Tel +45 35 30 01 40
> Fax +45 35 30 06 16
> mgr@vetinst.dk
> www.vetinst.dk/EpiLab 
> *
> *   For searches and help try:
> *   http://www.stata.com/support/faqs/res/findit.html
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
> 
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   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   |   What's new   |   Site index