PDA

View Full Version : Help with calculation



Megahertz
- 28th August 2012, 13:44
I need a little understanding with how the calculation of DIV32 function works. I have a part of calculation where results can vary from 8 bit to 32 bit.
The following is causing problems and I would like to know how to avoid DIV32 when results are up to 16bit:

Variable starting with 'w' are word sized
wDummy=wTemp * (wWeight2-wWeight1)
wTemp= DIV32 100 ' I do not want to use it if the result is up to 16 bit. But how to implement this check?

rsocor01
- 28th August 2012, 14:46
Well, probably something like this


wDummy=wTemp * (wWeight2-wWeight1)

IF wDummy > 65536 THEN
wDummy=wTemp * (wWeight2-wWeight1) 'This line needs to be before DIV32
wTemp= DIV32 100
ENDIF

Acetronics2
- 28th August 2012, 15:59
@ first ...

could you explain how DIV32 results can be > 16 bits ...

Never seen that for 14 Years I use PBP !

Ok, ...

1) The "Dummy" variable must be < 32768 ... see Manual !

2) IF you use DIV32 ... your program variables only can be < 16 Bits ( 65535 ), obvious, eh ! ( DIV32 doesn't work when LONGS used !!! :rolleyes: )

=> soooo, how are you dealing with your supposed 16 -> 32 bits intermediate "results" ???

BTW :


IF wDummy > 65536 THEN


" another " compiler would have sent you the message

Condition always TRUE

Alain

Megahertz
- 28th August 2012, 17:10
@ first ...

could you explain how DIV32 results can be > 16 bits ...

Never seen that for 14 Years I use PBP !

Ok, ...

1) The "Dummy" variable must be < 32768 ... see Manual !

2) IF you use DIV32 ... your program variables only can be < 16 Bits ( 65535 ), obvious, eh ! ( DIV32 doesn't work when LONGS used !!! :rolleyes: )

=> soooo, how are you dealing with your supposed 16 -> 32 bits intermediate "results" ???

BTW :


IF wDummy > 65536 THEN


" another " compiler would have sent you the message




DIV32 results are never over 16bits. What I meant is that my calculation in the following sentence

wDummy=wTemp * (wWeight2-wWeight1)

Can result in result of upto 16 bits, which is fine as wDummy can handle it. But in cases when it gets over 16bits for example if wTemp is 100 and result of the subtraction is 100 as well, DIV32 gets me what I want. i.e. 10,000 in this example.

BUT the problem is that I am not sure if DIV32 is still doing something to my answer when the result is up to 16 bits as I would not like DIV32 to execute in this case. Hope I made myself more clear this time.