Negative numbers


Closed Thread
Results 1 to 10 of 10

Hybrid View

  1. #1
    RUBiksCUbe's Avatar
    RUBiksCUbe Guest


    Did you find this post helpful? Yes | No

    Default

    At the very beginning I have

    Code:
    rows = 8
    remaining = (0 - rows)

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    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.
    DT

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Or testing the MSB of the variable. If 1=> negative if 0=>positive.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  4. #4
    Join Date
    Mar 2006
    Location
    INDIA
    Posts
    89


    Did you find this post helpful? Yes | No

    Default

    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

  5. #5
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by precision
    ....

    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.

    Code:
    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.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

Similar Threads

  1. Working with 3 byte numbers
    By Josuetas in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 26th July 2007, 22:59
  2. Splitting numbers and recombining them(EEPROM Error)
    By selbstdual in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 3rd June 2007, 06:40
  3. Returning whole numbers for DS1820?
    By jessey in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 12th February 2007, 12:15
  4. Reading (ADC) negative current
    By sougata in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 21st November 2006, 17:38
  5. Can PBP understand negative numbers?
    By Tom Gonser in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 13th June 2005, 00:23

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts