Rounding Word Values


Closed Thread
Results 1 to 9 of 9

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default

    Hi,
    PBP only deals with integer math, truncating (not rounding) anything to the right of any decimal point. In your case the largest number you're having in the calculation is 30/32 which will come back as 0 when executed because 30/32 = 0.9375, 0.9375*100 will be treated as 0*100.

    To come around this you can multiply Dist by 100 before doing the division:
    Dist = Dist * 100 'Will now be 100-3000
    Percent = Dist / Maxval 'Will return 93, should be 93.75

    To get even better results:
    Dist = Dist * 1000 'Dist will now be 1000-30000
    Percent = Dist / Maxval 'Will return 937 as a representation of 93.7

    Then to display the value you can do:
    HSEROUT ["Result: ", Percent / 10, ".", Percent // 10, "%"], 10, 13
    Which should display 93.7

    If Dist was, say 11 the "true" result is (11/32)*100 = 34.375% but you'll get
    11000/32 = 343, displayed as 34.3

    Is that good enough or do you need it to be rounded?

    /Henrik.

  2. #2
    Join Date
    Mar 2010
    Posts
    5


    Did you find this post helpful? Yes | No

    Default

    Thank you for the reply Henrik.

    Your reply was most helpful.

    Is there a way to get around truncation (round values to nearest integer) in PB PRO?

    I am using LED's rather than a display as follows:

    '------------------------------------------------------------------------
    comp1:
    If Percent > 10 Then comp2
    PORTB = %00000001
    Goto continue
    comp2:
    If Percent > 20 Then comp3
    PORTB = %00000011
    Goto continue
    comp3:
    If Percent > 30 Then comp4
    PORTB = %00000111
    Goto continue
    comp4:
    If Percent > 40 Then comp5
    PORTB = %00001111
    Goto continue
    comp5:
    If Percent > 50 Then comp6
    PORTB = %00011111
    Goto continue
    comp6:
    If Percent > 60 Then comp7
    PORTB = %00111111
    Goto continue
    comp7:
    If Percent > 70 Then comp8
    PORTB = %01111111
    Goto continue
    comp8:
    If Percent > 80 Then comp9
    PORTB = %11111111
    Goto continue
    comp9:
    If Percent > 95 Then comp10
    comp10:
    PORTB = %11111111
    FreqOut bz, 250, 4000
    pause 250
    PORTB = %01111111
    Low bz
    pause 250

    continue:

    'Rest of code

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

    Again, thank you so much for your help.

    Chris

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