Multi calculation bug

A forums to allow bugs and problems with Flowcode v6 to be reported and resolved.

Moderator: Benj

Post Reply
User avatar
mikn
Posts: 209
Joined: Mon Mar 03, 2014 10:11 pm
Has thanked: 54 times
Been thanked: 41 times
Contact:

Multi calculation bug

Post by mikn »

When I use several operators in one calculation string, the whole calculation becomes incorrect.
Here's the example project and the result in terminal:
result=-4096
result=-4096
result=520192
Attachments
multicalc.fcfx
(8.68 KiB) Downloaded 215 times
FC 6.1.3.2 (18.02.2016)

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Multi calculation bug

Post by Benj »

Hello Mikn,

Looks like a compiler bug/feature to me. The sum you are doing is being truncated to 16-bit.

127 * 8 * 512 = 520192

520192 MOD 65536 = 61440 which corresponds to signed value -4096.

In C I would add a typecast to the sum to force it to 32-bit but there is currently no provision to do this in Flowcode so breaking up the sum into smaller chunks is likely the best option.

User avatar
mikn
Posts: 209
Joined: Mon Mar 03, 2014 10:11 pm
Has thanked: 54 times
Been thanked: 41 times
Contact:

Re: Multi calculation bug

Post by mikn »

I did so, separated to 3 operations. Didn't expect the bug in this place.
FC 6.1.3.2 (18.02.2016)

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: Multi calculation bug

Post by LeighM »

It’s not a bug, it’s just the way C integer promotions work and the size of an integer on the 16 bit target platform.
Unfortunately this means that the results will be different on target than on Windows simulation, which is going to be confusing.
As Ben says, adding explicit casts would be one way to fix it in C code.
But this would be difficult to do in Flowcode as we would not want to force all calculations to 32 bit with the resulting bloat of program code.
The safest solution is to ensure that numbers are stored in long integer variables before attempting long integer maths on them, as in your third example.

Post Reply