News:

Let's find out together what makes a PIC Tick!

Main Menu

Comparison with a negative number

Started by joesaliba, Feb 27, 2022, 05:43 PM

Previous topic - Next topic

joesaliba

Hi Les,

Device 18F26K22, xtal 64

The following is not working: -

dim Roll        as sword = 0
Dim Pitch as        as sword = 0

'---------------------------------------------

Main:
Test(0, 0)

delayms 500
goto main

'-------------------------------------------------------------------

proc Test(roll as sbyte, pitch as sbyte)

If Roll <= -135 then
    Print at 1,1, "Roll ", sdec roll
    Print at 2,1, "Pitch ", sdec pitch
endif

endProc

As Roll is greater than -135, i.e. 0, it should not Print

As a workaround I used: -

dim Roll        as sword = 0
Dim Pitch as        as sword = 0

'---------------------------------------------

Main:
Test(0, 0)

delayms 500
goto main

'-------------------------------------------------------------------

proc Test(roll as sbyte, pitch as sbyte)

dim sTemp as sword = -135

If Roll <= sTemp then
    Print at 1,1, "Roll ", sdec roll
    Print at 2,1, "Pitch ", sdec pitch
endif

endProc

This does not print as expected as Roll is greater -135.

Regards
Joe

Yasin

#1
The SBYTE variable cannot be less than -127. This is not possible. It's unreasonable.

For
If Roll <= -135 thento work, the Roll value must be less than -134, for example -135 or -140.


Add>> So I mean the procedure variable sbyte is wrong.

joesaliba

I think you are correct.

The confusion i made was that variable roll is used as global and local.

But I expected that as roll is 0, both the local and global Roll, to work.

But to be honest, I made a mistake declaring local variable as sbyte.

Will test again when at the pc.

Regards

Joe

top204

Yasin is correct. Signed variables are 2's complemented types, so a signed 8-bit variable can hold -128 to 127. Signed 16-bit variables can hold -32768 to 32767, and signed 32-bit variables can hold -2147483648 to 2147483647.