Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

operator precedence in VecTcl

51 views
Skip to first unread message

heinrichmartin

unread,
Jun 13, 2018, 4:21:00 AM6/13/18
to
Hi,
On Wednesday, June 13, 2018 at 12:20:31 AM UTC+2, Christian Gollwitzer wrote:
> You could even go one step further and move assignment and simple
> control structures into the expression. That is already possible using
> VecTcl:
>
> vectcl::vexpr {
> t = linspace(0, 6.29, 100)
> x = a*cos(t)
> y = b*sin(t)
> }

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.

Regards
Martin

Christian Gollwitzer

unread,
Jun 13, 2018, 2:56:21 PM6/13/18
to
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 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 -. The other difference to spot is that the power
operator in VecTcl has no overload for integers, that might be
considered a bug.

Christian

Brad Lanam

unread,
Jun 13, 2018, 3:44:13 PM6/13/18
to
https://en.wikipedia.org/wiki/Order_of_operations#Special_cases

What does 4**3**2 evaluate to? Just curious.

Christian Gollwitzer

unread,
Jun 13, 2018, 3:48:20 PM6/13/18
to
Am 13.06.18 um 21:44 schrieb Brad Lanam:
As it should:

(Tests) 63 % vectcl::vexpr {4**3**2}
262144.0
(Tests) 64 % vectcl::vexpr {4**(3**2)}
262144.0
(Tests) 65 % vectcl::vexpr {4**3**2}
262144.0
(Tests) 66 % vectcl::vexpr {(4**3)**2}
4096.0

Christian

Arjen Markus

unread,
Jun 14, 2018, 3:02:14 AM6/14/18
to
Yes, ** is a right-associative operator whereas the other common binary operators are left-associative.

Regards,

Arjen

heinrichmartin

unread,
Jun 14, 2018, 3:23:59 AM6/14/18
to
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
0 new messages