understanding calculations

Modders can post their questions on scripting and more.

Moderators: Slitherine Core, BA Moderators

Post Reply
enric
Brigadier-General - 8.8 cm Pak 43/41
Brigadier-General - 8.8 cm Pak 43/41
Posts: 1855
Joined: Sun May 15, 2011 8:47 am

understanding calculations

Post by enric »

What do you expect d1 has as value?

d1 = 3 + 2 - 1 - 1;
Log ("result", d1);
enric
Brigadier-General - 8.8 cm Pak 43/41
Brigadier-General - 8.8 cm Pak 43/41
Posts: 1855
Joined: Sun May 15, 2011 8:47 am

Re: understanding calculations

Post by enric »

I expected 3 but get 5 .....
pipfromslitherine
Site Admin
Site Admin
Posts: 9923
Joined: Wed Mar 23, 2005 10:35 pm

Re: understanding calculations

Post 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
follow me on Twitter here
enric
Brigadier-General - 8.8 cm Pak 43/41
Brigadier-General - 8.8 cm Pak 43/41
Posts: 1855
Joined: Sun May 15, 2011 8:47 am

Re: understanding calculations

Post 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.
Attachments
odd results.jpg
odd results.jpg (32.82 KiB) Viewed 3723 times
Amaris
Captain - Heavy Cruiser
Captain - Heavy Cruiser
Posts: 929
Joined: Fri Jul 23, 2010 11:08 am
Location: France
Contact:

Re: understanding calculations

Post 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.
“Take care, my friend; watch your six, and do one more roll… just for me.”
Amaris
Captain - Heavy Cruiser
Captain - Heavy Cruiser
Posts: 929
Joined: Fri Jul 23, 2010 11:08 am
Location: France
Contact:

Re: understanding calculations

Post 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...
“Take care, my friend; watch your six, and do one more roll… just for me.”
enric
Brigadier-General - 8.8 cm Pak 43/41
Brigadier-General - 8.8 cm Pak 43/41
Posts: 1855
Joined: Sun May 15, 2011 8:47 am

Re: understanding calculations

Post 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.
enric
Brigadier-General - 8.8 cm Pak 43/41
Brigadier-General - 8.8 cm Pak 43/41
Posts: 1855
Joined: Sun May 15, 2011 8:47 am

Re: understanding calculations

Post 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
pipfromslitherine
Site Admin
Site Admin
Posts: 9923
Joined: Wed Mar 23, 2005 10:35 pm

Re: understanding calculations

Post 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
follow me on Twitter here
Post Reply

Return to “Battle Academy 2: Modders Corner”