View Full Version : Rounding Word Values
viperhex
- 26th March 2010, 16:46
I am new to PICBasic and having an issue with the following portion of my code:
MAXVAL VAR Word
DIST VAR Word
MAXVAL = 32
DIST = 1 to 30
Percent=(DIST/MAXVAL)*100
The Precent value is always zero!
I suspect the "Percent" variable contains a non whole number and that is why it has no value.
Is there a way I can present this equation differently to round up or down to the nearest whole number?
I appreciate any advise.
Thank you.
HenrikOlsson
- 26th March 2010, 17:23
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.
viperhex
- 26th March 2010, 18:59
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
Normnet
- 26th March 2010, 22:28
Dist = Dist * 1000 'Dist will now be 1000-30000
Percent = Dist / Maxval 'Will return 937 as a representation of 93.7Percent = Percent + 5 'ROUND 937 UP
Percent = Percent / 10 'ANSWER 94
Norm
viperhex
- 27th March 2010, 20:01
Thanks Norm. I think that will do the trick....
aratti
- 27th March 2010, 20:59
Chris, what about semplifying your code in this way:
AA0 var Byte ' new variable to add.
If Percent < 95 then
AA0 = (Percent / 10) - 1
PortB = (2^AA0)-1
else
PORTB = %11111111
FreqOut bz, 250, 4000
pause 250
PORTB = %01111111
Low bz
pause 250
endif
will eliminate from comp1 up to label continue.
Al.
viperhex
- 28th March 2010, 17:54
This is perfect.... I get more out of these discussions then any one book I own.
Thanks Al
Chris
btw
The circuit I am working on is for measuring the amount of salt in my water softener brine tank and displays the level on a blank switch plate via 8 LED's (see attachment). The circuit uses a Parallax PING sensor, 16F87 for the slave and 16F876 for the master. communication is via RS-485.
If anyone is interested in the specifics, I would be more than happy to share details.
Again, thanks for all the help.
Chris
aratti
- 28th March 2010, 18:43
Chris, I made a mistake! Sometimes I forget that PBP is not VB and in this case I have use the power sign^ improperly since PBP is not able to do power (the sign is used for Bitwise Exclusive OR). So the code I have posted will not work! To overcome this limitation I have used a lookup table so the working code is as follow:
AA0 var Byte ' new variable to add.
BB0 var byte ' new variable to add
If Percent < 95 then
AA0 = (Percent / 10) - 1
LOOKUP2 AA0,[1,3,7,15,31,63,127,255],BB0
PortB = BB0
else
PORTB = %11111111
FreqOut bz, 250, 4000
pause 250
PORTB = %01111111
Low bz
pause 250
endif
As you can see the code didn't change much, just removed (2^AA0)-1 and added a new variable.
Al.
viperhex
- 28th March 2010, 19:13
Al, I appreciate the correction. I understood what you where trying to do. I too am more familiar with VB than PBP.
This definitely simplifies the overall code.
Thank you very much.
Chris
Powered by vBulletin® Version 4.1.7 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.