Re: Problem with exponentiation: -2**2 gives 4 instead of -4



On 22 mei, 13:37, Arjen Markus <arjen.mar...@xxxxxxxxxx> wrote:
On 22 mei, 13:04, Jonathan Bromley <jonathan.brom...@xxxxxxxxxxxxx>
wrote:





On Fri, 22 May 2009 03:56:41 -0700 (PDT), Arjen Markus

<arjen.mar...@xxxxxxxxxx> wrote:
Hello,

I ran into a problem with exponentiation in Tcl 8.5.0:

expr {-2**2} gives 4 where I would expect -4 as an answer.

Personally, I would expect -2**2 to parse as (-2)**2,
which was positive when I was at school; if I wanted
to get -4 I would probably ask for -(2**2).

Of course, as I get older my grip on maths gets shakier,
so I could be wrong...
--
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services

Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
jonathan.brom...@xxxxxxxxxxxxxxxxx://www.MYCOMPANY.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.

I expect -2**2 to parse as: -(2**2) - it is a literal expression, it
is
not:
    set x -2
    expr {$x**2}

I came across this in an expression like:

    exp( -$y**2/$D }

Regards,

Arjen- Tekst uit oorspronkelijk bericht niet weergeven -

- Tekst uit oorspronkelijk bericht weergeven -

Furthermore, I would expect that "0-expression" and "-expression" give
the same value, but:

expr {0-2**2} gives -4

and

expr {-2**2} gives 4

It may be my Fortran background, but I find the current parsing
somewhat
disconcerting.

Regards,

Arjen
.



Relevant Pages