Checking an integer-backed fixed-point value
A fixed-point value is an integer paired with a scale, and the integer alone does not say what quantity it represents. This guide decodes 12345 at scale 100 as 123.45 and shows how to add values that share a scale.
An integer plus a scale
Fixed-point storage represents a fractional number by saving an integer and remembering that the real value is that integer divided by a fixed scale. With a scale of 100, the stored integer 12345 stands for 123.45. With a scale of 1000, the same integer stands for 12.345. The integer itself is identical in the two cases, and the quantity differs by a factor of ten. For that reason a fixed-point field is described by two things, the stored integer and its scale, and a value read without the scale cannot be interpreted. This guide keeps both in view and uses a basic calculator for each conversion.
Decoding 12345 at scale 100
To decode, divide the stored integer by the scale. Dividing 12345 by 100 gives 123.45, which the first case records. The scale of 100 means two digits after the decimal point, because 100 is 10 squared. Going the other way, to encode 123.45 at scale 100, multiply by 100 and keep the integer 12345. Encoding a value with more decimals than the scale allows, such as 123.456, forces a rounding or truncation decision, and the scale is the place where that decision is made. The scale therefore defines both the resolution and the stored form. The smallest step a field can represent is one over the scale, so a scale of 100 resolves steps of 0.01 and a scale of 1000 resolves steps of 0.001. Choosing the scale amounts to choosing how fine the data must be, and it should be written into the specification before any values are stored, because changing it later means rewriting every stored integer.
Adding stored integers directly
When two values share one scale, their stored integers can be added directly. Adding 12345 and 55 gives 12400, which the second case records. Decoding 12400 at scale 100 gives 124, which the third case records. That matches the decimal sum of 123.45 and 0.55, which is 124.00. The stored form avoids the small representation errors that binary floating-point numbers show with decimal fractions, because integer addition is exact until the integer exceeds the storage width. Exactness is the main reason fixed-point formats are used for quantities with a fixed number of decimals. Binary floating point, by contrast, stores 0.1 as a close but inexact value, so repeatedly adding 0.1 ten times can end slightly away from 1. Stored integers have no such drift. Subtraction works the same way, and so do comparisons: two values at the same scale are equal exactly when their stored integers are equal, which makes equality tests dependable.
Aligning different scales
If one field uses scale 100 and another uses scale 1000, their integers cannot be added as they stand. Suppose 12345 at scale 100 is added to 2500 at scale 1000, which stands for 2.5. First convert the first value to the finer scale by multiplying its integer by 10, giving 123450. Then add: 123450 plus 2500 is 125950. Decoding by 1000 gives 125.95. Adding the raw integers, 12345 plus 2500, would give 14845, which decodes to 14.845 or 148.45, neither of which is the true sum. Convert to the finer scale before adding, since converting to the coarser scale could drop digits.
Multiplying fixed-point values
Multiplication changes the scale. Multiplying a value at scale 100 by another at scale 100 produces a result at scale 10000, because the two scales multiply. With 12345 and 200, which stand for 123.45 and 2.00, the stored product is 2,469,000, and decoding by 10000 gives 246.9. The decimal product 123.45 times 2 is 246.9, so the result agrees. To return to scale 100, divide the stored product by 100, which brings in a rounding decision if the division has a remainder. Tracking the scale through each operation prevents products that are off by a factor of a hundred.
Binary scales
The scale does not have to be a power of ten. Many embedded systems and signal-processing libraries use powers of two, written as Q formats. In a format with 8 fractional bits, the scale is 256, and a stored integer of 640 stands for 640 divided by 256, which is 2.5. Powers of two make scaling cheap on hardware because multiplying and dividing become bit shifts, but decimal fractions such as 0.1 are not exactly representable. Which kind of scale suits a task depends on whether the inputs are decimal quantities or measurements from converters. The idea of an integer plus a scale is the same in both.
Range and overflow
A stored integer has a finite width, such as 16, 32 or 64 bits. The largest value a signed 32-bit integer can hold is 2,147,483,647. At scale 100, that represents 21,474,836.47, so the scale shrinks the range of representable values as it increases the resolution. Intermediate results of multiplication can exceed the width even when the final answer would fit, and software that handles fixed-point values often uses a wider type for those intermediate steps. The worksheet does not model storage limits. It uses the exact integers of an arbitrary-precision calculator to show the logic.
No currency or rounding policy is implied
Fixed-point values are common in financial and measurement software, but the example here is a plain numeric representation. It does not assign a currency or recommend a rounding rule, and it does not say how a financial system should round. Rules for rounding money, such as which way to round halves and when, come from the contract, regulation or accounting standard that applies, and they are separate from the arithmetic of scales. The guide only shows how the integer and the scale determine the value.
Habits that prevent scale mistakes
Record the scale next to every stored integer, in field names, comments or documentation. Convert to a common scale before adding, comparing or subtracting. Track how the scale changes through multiplication and division. Decode at the boundary of the system, where values are shown to people. A quick sanity check is to decode a stored value and see whether the magnitude is plausible: if 12345 at the wrong scale produces 1.2345 or 12345.0 when a few hundred was expected, the scale has been misread. The basic calculator is enough for these checks. It is also worth logging both the integer and the decoded value when debugging, so that a reader can see 12345 and 123.45 side by side and spot a missing factor of ten at once. Comparing a decoded total with an independent decimal calculation, as the cases here do, is the quickest test that the scale handling is right.
Reproduce the checks
Decoded value
Open the Basic calculator and enter:
- First number
12345- Operation
divide- Second number
100
Expected output:
Result
123.45The scale defines the decimal position.
Same-scale stored addition
Open the Basic calculator and enter:
- First number
12345- Operation
add- Second number
55
Expected output:
Result
12400Both stored integers use one common scale.
Decoded sum
Open the Basic calculator and enter:
- First number
12400- Operation
divide- Second number
100
Expected output:
Result
124The result is interpreted under that same scale.
References and implementation scope
The examples describe the behavior of the linked Computandum tools. A successful local check does not establish that another application accepts the same format or applies the same limits.