No floating point for sprintf, it prints a '?'.
The functions sprintf
, sscanf
and the alike functions (snprintf
and others) do not have floating point support for avr microcontrollers.
History
One of the first Arduino boards used the Atmega8 microcontroller with only 8k flash and 1k sram. It was important to be very memory efficient.
The gcc compiler did not automatically include the floating point support libraries for the sprintf
functions, however it was easy to include them. Arduino decided not to include those extra libraries because they use extra memory.
Arduino has a preprocessor, compiler and linker that tries to only use code that is actually used. The sprintf
does not know at compile time if floating point will be used, so it would have to include those extra floating point support libraries whether they were used or not.
Now it is 2018 and it is sadly still not included.
Workaround
- The
Serial.println
functions can print floating point variables. They can however not print the output in scientific notation. It rounds the value that is printed to the nearest number.
- The
dtostrf()
and dtostre()
convert a float
variable to text. The dtostre()
is for scientific notation.
- A variable of the
float
type can be cast to a integer.
- The Arduino String object can handle a
float
.
Only float
for avr microcontrollers, no double
.
The avr microcontrollers are 8-bit microcontrollers. There is no floating point library for 64-bit double
variables, only a library for 32-bit float
variables.
That library is according to the IEEE standard and is extremely fast and optimized.