Power operator precedence

160 views
Skip to first unread message

Helder Correia

unread,
Jun 24, 2007, 8:02:05 PM6/24/07
to SpeedCrunch
SpeedCrunch currently evaluates "-3^2" as "9". I'm not sure whether
this is correct.
So far I think the correct result would be "-9" because the power
should be evaluated first.
In turn, "(-3)^2" returns (correctly) "9" and "-(3^2)" returns (correctly) "-9".

In fact, "-(3)^2" even returns "9", which is the weirdest case in my opinion.

Or at least I think so. What about you?

Marco Wegner

unread,
Jun 24, 2007, 9:02:52 PM6/24/07
to speed...@googlegroups.com
2007/6/25, Helder Correia <helder.pere...@gmail.com>:

SpeedCrunch currently evaluates "-3^2" as "9". I'm not sure whether
this is correct.

Yes, I do agree that "-3^2" should rather be evaluated as "-(3^2)". At least that's what I would expect.
The same should apply to "-3!" which should evaluate as "-(3!)" resulting in "-6". Currently you get "NaN" which suggests that it's rather "(-3)!".
 
In fact, "-(3)^2" even returns "9", which is the weirdest case in my opinion.

No, I don't think this is weird at all since the expression in the brackets is evaluated first and becomes just "3" (without the brackets). Which means that you get the same expression as the one above: "-3^2". So both terms should evaluate the same. Which they do ;-)

Regards,
Marco

Ariya Hidayat

unread,
Jun 25, 2007, 3:16:22 AM6/25/07
to speed...@googlegroups.com
I agree with you guys. -3^2 should definitely give -9.

Can someone file it as an issue so that it won't get lost?


Regards,

Ariya

ookami

unread,
Jun 25, 2007, 3:23:21 PM6/25/07
to speedcrunch
The power operator has usually higher priority than the unary -
operator.

Wolf Lammen

Reply all
Reply to author
Forward
0 new messages