Arithmetic - how is the order of operations determined?

image

Probably my missunderstood
first line is calculate without using brackets with value 7
second line is calculate using bracket for 1 and 2 argument with value 9
second line is calculate using bracket for 2 and 3 argument with value 7

Does Elixir make calculate choose the right then to the left?

1 Like

Hi @ariandanim,

It’s to do with Elixir’s operator precedence - documented here: Operators — Elixir v1.15.7

* & \ are higher than (non-unary) + so are applied first. Note that there are two + operators in the list. The first is the Unary version, e.g. +10, the other is binary (e.g. 1 + 2). The unary one comes before * so you can do things like +10 * -20. If you want to override the default order you need to use brackets (as you have nicely demonstrated!)

7 Likes

PS, when I was learning about this in high school maths, we used the “BODMAS” phrase to help us remember the order - it is mentioned in the Wikipedia entry on operator precedence: Order of operations - Wikipedia

5 Likes

Yeah, I think it‘s important to highlight, that this are not elixir rules, but it‘s how arithmetics in math works. However elixir has more operator than used for arithmetics, where things then become more elixir specific again.

4 Likes

This is how math works. I do not think Elixir does anything “strange” with the math operators, should be familiar if you know the arithmetic rules from math.

5 Likes

Tks Brother Mindok

1 Like

tks Brother LostKobrakai

tks Kwando