Fixed-point
Overview
A Fixed-point data type represents real numbers with a fixed number of digits after the decimal point. Unlike floating-point, the decimal position never moves. This makes fixed-point arithmetic predictable, precise, and well-behaved.
It’s the calm accountant to floating-point’s adventurous explorer.
How Fixed-point Numbers Work
A fixed-point value is usually stored as an integer plus an implicit scale factor.
Example:
Value = stored_integer / scale
If scale = 100:
Stored 12345 → represents 123.45
The decimal point is agreed upon in advance and never shifts.
Representation Formats
| Format | Meaning |
|---|---|
| Qm.n | m integer bits, n fractional bits |
| Decimal fixed-point | Fixed decimal places (e.g., 2 digits) |
Embedded systems often use Q-format, while business systems prefer decimal fixed-point.
Common Operations
- Addition and subtraction (straightforward)
- Multiplication and division (require rescaling)
- Comparison (safe and exact)
Example
Pseudocode
price = 1999 // represents 19.99
tax = 250 // represents 2.50
total = price + tax // 2249 → 22.49
Real-world analogy
Fixed-point numbers are like currency. Two decimal places, no negotiations 💰.
Time and Space Complexity
Space: O(1) Operations: O(1)
Often faster than floating-point on hardware without FPUs.
Use Cases
- Financial calculations
- Embedded systems
- Digital signal processing
- Real-time systems
- Accounting
Advantages
- Exact decimal representation
- Predictable arithmetic
- No rounding surprises
Limitations
- Limited range
- Manual scaling required
- Less flexible than floating-point
Fixed-point vs Floating-point
| Aspect | Fixed-point | Floating-point |
|---|---|---|
| Precision | Exact | Approximate |
| Range | Limited | Very large |
| Performance | Fast (simple CPUs) | Fast (modern CPUs) |
| Best for | Money, control systems | Science, graphics |