PDA

View Full Version : Negative numbers



RUBiksCUbe
- 24th September 2006, 18:29
I'm trying to set a variable to the negative of another variable by saying:
remaining = -rows
Where rows has already been set to 8, but remaining seems to end up being set to 0 instead of -8.

sayzer
- 24th September 2006, 19:23
"...Keep in mind that all of the math and comparisons in PBP are unsigned."

Will you use that negative number in a math operation or is it just for a representation on LCD or sending via a serial comm. etc?

Darrel Taylor
- 24th September 2006, 19:51
Unless it has something to do with a previous version of PBP, can't say.
But this simple program comes up with 248 (-8). PBP 2.46
@ __CONFIG _CONFIG1H, _OSCS_OFF_1H & _HS_OSC_1H
define OSC 20
clear

remaining var byte
rows var byte

rows = 8
remaining = -rows

stop


Something you might try is ...

remaining = 0 - rows

RUBiksCUbe
- 24th September 2006, 20:14
I'm not using it for any math operations, just to control the start of a for loop.


for i = remaining toremaining + (rows - 1)
if i < 0 then
messageScroll[writePos] = $0
endif
if i >= 0 and i < messagelength then
messageScroll[writePos] = message[i]
endif
if i >= messageLength then
messageScroll[writepos] = $0
endif
writePos = writePos + 1
next i

for some reason when remaining is negative this part "crashes"

sayzer
- 25th September 2006, 03:27
Hi RUBiksCUbe,

Where do you get this "Remaining" variable into negative? Are you setting it to "negative" somewhere before this loop as you stated in the example above?


Also, here I made the same code of yours in a different look.
It seems you only need the negative value for " messageScroll[writePos] = $0 "






for i = remaining to remaining + (rows - 1)

if i < 0 then
messageScroll[writePos] = $0

else

if i < messagelength then
messageScroll[writePos] = message[i]
else
messageScroll[writepos] = $0
endif

endif

writePos = writePos + 1


next i






Also, are you setting other variables to an initial value at the beginning?






--------------------------

RUBiksCUbe
- 26th September 2006, 01:51
At the very beginning I have



rows = 8
remaining = (0 - rows)

Darrel Taylor
- 26th September 2006, 04:04
Couple problems here.

As Sayzer pointed out ... "...Keep in mind that all of the math and comparisons in PBP are unsigned."

This means that

if i < 0 then

will always evaluate to FALSE. And the FOR loop will always terminate before executing anything because -8 is really 248 in PBP math (assuming byte vars).

You'll need to find positive numbers to work with.

mister_e
- 26th September 2006, 05:12
Or testing the MSB of the variable. If 1=> negative if 0=>positive.

precision
- 26th September 2006, 05:57
I make load indicator with loadcell, i using pic 16f877. For 16 bit adc i using cd4066 , value from -65535 to +65535 with peak hold

I setup - reading

B0 var word ( reading of adc )
B1 var word
B2 var byte

main: if B0 > 0 then ( B1 = B0 ) and ( B2 = 43 ) ' 43 is + sign
if B0 < 0 then B1 = ( 65535 - B0 ) and ( B2 = 45 ) '45 is - sign

lcdout $fe,1
lcdout " Load ", B2, dec5 B1

sayzer
- 26th September 2006, 08:23
....

main: if B0 > 0 then ( B1 = B0 ) and ( B2 = 43 ) ' 43 is + sign
if B0 < 0 then B1 = ( 65535 - B0 ) and ( B2 = 45 ) '45 is - sign
...


Hi precision,

Your IF statements are not precise :)

You should correct them as follows.



if B0 > 0 then
B1 = B0
B2 = 43
endif

if B0 < 0 then
B1 = 65535 - B0
B2 = 45
endif

B0 never equals to zero?






This is actually the idea of assigning a flag that will indicate negative status.
That is why I asked above, will you use this negative number in a math operation.

You can have a flag, like NegFlag=1 meaning you have the negative value, and NegFlag=0 meaning positive value.

If of course it can be useful in your case.