I'm not sure if this is exactly what you are looking for, but ... For in-line programming, we sometimes store values ​​as signed 0.15 fixed points with an appropriate scale factor. You can think of it as saving all values ​​as fractions between -1.1 and tracking the multiplier along with units. For example, to represent 5 amperes in a variable with scaling up to 10 A, you should:
int16 I = 16384; // .5@10A = 5A
When you do the math, you simply track the scale with the units.
int16 R = 3277 // .1@2ohm = .2ohm int16 V = ((int32)I*R)>>15; //@10A*@2Ohm = @20V //result = 1638 => 1638/32768= .05@20V = 1V
It takes a lot of attention to detail to work with this system, but on a system with a slow or no floating point processor, this is a way to maintain arbitrary precision and ultrafast operations.
source share