Closed
Description
Truncation during conversion from floating point to integer types is non-obvious behavior that frequently causes confusion for beginners who expect rounding to occur. Example:
float x = 2.9;
int y = x; // 2
Rounding could be achieved by adding 0.5 during the conversion:
float x = 2.9;
int y = x + 0.5; // 3
or the use of round()
:
float x = 2.9;
int y = round(x); // 3
This should be documented in the float and double reference pages.
Originally suggested by PaulStoffregen at arduino/Arduino#8751 (comment)