# C++ Fixed-Point Number
## Why need fixed-point number?
### Main problem of float-point number
- Accuracy vs Precision
- Accurency: how close a mesurement is to the true value
- Precision: how much info you have about a quantity
- Integer vs Float point
> Floating point numbers are inherently different from integers in that **not every fraction can be represented exactly in binary**, whereas any integer can.
- Integer: complete accuracy, but lacks precision
- Floating point: good precision, but poor accuracy
### Example of poor accuracy: Accumulating errors in a loop
```C
#include <stdio.h>
int main() {
float sum = 0.0f;
for (int i = 0; i < 1000000; ++i) {
sum += 0.000001f;
}
printf("Sum: %.10f\n", sum); // Expect 1.0
return 0;
}
```
- When compiled, the output is ```Sum: 1.0090389252```, instead of 1.0
### Example of poor accuracy: Catastrophic cancellation
```c
#include <stdio.h>
int main() {
double a = 1e16;
double b = 1.0;
double result = (a + b) - a;
printf("Expected 1.0, got: %.17f\n", result);
return 0;
}
```
- Output is ```Expected 1.0, got: 0.00000000000000000```, instead of 1.0
### Balance between accuracy and precision
- Due to the feature of floating point, it's unsuitable in situations where
- exact decimal precision is required (e.g., financial calculations)
- consistent rounding behavior is critical
- or small errors could accumulate over time.
- Thus, a more balanced approach is needed to represent numbers that preserve accuracy but with predictable behaviour
## What's fixed-point number?
Fixed-point arithmetic offers a solution by **using integers to represent real numbers with a fixed number of decimal places**. This approach:
- **Preserves accuracy** by avoiding rounding errors from binary fraction approximations
- **Offers predictable behavior**, especially in embedded systems, financial calculations, and real-time applications
- **Can be faster than floating-point on hardware** without floating-point units (e.g., microcontrollers).
## How fixed-point works?
### 🔢 Basic Concept
In fixed-point representation, a real number is scaled by a predetermined factor to convert it into an integer. The scaling factor determines how many bits are allocated to the integer and fractional parts.
### Example
>To represent 26.5 in fixed-point with a scaling factor of 2 (i.e., one fractional bit), you would multiply 26.5 by 2 to get 53
>This integer (53) is stored, and the fixed-point system interprets it as 26.5 by dividing by the scaling factor during computations.
### ⚙️ Arithmetic Operations
- +/-: When two fixed-point numbers have the same scaling factor, they can be added or subtracted directly as integers. The result maintains the same scaling factor.
- *: Multiplying two fixed-point numbers involves multiplying their integer representations and then adjusting the result by dividing by the scaling factor to maintain the correct scale.
- /: Dividing a fixed-point number by another requires scaling the numerator appropriately before performing integer division to preserve precision.
## Pros Cons of Fixed-Point
### Pros
- As straight-forward and efficient as integers arithmetic in computers
### Cons
- Loss of range and precision compared to floating point number representations
## Reference
- 42 cpp02 subject
- Understanding and using floating point number: https://www.cprogramming.com/tutorial/floating_point/understanding_floating_point.html
- Floating point nbr representation: https://www.cprogramming.com/tutorial/floating_point/understanding_floating_point_representation.html
- Printing floating point nbrs: https://www.cprogramming.com/tutorial/floating_point/understanding_floating_point_printing.html
- Intro to fixed point nbr representation: https://web.archive.org/web/20231224143018/https://inst.eecs.berkeley.edu/~cs61c/sp06/handout/fixedpt.html