Replacing Floating-Point Arithmetic with Integer Arithmetic in C
In C programming, particularly for embedded systems, replacing floating-point arithmetic with integer arithmetic (often called fixed-point arithmetic) is a well-known technique to improve performance on processors that lack floating-point units (FPUs) or where execution speed and memory are limited.
Goal:
Use integer addition to approximate floating-point addition by scaling your numbers.
1. Concept of Fixed-Point Arithmetic
Instead of working directly with floats, multiply your floating-point values by a scaling factor (a power of 10 or 2) and treat them as integers.
For example:
This works because:
(1.234 + 2.000) ≈ (1234 + 2000) / 1000
2. Code Example
Output:
Approximate sum = 3.234
3. Why Use This?
4. Cautions
5. Conclusion
Approximating floating-point operations with scaled integer arithmetic is a powerful technique in C, especially for embedded systems where performance and memory efficiency are critical. By applying a fixed scaling factor, developers can perform precise and fast calculations using simple integer operations. While this method introduces some limitations in precision and range, it offers a reliable alternative to floating-point math on constrained hardware—making it an essential skill for embedded and systems-level programmers.
Automotive Embedded Software
4moHi Yamil, if I’m not mistaken there multiple type of fixed-point arithmetic methods, do you know more about formal names and types??