Math problem


Closed Thread
Results 1 to 6 of 6

Thread: Math problem

  1. #1
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429

    Default Math problem

    I have these 3 variables:

    Code:
    a VAR WORD
    b VAR WORD
    a and b can be anywhere from 0 to 999.

    I need to work out a way of displaying an a/b percentage on my LCD to 1 decimal place.

    I thought of this;

    Code:
    a   VAR WORD
    b   VAR WORD
    pct VAR WORD
    
    pct=(a*1000)/b
    which would give me the percent, with the last decimal being the decimal place. But this wont work if a=999 becuase 999*1000 is lager than a WORD can be.

    I'm using PBP 2.46 so I cant use LONGs.

    Any ideas? I'm sure this is possible, but i'm having a brain freeze.
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    DIV32...it's in the book.

  3. #3
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    thanks shimask

    ive done this:

    Code:
    a    VAR WORD
    b    VAR WORD
    pct  VAR WORD
    temp VAR WORD
    
    temp=a*1000
    pct=DIV32 b
    The only problem im having now is with rounding. If the real percentage is 65.28% id like it to display 65.3%, but of course im getting 65.2 because its just truncating the result.
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  4. #4
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Kamikaze47 View Post
    thanks shimask
    ???????

    The only problem im having now is with rounding. If the real percentage is 65.28% id like it to display 65.3%, but of course im getting 65.2 because its just truncating the result.
    Add .05 to everything...but that's a good trick since PBP doesn't support decimal points.
    So how are you handling that?

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


    Did you find this post helpful? Yes | No

    Default Think like GrandPA

    If I get it right;

    Why not this way?

    Code:
    <font color="#000000">        A <font color="#000080"><b>VAR WORD
            </b></font>B <font color="#000080"><b>VAR WORD
            </b></font>Solid <font color="#000080"><b>VAR WORD
            </b></font>Liquid <font color="#000080"><b>VAR BYTE
    
            </b></font>DoMath:
    '....get values  from somewhere..
                Solid = A/B
                Liquid = A//B
                <font color="#000080"><b>IF </b></font>Liquid <font color="#000080"><b>DIG </b></font><font color="#FF0000"><b>0 </b></font>&gt;= <font color="#FF0000"><b>5 </b></font><font color="#000080"><b>THEN </b></font>Liquid = Liquid + <font color="#FF0000"><b>10
                </b></font>Liquid = Liquid <font color="#000080"><b>DIG </b></font><font color="#FF0000"><b>1
                
                
                </b></font><font color="#000080"><b>LCDOUT </b></font><font color="#FF0000"><b>$fe</b></font>,<font color="#FF0000"><b>1</b></font>,  <font color="#008000"><b>&quot;A:&quot;</b></font>,<font color="#000080"><b>DEC </b></font>A, <font color="#008000"><b>&quot; B:&quot;</b></font>,<font color="#000080"><b>DEC </b></font>B
                <font color="#000080"><b>LCDOUT </b></font><font color="#FF0000"><b>$fe</b></font>,<font color="#FF0000"><b>$c0</b></font>,<font color="#008000"><b>&quot;%&quot;</b></font>,<font color="#000080"><b>DEC </b></font>Solid,<font color="#008000"><b>&quot;.&quot;</b></font>,<font color="#000080"><b>DEC </b></font>Liquid
                <font color="#000080"><b>PAUSE </b></font><font color="#FF0000"><b>30
    
    
            </b></font><font color="#000080"><b>GOTO </b></font>DoMath
    Something like this way???
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  6. #6
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Wink

    Hi, Sayzer

    You' re to it ...

    With showing the Half, now ( was DS 1820 Degrees with 3 Digits Leds...)

    Code:
    		IF ( TEMP > 2631 and Temp < 3731)  	THEN 	' -10 to 100°C
    			
    				Virgule = 1 
    				
    				' round to 1/2 degré et show the 5/10 
    				
    				IF(Tempa // 10) < 3 THEN Tempa = ( Tempa/10)* 10 : GOTO AFF
    			 	
    			 	IF(Tempa // 10) < 8 THEN Tempa = (Tempa/10)*10 + 5 : GOTO AFF
    			 	
    			 	IF(Tempa // 10) > 7 THEN Tempa = (Tempa/10)*10 + 10 : GOTO AFF
    			 			 	
    			ENDIF
    		
    	
    			Virgule = 0
    			
    			IF (Tempa // 10) > 4 THEN 	Tempa = Tempa + 10
    					 						
    			Tempa = Tempa/10 	' Show Degrees without tenths
    Have a nice day

    Alain
    Last edited by Acetronics2; - 16th February 2008 at 10:57.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

Similar Threads

  1. Line Graph, math problem...
    By TerdRatchett in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 6th May 2009, 04:20
  2. Math problem with string result
    By Lotondo in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 15th November 2006, 10:06
  3. PicBasic Pro Math Problem??
    By Glen65 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th March 2006, 04:36
  4. Math Formula / Variable Problem! Help!
    By bwarp in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 18th February 2006, 07:59
  5. Math problem...with picbasic pro
    By pcaccia in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 15th October 2005, 19:28

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