What do you expect d1 has as value?
d1 = 3 + 2 - 1 - 1;
Log ("result", d1);
understanding calculations
Moderators: Slitherine Core, BA Moderators
Re: understanding calculations
I expected 3 but get 5 .....
-
pipfromslitherine
- Site Admin

- Posts: 9923
- Joined: Wed Mar 23, 2005 10:35 pm
Re: understanding calculations
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
Cheers
Pip
follow me on Twitter here
Re: understanding calculations
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.
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.
- Attachments
-
- odd results.jpg (32.82 KiB) Viewed 3728 times
-
Amaris
- Captain - Heavy Cruiser

- Posts: 929
- Joined: Fri Jul 23, 2010 11:08 am
- Location: France
- Contact:
Re: understanding calculations
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
I guess you need to use Brackets to have multiple operator on the same line.
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
I guess you need to use Brackets to have multiple operator on the same line.
“Take care, my friend; watch your six, and do one more roll… just for me.”
-
Amaris
- Captain - Heavy Cruiser

- Posts: 929
- Joined: Fri Jul 23, 2010 11:08 am
- Location: France
- Contact:
Re: understanding calculations
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.
Well I haven't BA2 under hands so I can't test that myself...
“Take care, my friend; watch your six, and do one more roll… just for me.”
Re: understanding calculations
So, ""Expressions are parsed left to right."," as the documentation say should say "right to left"?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![]()
I guess you need to use Brackets to have multiple operator on the same line.
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
Probably BA assumes firstValue - (anything here)
1 + 2 + 3; assumes 1 + (2+3)
4 - 2 - 1; assumes 4 - (2-1)
and so on
1 + 2 + 3; assumes 1 + (2+3)
4 - 2 - 1; assumes 4 - (2-1)
and so on
-
pipfromslitherine
- Site Admin

- Posts: 9923
- Joined: Wed Mar 23, 2005 10:35 pm
Re: understanding calculations
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
Cheers
Pip
follow me on Twitter here
