better way to do this using math


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838

    Default better way to do this using math

    hi guys , have mind blank on how to best do this code in math than use a lookup

    Code:
            if val < 6  AND Menu_Select = 211 then   lookup val,[0,2,4,8,16,32],val 
            if val =>6  and val <= 11 and Menu_Select = 2112 then  lookup val-6,[0,2,4,8,16,32],val
            if val =>12 and Menu_Select = 2113 then lookup val-12,[0,2,4,8,16,32],val

  2. #2
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: better way to do this using math

    retry
    Code:
    
            if val < 6  AND Menu_Select = 211 then   lookup val,[1,2,4,8,16,32],val 
            if val =>6  and val <= 11 and Menu_Select = 2112 then  lookup val-6,[1,2,4,8,16,32],val
            if val =>12 and Menu_Select = 2113 then lookup val-12,[1,2,4,8,16,32],val

  3. #3
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default Re: better way to do this using math

    Power 2 to the value of val ( 2^val) and you will get the same value as your lookup table.
    Since 2^0 = 1 you will use an if then to turn it into zero.
    Cheers

    Al.
    Last edited by aratti; - 5th August 2014 at 01:47.
    All progress began with an idea

  4. #4
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: better way to do this using math

    the manual shows " ^ " to be a bitwise exclusive OR ?

  5. #5
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default Re: better way to do this using math

    Forgotten that PBP has not the power. Well you can use the shift left. Every shift you multiply by two so B1 = B0 << val where B0 = 1. Still need the filter to turn the one into zero, when val = 0.

    Cheers

    Al.
    All progress began with an idea

  6. #6
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: better way to do this using math

    Quote Originally Posted by longpole001 View Post
    retry
    Code:
    
            if val < 6  AND Menu_Select = 211 then   lookup val,[1,2,4,8,16,32],val 
            if val =>6  and val <= 11 and Menu_Select = 2112 then  lookup val-6,[1,2,4,8,16,32],val
            if val =>12 and Menu_Select = 2113 then lookup val-12,[1,2,4,8,16,32],val
    Obviously I do not know how this code fits into your main program but here is an idea that could be of use.

    Code:
    WHILE val > 5
    val = val - 6
    wend
    
    val = 1 << val
    I am not sure about this line "val = 1 << val " just try it is all I can suggest.

  7. #7
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: better way to do this using math

    yep i did not see an easy math "power " option , which would be good
    i am just trying to save code space a bit, lookups take more code space than math calc

  8. #8
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: better way to do this using math

    Quote Originally Posted by EarlyBird2 View Post
    Obviously I do not know how this code fits into your main program but here is an idea that could be of use.

    Code:
    WHILE val > 5
    val = val - 6
    wend
    
    val = 1 << val
    I am not sure about this line "val = 1 << val " just try it is all I can suggest.
    Sheldon,

    I have tested this and it works a treat. It must save a lot of code space compared to your original three lines?

Similar Threads

  1. Help with A/D math!
    By dbodenheimer in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 19th March 2011, 00:51
  2. Math help please...
    By Ioannis in forum General
    Replies: 2
    Last Post: - 20th June 2008, 10:18
  3. Math Help Please
    By markcadcam in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 21st May 2008, 19:00
  4. PBPL Math...new math takes more cycles...Always?
    By skimask in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 10th February 2008, 10:22
  5. Math help please!!!
    By jbirnsch in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 10th August 2007, 14:45

Members who have read this thread : 1

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