On Wednesday, June 13, 2018 at 8:56:21 PM UTC+2, Christian Gollwitzer wrote:
> Am 13.06.18 um 10:20 schrieb heinrichmartin:
> > Looking at the last example on
http://auriocus.github.io/VecTcl/tutorial.html,
> >
> >> vectcl::compile {
> >> x, y = list(y, x) ;# swap x and y
> >> A= -3*x
> >> }
> >> # this outputs:
> >> ...
> >> set A [numarray::neg [numarray::* 3 [set x]]]
> >
> > is unary minus really evaluated after the multiplication? In Tcl unary operators take precedence.
> >
>
> Yes, and I think Tcl is wrong. The only difference you'll ever notice is
> with the power operator:
>
> (Tests) 57 % package require vectcl
> 0.2
> (Tests) 58 % set a 3
> 3
> (Tests) 59 % expr {-$a**2}
> 9
> (Tests) 60 % vectcl::vexpr {-a**2}
> -9.0
I agree about the power operator, but the example uses the multiplication. It is more efficient (notable only for very large vectors) to multiply the vector with a negative scalar than to then negate the vector. So basically, my "observation" could also be resolved by explicitly parsing negative numbers (i.e. minus followed by a (decimal) digit without space).
> I think evaluating the expression -a**2 to (-a)**2 is not what you
> usually want, because it contradicts textbook math - power has
> precedence over the -.
Yes, Tcl expressions could swap the precedence of power and unary ops - but we know this will not happen ;-)
Regards
Martin