Floating point cost on embedded systems in Embedded C - Time & Space Complexity
We want to understand how using floating point math affects the time it takes for embedded programs to run.
How does the cost of floating point operations grow as we do more calculations?
Analyze the time complexity of the following code snippet.
float sum = 0.0f;
for (int i = 0; i < n; i++) {
float val = (float)i * 0.5f;
sum += val;
}
return sum;
This code adds up floating point values in a loop from 0 to n-1.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Floating point multiplication and addition inside the loop.
- How many times: Exactly n times, once per loop iteration.
Each time n grows, the number of floating point operations grows the same amount.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 multiplications and 10 additions |
| 100 | About 100 multiplications and 100 additions |
| 1000 | About 1000 multiplications and 1000 additions |
Pattern observation: The work grows directly with n, so doubling n doubles the work.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of loop steps.
[X] Wrong: "Floating point operations take the same time as integer operations, so complexity is unaffected."
[OK] Correct: Floating point math often takes more time on embedded systems without hardware support, so each operation can be slower even if the count is the same.
Knowing how floating point math affects time helps you write efficient embedded code and explain your choices clearly in interviews.
"What if we replaced floating point math with fixed-point integer math? How would the time complexity and cost change?"
Practice
Solution
Step 1: Understand floating point vs integer operations
Floating point operations involve more complex calculations than integer operations.Step 2: Consider embedded system constraints
Embedded systems have limited processing power and battery life, so slower and power-hungry operations are costly.Final Answer:
They consume more time and power compared to integer operations. -> Option BQuick Check:
Floating point cost = More time and power [OK]
- Thinking floating point uses less memory
- Assuming floating point is always more accurate
- Believing floating point operations are faster
Solution
Step 1: Check variable type and value format
Floating point variables use the keyword 'float' and values use dot (.) as decimal separator.Step 2: Validate syntax correctness
float temperature = 23.5; uses float and a correct decimal number with dot, so it is correct.Final Answer:
float temperature = 23.5; -> Option AQuick Check:
float with dot decimal = correct syntax [OK]
- Using int for decimal numbers
- Using quotes around numbers
- Using comma instead of dot for decimals
float a = 1.5f;
float b = 2.5f;
float c = a * b;
printf("%f", c);Solution
Step 1: Analyze the code operations
The code multiplies two float variables and prints the result using %f, which is correct syntax.Step 2: Consider embedded system performance
Floating point multiplication is slower and consumes more power on embedded systems compared to integer operations.Final Answer:
It uses floating point multiplication which is slower on embedded systems. -> Option AQuick Check:
Floating point multiply = slower on embedded [OK]
- Thinking code has syntax errors
- Confusing format specifiers
- Assuming output is integer
float x = 3.0;
int y = 2;
float z = x / y;
printf("%d", z);Solution
Step 1: Check variable types and operations
Variable z is float, result of float divided by int, which is valid and results in float.Step 2: Check printf format specifier
Using %d expects an int, but z is float, so output will be incorrect or undefined.Final Answer:
Using %d to print a float variable causes incorrect output. -> Option CQuick Check:
Printf %d with float = wrong output [OK]
- Thinking division of float and int is invalid
- Assuming variable type declaration is wrong
- Ignoring printf format specifier importance
Solution
Step 1: Understand floating point cost and accuracy needs
Floating point operations are costly; using integers scaled by a factor preserves accuracy and reduces cost.Step 2: Evaluate options for optimization
Use integer arithmetic by scaling sensor values and divide after summing. uses integer math with scaling, which is efficient and accurate. Replace all floats with doubles for better precision. increases cost. Use floating point operations directly for all calculations. ignores cost. Convert sensor readings to strings and parse them later. is inefficient and unnecessary.Final Answer:
Use integer arithmetic by scaling sensor values and divide after summing. -> Option DQuick Check:
Scale integers to avoid floats = efficient and accurate [OK]
- Assuming doubles are always better
- Ignoring floating point cost
- Using string conversions unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
