Shop OBEX P1 Docs P2 Docs Learn Events
New P Basic Math Problem How do you calculate Percentage of a Number — Parallax Forums

New P Basic Math Problem How do you calculate Percentage of a Number

sam_sam_samsam_sam_sam Posts: 2,286
edited 2009-07-23 00:10 in General Discussion
Hi EveryOne

This is a project that I have been ·ask to do·by employer

As of right now I can not give all of the details.......................
................·but when the Project is done I will share with all of you on the Forum


I know that I have·This a another Post but this is a different question
http://forums.parallax.com/showthread.php?p=804156

http://forums.parallax.com/showthread.php?p=804554

http://forums.parallax.com/showthread.php?p=825473


Can this be done with P Basic Math and how would you do it

What I have will work but it would be nice to update more often


IF SL = 5000 THEN······· ' SL =· 5000· 10% of···· ·SL = 50000· 100%
T = 10························' T = % of what has been used
ELSE
IF SL = 10000 THEN
T = 20
ELSE
IF SL = 15000 THEN
T = 30
ELSE
IF SL = 20000 THEN
T = 40
ELSE
IF SL = 25000 THEN
T = 50
ELSE
IF SL = 30000 THEN
T = 60
ELSE
IF SL = 35000 THEN
T= 70
ELSE
IF SL = 40000 THEN
T = 80
ELSE
IF SL = 45000 THEN
T = 95
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

·
·
·
·
Sam

Post Edited (sam_sam_sam) : 7/22/2009 1:40:06 PM GMT

Comments

  • LeonLeon Posts: 7,620
    edited 2009-07-22 13:49
    You could do this:

    T = (SL/5000) * 10

    You could handle the last option as a special case.

    I've never used PBASIC so I don't know if it would be any better, though.

    Leon

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Amateur radio callsign: G1HSM
    Suzuki SV1000S motorcycle
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2009-07-22 17:26
    percent:
    ' J is a Nib index
    ' F is Word result
    ' N is Word
    N=SL   ' the variable going in
    D CON 50000  ' the 100% value
    '---------binary division loop-----------
    for J=15 to 0               ' 16 bits 
    N=N//D<<1                   ' remainder*2 
    F.bit0(J)=N/D               ' next bit 
    next
    F = F**1000 + 5 / 10    '  compute to 1% with roundoff
    RETURN  ' F as percent 0 to 100
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-07-22 23:21
    Tracy Allen
    Thank You for your reply

    I ran this as a Demo and the result is 4


    Do I have some thing wrong with the way that I have this Please let me know



    '·{$STAMP BS2}
    ' {$PBASIC 2.5}


    · percent:

    J······ VAR···· Nib········ · ' J is a index
    F······ VAR···· Word······· ' F is the result
    N······ VAR···· Word······· ' the variable going in

    D····· CON····· 50000····· ' the 100% value

    N = 43000

    '
    binary division loop

    FOR J=15 TO 0··············· ' 16 bits
    N=N//D<<1··················· ' remainder*2
    F.BIT0(J)= N/D·············· ' next bit
    NEXT
    F = F**1000 + 5 / 10········ '· compute to 1% with roundoff
    DEBUG HOME,· DEC· F
    RETURN······················ ' F as percent 0 to 100



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2009-07-22 23:49
    Oh, right. The limit of the algorithm is 32767 in the numerator. Will have to divide by two at the outset:

    D   CON   50000/2   ' limit will be 25000
    N  = SP / 2   '  so when SP=43000, N=21500
    '---------binary division loop-----------
     
    FOR J=15 TO 0                ' 16 bits
    '  ... and so on as above
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-07-23 00:08
    Tracy Allen

    Thank You for all of your help with all of my P Basic Math Calculation Questions·this has a lot of help and I have learned a lot doing this Project
    for where I work··



    I have a lot fun doing this Project·····hop.gif··· ·····smile.gif········ ·jumpin.gif···· Thanks again

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-07-23 00:10
    Leon

    I try this and works very good· Thank You for your help and your reply

    T = (SL/5000) * 10





    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam
Sign In or Register to comment.