On the ESP32, why should performance-critical code use float rather than double?
The ESP32's FPU handles single precision only. Every double operation is emulated in software, which is far slower than the one-instruction float version.
A floating-point unit is one of the options a chip designer can select, and Espressif chose the single-precision one. A float multiply therefore compiles to one FPU instruction. A double multiply compiles to a call into a software library routine that does the job with integer instructions, many times slower.
The trap is that C makes double the default without anyone asking for it:
- A literal such as
0.1is adouble;0.1fis afloat. sqrt(),sin()and friends take and returndouble;sqrtf()andsinf()are thefloatversions.float x = y * 0.1;promotesytodouble, multiplies in software, then converts back.
So in a sensor loop that runs thousands of times a second, writing 0.1f and sinf() can make a large difference. (The compiler can sometimes narrow a double operation back to float when the result is provably identical, but you shouldn't count on it.) Where you genuinely need 15 significant digits, double still works, just slowly.
Tip: On the ESP32, a bare decimal literal like 0.1 in float code can be a hidden software call; put an f on it.
Go deeper:
ESP-IDF — Maximizing Execution Speed — Espressif's own advice to avoid double-precision arithmetic.