Calculation order

Any bugs you encounter with Flowcode should be discussed here.
Post Reply
mnfisher
Valued Contributor
Posts: 972
http://meble-kuchenne.info.pl
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 515 times

Calculation order

Post by mnfisher »

Not sure if this has been reported before - but it's just caused me some debug grief.

Trying out an algorithm in the simulator I have

Code: Select all

.Return = .a + .b / 2
Which on hardware I would expect to give the answer, say for .a = 10 and .b = 20, of 15..

In the simulator it doesn't - it gives 25 (so it's doing .Return = .a + (.b / 2))

Tested under v8 and it's also a problem there.
bodmas.fcfx
(7.2 KiB) Downloaded 166 times
Martin

Steve-Matrix
Matrix Staff
Posts: 1265
Joined: Sat Dec 05, 2020 10:32 am
Has thanked: 168 times
Been thanked: 279 times

Re: Calculation order

Post by Steve-Matrix »

I think that's correct. in BODMAS, the "D" (divide) takes precedence over the "A" (add).

So

Code: Select all

z = a + b / c 
will be calculated as

Code: Select all

z = a + (b / c)

kersing
Valued Contributor
Posts: 162
Joined: Wed Dec 02, 2020 7:28 pm
Has thanked: 66 times
Been thanked: 58 times

Re: Calculation order

Post by kersing »

Sounds like perfect order in which the operands should be applied to me.
Division has higher priority and is performed first. If you want to make sure things are done in the order you expect always use parenthesis.

mnfisher
Valued Contributor
Posts: 972
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 515 times

Re: Calculation order

Post by mnfisher »

Yes - having a bad day on the math front! :oops: what a numpty....

It is (of course) doing it perfectly and my brain isn't.....
What I wanted was (.a + .b) / 2 and that was what I used in the end... Sadly .Return = 0 would have probably worked just as well ;)
(see https://www.matrixtsl.com/mmforums/view ... 26&t=20604)


Martin

Lesteraction
Posts: 1
Joined: Mon Mar 06, 2023 11:23 am

Re: Calculation order

Post by Lesteraction »

Division is completed first since it is more important. Always use parentheses if you want to ensure that things are completed in the sequence you anticipate.

Post Reply