Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Fixed-point arithmetic
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Operations== {{Unreferenced section|date=May 2023}} ===Addition and subtraction=== To add or subtract two values with the same implicit scaling factor, it is sufficient to add or subtract the underlying integers; the result will have their common implicit scaling factor and can thus be stored in the same program variables as the operands. These operations yield the exact mathematical result, as long as no [[arithmetic overflow|overflow]] occurs—that is, as long as the resulting integer can be stored in the receiving program [[variable (computing)|variable]]. If the operands have different scaling factors, then they must be converted to a common scaling factor before the operation. ===Multiplication=== To multiply two fixed-point numbers, it suffices to multiply the two underlying integers, and assume that the scaling factor of the result is the product of their scaling factors. The result will be exact, with no rounding, provided that it does not overflow the receiving variable. For example, multiplying the numbers 123 scaled by 1/1000 (0.123) and 25 scaled by 1/10 (2.5) yields the integer 123×25 = 3075 scaled by (1/1000)×(1/10) = 1/10000, that is 3075/10000 = 0.3075. As another example, multiplying the first number by 155 implicitly scaled by 1/32 (155/32 = 4.84375) yields the integer 123×155 = 19065 with implicit scaling factor (1/1000)×(1/32) = 1/32000, that is 19065/32000 = 0.59578125. In binary, it is common to use a scaling factor that is a power of two. After the multiplication, the scaling factor can be divided away by shifting right. Shifting is simple and fast in most computers. Rounding is possible by adding a 'rounding addend' of half of the scaling factor before shifting; The proof: round(x/y) = floor(x/y + 0.5) = floor((x + y/2)/y) = shift-of-n(x + 2^(n−1)) A similar method is usable in any scaling. ===Division=== To divide two fixed-point numbers, one takes the integer quotient of their underlying integers and assumes that the scaling factor is the quotient of their scaling factors. In general, the first division requires rounding and therefore the result is not exact. 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. If the result is not exact, the error introduced by the rounding can be reduced or even eliminated by converting the dividend to a smaller scaling factor. For example, if ''r'' = 1.23 is represented as 123 with scaling 1/100, and ''s'' = 6.25 is represented as 6250 with scaling 1/1000, then simple division of the integers yields 123÷6250 = 0 (rounded) with scaling factor (1/100)/(1/1000) = 10. If ''r'' is first converted to 1,230,000 with scaling factor 1/1000000, the result will be 1,230,000÷6250 = 197 (rounded) with scale factor 1/1000 (0.197). The exact value 1.23/6.25 is 0.1968. ===Scaling conversion=== In fixed-point computing it is often necessary to convert a value to a different scaling factor. This operation is necessary, for example: * To store a value into a program variable that has a different implicit scaling factor; * To convert two values to the same scaling factor, so that they can be added or subtracted; * To restore the original scaling factor of a value after multiplying or dividing it by another; * To improve the accuracy of the result of a division; * To ensure that the scaling factor of a product or quotient is a simple power like 10<sup>''n''</sup> or 2<sup>''n''</sup>; * To ensure that the result of an operation can be stored into a program variable without overflow; * To reduce the cost of hardware that processes fixed-point data. To convert a number from a fixed point type with scaling factor ''R'' to another type with scaling factor ''S'', the underlying integer must be multiplied by the ratio ''R''/''S''. Thus, for example, to convert the value 1.23 = 123/100 from scaling factor ''R''=1/100 to one with scaling factor ''S''=1/1000, the integer 123 must be multiplied by (1/100)/(1/1000) = 10, yielding the representation 1230/1000. If the scaling factor is a power of the base used internally to represent the integer, changing the scaling factor requires only dropping low-order digits of the integer, or appending zero digits. However, this operation must preserve the sign of the number. In two's complement representation, that means extending the sign bit as in [[arithmetic shift]] operations. If ''S'' does not divide ''R'' (in particular, if the new scaling factor ''S'' is greater than the original ''R''), the new integer may have to be [[rounding|rounded]]. In particular, if ''r'' and ''s'' are fixed-point variables with implicit scaling factors ''R'' and ''S'', the operation ''r'' ← ''r''×''s'' requires multiplying the respective integers and explicitly dividing the result by ''S''. The result may have to be rounded, and overflow may occur. For example, if the common scaling factor is 1/100, multiplying 1.23 by 0.25 entails multiplying 123 by 25 to yield 3075 with an intermediate scaling factor of 1/10000. In order to return to the original scaling factor 1/100, the integer 3075 then must be multiplied by 1/100, that is, divided by 100, to yield either 31 (0.31) or 30 (0.30), depending on the [[rounding|rounding policy]] used. Similarly, the operation ''r'' ← ''r''/''s'' will require dividing the integers and explicitly multiplying the quotient by ''S''. Rounding and/or overflow may occur here too. ===Conversion to and from floating-point=== To convert a number from floating point to fixed point, one may multiply it by the scaling factor ''S'', then round the result to the nearest integer. Care must be taken to ensure that the result fits in the destination variable or register. Depending on the scaling factor and storage size, and on the range input numbers, the conversion may not entail any rounding. To convert a fixed-point number to floating-point, one may convert the integer to floating-point and then divide it by the scaling factor ''S''. This conversion may entail rounding if the integer's absolute value is greater than 2<sup>24</sup> (for binary single-precision IEEE floating point) or of 2<sup>53</sup> (for double-precision). Overflow or [[underflow]] may occur if |''S''| is ''very'' large or ''very'' small, respectively.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)