When to not use floating point
The first thing one needs to realize is that floating point does not mean "I need decimals". This is where some 95% of all would-be embedded programmers misusing floating point fail.
The cure for that disbelief is to realize that internally, the program should use a unit that makes sense for the MCU to use, not one that makes sense to humans.
For example if you measure current in mA with an on-chip 10 bit ADC, the convenient unit to use in software is "fixed point raw ADC values from 0 to 1024". In C programming that means a
uint16_t or optionally a
uint_fast16_t. Not an
int and certainly not a
Using the unit mA inside firmware calculations is only convenient for the human programmer's brain, in case it can't handle abstract units. But it is inconvenient for the program, because it means you need to re-scale all readings to and potentially add rounding inaccuracy while doing so. Plus the scaling code is just overhead bloat. And it will likely include division, which can be painful for many MCUs.
Yeah you are reading the current in mA. But unless you actually need to print that current on a display or something to a human user, that unit is actually not helpful. Do the mA re-scaling on pen and paper while designing the algorithm, instead of dragging it into your firmware.
When to use floating point
- If your MCU has a FPU and you actually need to do advanced math, then you should use floating point. Otherwise you should not.
"Advanced math" doesn't necessarily mean advanced from the programmer's perspective, but from the software's perspective. "Advanced" includes things like square roots, geometry or trigonometry, the use of
math.h in general, complex numbers, AI math etc. Things that would be painful to implement in fixed point.