On Sat, 24 Nov 2001, Josh Koffman wrote: > Hi all. Just as a quick aside, I hate the American holidays...the list > slows down too much. I hate it when people go tend to their "real > lives". > > Anyway, I have a question about scaling. Basically what I want to do is > to have a few (8-32 maybe) registers with 8 bit values in them. Then, > based on another 8 bit value I want to scale them. Basically, the second > register becomes a percentage multiplication factor. So if I have a > value of d'204' in the first register, and a value of d'128' in the > second (50%), after the operation I want a result of d'102' (half of the > original value). It can be in a second register. At first I though of > just using multiplication routines, but I can't figure how to multiply > by less than 1 (ie 0.5). I had a flash that maybe this could all be > accomplished by shifting bits, but I don't know how. Any pointers to > info would be great! In general, if you wish to multiply by a "fraction" you'll need to convert it into a format suitable for the PIC. Suppose your fraction is: f = X/Y and you want to multiply some register by f. If you only are interested in 8-bits of precision then you can convert f into a fraction of 256: X b ----- = ----- Y 256 or, b = 256 * X / Y Note that b is number between 0 and 255 and can be fed into any 8-bit multiplication routine. After the multiplication, you can divide the product by 256 and you'll have your answer. Of course, "division" by 256 is trivial. Often time you know before hand the value of Y. Thus you can precompute the 256/Y factor. However, if not done carefully you can inadvertantly throw away precision. For example, if Y is 10 then 256/Y is 25.6. What do you do with that .6? Well it depends... If you want to see an optimized "scaling" routine used to convert numbers into percentages, then look here: http://www.piclist.com/techref/microchip/math/scale/8bxpercent.htm Scott -- http://www.piclist.com hint: The list server can filter out subtopics (like ads or off topics) for you. See http://www.piclist.com/#topics