For division in the past I have found a whole number that is divisible by a power-of-two, that is equal to the reciprocal of the divisor, then multiply by the whole number and divide by the power-of-two. Here's that again, but in English.... for your situation, I plug in 182.024 on a calculator, and get the reciprocal of that, which is 0.005493781039.... Now, I keep multiplying that by 2 until I get a whole-ish number, and arrive at 45.00505427... Seems close enough to a whole number. But I have multiplied by 2 thirteen times, so to get back the original number, I need to divide by 2^13. So instead of dividing by 182.024, you would multiply by 45 (each with adding, subtracting and shifting), and then divide by 2^13 (easy by right shifting 13 times). Error? 1 / (45/(2^13)) = 182.04444444... Cheers, -Neil. -----Original Message----- From: pic microcontroller discussion list [mailto:PICLIST@MITVMA.MIT.EDU]On Behalf Of Jinx Sent: Saturday, May 18, 2002 5:06 AM To: PICLIST@MITVMA.MIT.EDU Subject: [PIC]: Division by a constant Perhaps one of the maths-heads can help out here I need to divide a number 0 - 65535 by a constant 182.024. If I divide by 182 this would give an error of around 0.013%, which might be OK, I just don't know yet. ================================================ This 16bit / 8bit would work if the error is OK http://www.piclist.com/techref/microchip/math/div/16by8lzf-ng.htm ================================================ If I used this code to divide the range 000000 - FFFFFF by B606 (182.024 * 256) that would be almost perfect http://www.piclist.com/techref/microchip/math/div/24by16.htm or http://www.piclist.com/techref/microchip/math/div/24by16ad.htm ================================================ Could the 24 / 16 routine be optimised to work as a division by B606, like this (which I don't really understand) ? http://www.piclist.com/techref/method/math/divconst.htm Looking through some code it appears that at 20MHz calculations could take around 60-90us, which is good, but any increase in speed by using code fixed for a constant would be even gooder TIA -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body