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!
===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.
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)