Floating point operations use more time and power on small devices. Knowing this helps write faster and efficient code.
Floating point cost on embedded systems in Embedded C
float myNumber = 3.14f; double myDouble = 3.14159;
Floating point numbers store decimal values like 3.14 or 2.718.
Using float uses less memory than double, but is less precise.
float for decimal numbers when less precision is okay.float temperature = 36.5f; // stores temperature with decimals
int for whole numbers to save time and power.int count = 10; // whole number, faster and cheaper to use
double gives more decimal places but costs more resources.double preciseValue = 3.1415926535;
// more precise but slower on small devicesThis program measures how long it takes to add numbers using floats versus ints. It shows floats usually take more time on embedded systems.
#include <stdio.h> #include <time.h> int main() { clock_t start, end; volatile float f = 0.0f; volatile int i = 0; int loops = 1000000; // Measure time for float addition start = clock(); for (int j = 0; j < loops; j++) { f += 0.1f; } end = clock(); double float_time = (double)(end - start) / CLOCKS_PER_SEC; // Measure time for int addition start = clock(); for (int j = 0; j < loops; j++) { i += 1; } end = clock(); double int_time = (double)(end - start) / CLOCKS_PER_SEC; printf("Float addition time: %f seconds\n", float_time); printf("Int addition time: %f seconds\n", int_time); return 0; }
Floating point math can be much slower than integer math on small chips without special hardware.
Using integers or fixed-point math can save power and speed up your program.
Always test your program's speed if you use floating point on embedded devices.
Floating point operations cost more time and power on embedded systems.
Use integers when possible to make your program faster and more efficient.
Measure and test your code to choose the best number type for your needs.
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
