View Full Version : Math problem
Kamikaze47
- 16th February 2008, 09:01
I have these 3 variables:
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;
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.
skimask
- 16th February 2008, 09:14
DIV32...it's in the book.
Kamikaze47
- 16th February 2008, 09:29
thanks shimask
ive done this:
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.
skimask
- 16th February 2008, 09:37
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?
sayzer
- 16th February 2008, 10:26
If I get it right;
Why not this way?
<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>>= <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>"A:"</b></font>,<font color="#000080"><b>DEC </b></font>A, <font color="#008000"><b>" B:"</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>"%"</b></font>,<font color="#000080"><b>DEC </b></font>Solid,<font color="#008000"><b>"."</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???
Acetronics2
- 16th February 2008, 10:53
Hi, Sayzer
You' re to it ...
With showing the Half, now ( was DS 1820 Degrees with 3 Digits Leds...)
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
Powered by vBulletin® Version 4.1.7 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.