Page 1 of 1
Multi calculation bug
Posted: Mon Dec 22, 2014 7:28 pm
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
Re: Multi calculation bug
Posted: Tue Dec 23, 2014 10:45 am
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.
Re: Multi calculation bug
Posted: Wed Dec 24, 2014 2:30 am
by mikn
I did so, separated to 3 operations. Didn't expect the bug in this place.
Re: Multi calculation bug
Posted: Wed Dec 24, 2014 10:36 am
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.