Electronic – Does the “Avoid using floating-point” rule of thumb apply to a microcontroller with a floating point unit (FPU)

cfloating pointmicrocontrollerrtos

As a rule of thumb, I try to avoid using floating-point in my embedded system codebase.

Floating-point variables are:

  • Computation-intensive
  • Not atomic (can cause problems in an RTOS application or with interrupts)
  • Their precision can cause non-obvious behaviour (float comparison problem).

But what about a microcontroller with a floating point unit (like the STM32F4)?

Do those concerns still apply? Would you still advise against using floating-point?

Best Answer

If you buy a processor with a hardware FPU, you don't have the same concerns about precision*, reentrant behaviour etc. Go ahead and use them!

Couple of thoughts though:

  • You might consider that the processor can power down the (large) FPU when it's not used, so check that running your FP routines saves you power (if you care about that) over doing it in software.

  • Depending on the implementation, the FPU might also have different registers to the core - sometimes compilers can make clever use of these.

  • Don't use the FPU as a crutch for bad firmware design. For example, could you do the same thing with fixed point, and use the normal core instead?

(* The FPU should conform to a given standard implementation, so be aware of any limitations arising from that.)