Fixed-point arithmetic: Difference between revisions

Content deleted Content added
Tags: Mobile edit Mobile web edit Advanced mobile edit
No edit summary
Tags: Mobile edit Mobile web edit Advanced mobile edit
Line 128:
 
===Division===
The division of fixed point numbers is best understood as the division of two fractions of potentially different denominators (scaling factors). With {{frac|p|q}} and {{frac|r|s}} (where p q r s are all integers), the naive approach is to rearrange the fraction to form a new scaling factor (s/q):
 
: (p/q) / (r/s) = (p/r) / (s/q)
Line 136:
For example, division of 3456 scaled by 1/100 (34.56) and 1234 scaled by 1/1000 (1.234) yields the integer 3456÷1234 = 3 (rounded) with scale factor (1/100)/(1/1000) = 10, that is, 30. As another example, the division of the first number by 155 implicitly scaled by 1/32 (155/32 = 4.84375) yields the integer 3456÷155 = 22 (rounded) with implicit scaling factor (1/100)/(1/32) = 32/100 = 8/25, that is 22×32/100 = 7.04.
 
One trick to reduce or even eliminate the rounding error is to convert the dividend to a smaller scaling factor. (withThis correspondinglycauses more digits/bits orto bitsbe preservedkept forin the calculation)integer part with a finer scaling factor in the result. IfSay we reduce the scaling factor by ''n'' times, then we effectivelyinstead havecalculate:
 
: (p/q) / (r/s) = (np/nq) / (r/s) = (np/r) / (s/nq)