Scaled Integers

This tip has been extracted from the full Scaled Integers article.

If you need variables to hold a very large range of values, or hold a small range of fractional values, then your project might benefit from using smaller integer types to store scaled integer values. This technique reduces data memory usage and can drastically increase code execution speed.

Scaled integers hold regular integer values, but the implied scaling factor indicates how the scaled integer value is to be interpreted. Integer values can be scaled up or down to suit your requirements. Scaled-up integers tradeoff precision for range (e.g., scaling by a factor of four: 0, 4, 8, 12, etc.); scaled-down integers trade off range for precision (e.g., scaling by a factor of two: 0, 0.5, 1, 1.5, etc.).

There is typically some computational overhead when working with scaled integers, but unlike floating-point values, there is no separate exponent to deal with, so this overhead is much lower.

© 2016 Microchip Technology, Inc.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.