Division

corblimey

New Member
I want to do the following maths

result = b0 / b1 * 1000

b0 or b1 can be any value between 0 and 255 (except b0 = 0) and I only need the integer part of the result.

e.g.

b0 = 143, b1 = 215 =>

143 / 215 * 1000 = 665.115

So I want 665.

Since I've not been playing with picaxes too long I find the maths a little confusing.

Any thoughs?
 
As you've already discovered. The result of maths will always be an integer rounded down. So, for the equation you've shown, do the multiplication first.

RESULT=b0*1000/b1

Don't forget that RESULT needs to be a word variable.
If b0 is larger than 65, an overflow can occur.
To avoid that, it is possible to split the 1000. eg
RESULT=b0*100/b1*10 (strict left to right operations)

I'm sure somebody will be able to give a fully optimised version.
In fact, I can already 'see' that RESULT=b0*200/b1*5 would be much better.
 
Last edited:
and RESULT=b0*250/b1*4 a little bit better (pushing the multiplication up as high to the possible ceiling of 65535 - beyond which there will be oveflow - as we can go).
 
Thanks guys, that was the last part of the jigsaw in my first pic axe project :-)

I've finally got a working system but it's a bit late in the day to post a description, I'll try and find a few mins tomorrow.

Thanks again.

Glenn
 
This may be a good topic to cover in the
Code Snippets section.
Also WORD splitting High byte, Low byte etc.
Including things like math function order/direction
and other things that may trip people up.
0.02¢
 
Yeah, good idea. I would have appreciated that. I did a quick search and found a few bits and pieces but a specific post about common maths techniques would go a long way.

Again, thanks for the help.
 
Back
Top