rix icon indicating copy to clipboard operation
rix copied to clipboard

Precedence rules: BEDMAS and order of operation

Open JoePelz opened this issue 10 years ago • 2 comments

Parentheses are observed but otherwise order of operations is right to left. Therefor:

a = 3+3*3+3
b = (3+3)*3+3

result in:

a == 21
b == 36

Expected result:

a = 15
b = 21

JoePelz avatar Oct 02 '15 19:10 JoePelz

a side effect is that the following code doesn't work:

print "cube root 27 = " + 27.0^^(1/3.0)

The above code attempts to raise a String to the power of 1/3. It can be fixed with parentheses as follows:

print "cube root 27 = " + (27.0^^(1/3.0))

JoePelz avatar Nov 15 '15 22:11 JoePelz

can be easily fixed by splitting the MATHOP token into precedence groups, and defining precedence. e.g.

  • +, -
  • *, /
  • ^^

JoePelz avatar Nov 17 '15 07:11 JoePelz