When processing signals and sensor values in embedded C, we can usually choose between representing the values in floating-point (e.g. IEEE 754) or fixed-point, the latter often preferred when no floating-point unit is available, as it's my case.
I was wondering what is the name of a solution/pattern where we use just plain integers to represent the sensor value without decimals because we simply change the unit of measurement the value is represented in the unit of the maximum accuracy.
Let's assume I have a distance-measuring sensor (like a proximity sensor) with millimetre-accuracy. An example output would be 1.234 m
. I have the following choices to store the output distance measurement:
I only need to take care of having large-enough integers when doing arithmetics with the third option to avoid overflows. For example: computing the area of a rectangle measured by two sensors is done by multiplying two distances in millimetres a*b
and this requires twice as many bits, because the unit is also squared: mm^2
.
The same "third option" representation can be used for more complex stuff than just SI-prefixes, like a int32_t
representing k
multiples of sqrt(2)
instead of saving k*sqrt(2)
into a float directly, that is: storing int32_t k_foo = 3;
instead of float foo = 4.2426405f;
.
Is there an official name for the "third option" in data representation? It's like fixed-point with no decimal places but with a non-standard unit. The "decimal places" are implicit in the used measurements unit. I have a hard time documenting this (kind of stupid) solution without a name.
Fixed-point representations are scaled representations and vice-versa. From the Wikipedia page on fixed-point arithmetic:
A fixed-point representation of a fractional number is essentially an integer that is to be implicitly multiplied by a fixed scaling factor. For example, the value 1.23 can be stored in a variable as the integer value 1230 with implicit scaling factor of 1/1000 (meaning that the last 3 decimal digits are implicitly assumed to be a decimal fraction), and the value 1 230 000 can be represented as 1230 with an implicit scaling factor of 1000 (with "minus 3" implied decimal fraction digits, that is, with 3 implicit zero digits at right).
The web page even gives an example of using a change of units as the scaling factor:
However, other scaling factors may be used occasionally, e.g. a fractional amount of hours may be represented as an integer number of seconds; that is, as a fixed-point number with scale factor of 1/3600.
The term “fixed-point” arises because it is common to represent a number using an integer in some fixed base, such as binary or decimal, and say that the number represented is the one obtained by putting a decimal point (or binary point or radix point) at a fixed place in the number. For example:
Generally, a fixed-point format represents a number x as x•be, where b and e are fixed. A scaled format represents a number x as x•S, where S is fixed. These two are equivalent. Any fixed-point format x•be is the scaled format x•S where S = be, and any scaled format x•S is the fixed-point format x•be where b = S and e = 1.
Integer representations are a trivial subset of fixed-point representations where e = 0.
One distinction of fixed-point formats and scaled formats arises in the ease of implementation using arithmetic in the base b. Normally, implementing multiplication and division requires multiplying and dividing by the scale. Given x and y with representations x' = x•S and y' = y•S:
Multiplication requires time and energy, and division more so. If a fixed-point representation is used with b equal to the base used to represent the integers, then the multiplications and divisions can be replaced by shifts. Thus, it is advantageous to choose a fixed-point format with b equal to the base used to represent integers. Equivalently, it is advantageous to choose a scaled format with S equal to a power of the base used to represent integers.
Thus, there is no mathematical difference between fixed-point and scaled representations, and there is little, if any, semantic difference. Either way, the choice of scaling factor is a matter of practical concern involving ease of computation and satisfactory precision, not whether the format is called fixed-point or scaled.