Skip to content

Document truncation when converting from float/double to an integer type #579

Closed
@per1234

Description

@per1234

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions