0
0
Embedded Cprogramming~20 mins

Floating point cost on embedded systems in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Embedded Floating Point Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Floating point vs integer addition timing

Consider the following embedded C code snippet that adds two numbers. What is the expected output on a typical microcontroller without hardware floating point support?

Embedded C
#include <stdio.h>
#include <time.h>

int main() {
    clock_t start, end;
    volatile float a = 1.5f, b = 2.5f, c;
    volatile int x = 1, y = 2, z;
    start = clock();
    for (int i = 0; i < 1000000; i++) {
        c = a + b;
    }
    end = clock();
    double float_time = (double)(end - start) / CLOCKS_PER_SEC;

    start = clock();
    for (int i = 0; i < 1000000; i++) {
        z = x + y;
    }
    end = clock();
    double int_time = (double)(end - start) / CLOCKS_PER_SEC;

    printf("Float add time: %.3f\nInt add time: %.3f\n", float_time, int_time);
    return 0;
}
ABoth operations cause a runtime error
BFloat add time is about the same as Int add time
CInt add time is significantly higher than Float add time
DFloat add time is significantly higher than Int add time
Attempts:
2 left
💡 Hint

Think about how microcontrollers handle floating point operations without special hardware.

🧠 Conceptual
intermediate
1:30remaining
Why floating point operations are costly on embedded systems

Which of the following best explains why floating point operations are more costly on many embedded systems?

AEmbedded systems have limited memory, so floating point numbers cannot be stored
BMost embedded CPUs lack hardware floating point units, so floating point math is done in software
CEmbedded systems use only integer math by design and do not support floating point
DFloating point operations require special power supply voltages not available on embedded boards
Attempts:
2 left
💡 Hint

Consider the hardware capabilities of typical microcontrollers.

🔧 Debug
advanced
1:30remaining
Identifying floating point performance bottleneck

Given the following embedded C code, which line is most likely causing a performance bottleneck on a microcontroller without an FPU?

Embedded C
void process_data(float *data, int size) {
    float sum = 0.0f;
    for (int i = 0; i < size; i++) {
        sum += data[i];
    }
    // Use sum for something
}
Afloat sum = 0.0f;
Bfor (int i = 0; i < size; i++) {
Csum += data[i];
D// Use sum for something
Attempts:
2 left
💡 Hint

Look for the line that does floating point math inside a loop.

📝 Syntax
advanced
1:00remaining
Floating point literal syntax in embedded C

Which of the following floating point literals is correctly written in embedded C?

Afloat x = 1.0f;
Bfloat x = 1,0;
Cfloat x = 1.0;
Dfloat x = 1.f.0;
Attempts:
2 left
💡 Hint

Remember the suffix for single precision float literals in C.

🚀 Application
expert
2:30remaining
Reducing floating point cost in embedded systems

You want to optimize an embedded system program that uses many floating point calculations but runs slowly. Which approach will most effectively reduce floating point cost?

AReplace floating point variables with fixed-point integer math and scale values appropriately
BIncrease the clock speed of the microcontroller to speed up floating point operations
CUse double precision floating point instead of single precision for accuracy
DAdd more floating point variables to distribute the load
Attempts:
2 left
💡 Hint

Think about how to avoid slow floating point math on hardware without an FPU.