Page 1 of 1

understanding calculations

Posted: Tue Jan 27, 2015 10:01 am
by enric
What do you expect d1 has as value?

d1 = 3 + 2 - 1 - 1;
Log ("result", d1);

Re: understanding calculations

Posted: Tue Jan 27, 2015 10:51 am
by enric
I expected 3 but get 5 .....

Re: understanding calculations

Posted: Tue Jan 27, 2015 4:37 pm
by pipfromslitherine
The scripting language does not apply standard operator precedence. You should use brackets to ensure you get the correct result for any calculation. IIRC this is stated in the docs.

Cheers

Pip

Re: understanding calculations

Posted: Tue Jan 27, 2015 5:07 pm
by enric
I looked at STUB_Engine_Docs.pdf

Key Differences with C
 There is no operator priority. Expressions are parsed left to right. Brackets are supported and their use recommended.

But this is not true: "Expressions are parsed left to right.", in that case "+" and "-" operators should work because order is not significant.
Look at the examples, the latest one gives a very odd result.

Examples
result = 1 + 2 + 3;
Log ("result", result);
6

result = 1 + 2 - 3;
Log ("result2", result);
0

result = 4 - 2 - 1;
Log ("result3", result);
3
result = 2 + 2 - 2 - 3;
Log ("result4", result);
5

The odd results are only when two "-" are involved.

Well, is like it is, I'll need to revise all my scripts. Hope not used too often two additional subtractions.

Re: understanding calculations

Posted: Tue Jan 27, 2015 6:11 pm
by Amaris
Take result3: 4 - 2 - 1 ---> 3

So I guess BA2 make first 2 - 1 = 1
And after 4 - result of the first calcul = 4 - 1 = 3

Same think with result4: 2 + 2 - 2 - 3
So 2 - 3 = -1
2 - -1 = 3
2 + 3 = 5

It's logical, not natural logic :wink:

I guess you need to use Brackets to have multiple operator on the same line.

Re: understanding calculations

Posted: Tue Jan 27, 2015 6:20 pm
by Amaris
So I guess if expressions are parsed left to right, they are calculated in the inverse order, right to the left. So we have a top-down parser or its inverse. :roll: Well I haven't BA2 under hands so I can't test that myself...

Re: understanding calculations

Posted: Tue Jan 27, 2015 6:34 pm
by enric
Amaris wrote:Take result3: 4 - 2 - 1 ---> 3

So I guess BA2 make first 2 - 1 = 1
And after 4 - result of the first calcul = 4 - 1 = 3

Same think with result4: 2 + 2 - 2 - 3
So 2 - 3 = -1
2 - -1 = 3
2 + 3 = 5

It's logical, not natural logic :wink:

I guess you need to use Brackets to have multiple operator on the same line.
So, ""Expressions are parsed left to right."," as the documentation say should say "right to left"?
but even this is not clear

result 3
"So I guess BA2 make first 2 - 1 = 1"
But this is misleading because should be -2 -1 = -3
You assume 4 - (2-1)

result4
you begin with values 3rd and 4th
2 - 3 = -1
BUT this is also misleading because should be -2 -3 = -5

In both cases there is a negative sign before the value.

Re: understanding calculations

Posted: Tue Jan 27, 2015 6:40 pm
by enric
Probably BA assumes firstValue - (anything here)

1 + 2 + 3; assumes 1 + (2+3)
4 - 2 - 1; assumes 4 - (2-1)
and so on

Re: understanding calculations

Posted: Tue Jan 27, 2015 8:42 pm
by pipfromslitherine
I will have to look at the code - it is possible that the documentation has gotten out of date! Whichever way it works, I guess I should basically stop people from guessing and just say that brackets are recommended (as they generally are - it's simpler to see bracketing and understand a line of code than trying to parse it in your head, remembering the precedence of operators ;) ).

Cheers

Pip