Acmlm's Board - I2 Archive - Programming - Tricky division
User | Post |
HyperLamer
Posts: 5758/8210 |
Cool, I knew I could do something useful with that remainder. Now I just have to get it to actually work. |
Dish
Posts: 438/596 |
You might be on the right track with that. do your 32768 / x and get the result and remainder. Multiply them both by 4, then divide the remainder by x again, add that result to your previous result and the new remainder is your remainder. This means two divisions, which would be pretty slow -- you might be able to speed this up by having a chopped down version of your division routine (the result of the second division can't be greater than 3).
Example:
131072 / 1998 = 65 remainder 1202 32768 / 1998 = 16 remainder 800
16*4 = 64 800*4 = 3200
3200 / 1998 = 1 remainder 1202
64 + 1 = 65, with a remainder of 1202
|
HyperLamer
Posts: 5739/8210 |
Alright, well I've been writing some ASM code for Gameboy, but I've run into a snag. I need to do the equation x = 131072 / x. Now, I have division code but it's only 16-bit (can only go up to 65535 / x) and honestly I really don't know how it works; I downloaded it somewhere and it's completely lacking comments. It returns the result and remainder, both 16-bit. I tried doing x = 4 * (32768 / x) - this almost works, but because the decimals get dropped, the result is not quite accurate. Eg: 131072 / 1998 = ~65.6, 32768 / 1998 = ~16.4, and 16.4 * 4 = 65.6. Problem being, since Gameboy has no floating point support, it ends up being 16 * 4 = 64.
Sorry if this is confusing, I'm sleepy. I need to work out a way to do this, preferrably without having to modify the division code. |
|