Page 1 of 1

Calculations with brackets

Posted: Thu Dec 23, 2010 8:51 am
by Jan Lichtenbelt
I simulate the calculation in Flowcode 4 with variable I(byte):
I=8
I=6*(I/3)
This gives for me the unexpected results of I=16 instead of I=12. It looks like the brackets are not seen.
The following calculation :
I=8
I=I/3
I=6*I
This gives I=12 as expected.

I tried to check the C and asm files, but both looks correct. That means in asm first de division has been done before the multiplication.
Then I downloaded the program into PIC16F88, which shows as results of the first calculation the expected 12! That means the brackets in Flowcode 4 simulation are not seen. Be prepared!

Jan

Re: Calculations with brackets

Posted: Thu Dec 23, 2010 11:29 am
by Steve
This is a known issue, but is actually not directly a fault of the brackets. It is because in simulation, all calculations are performed as if the numbers are floats until they are placed into an actual variable.

So, I = 6*(I/3) = 6*(8/3) = 6*(2.66666) = 16.0000

Only once the calculation is complete does the float get changed to an integer.

Doing the calculation in separate steps works because the I = I/3 = 2.66666 gets turned into 2 because it is being placed into the "I" variable.

Re: Calculations with brackets

Posted: Thu Dec 23, 2010 1:05 pm
by medelec35
Hiya Jan,
This is an issue I raised concern with here:
http://www.matrixmultimedia.com/mmforum ... =31&t=7714

Re: Calculations with brackets

Posted: Thu Dec 23, 2010 8:43 pm
by Jan Lichtenbelt
Dear Steve and Medelec,

I was afraid this was not a new issue. But I could not find the answer in the Forum.

Both thanks

Jan