Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
per1234 opened this issue Apr 5, 2019 · 0 comments
Closed

Comments

@per1234
Copy link
Collaborator

per1234 commented Apr 5, 2019

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)

animeshsrivastava24 added a commit to animeshsrivastava24/reference-en that referenced this issue May 29, 2019
Addressing the Issue arduino#579. Information related to Truncation and Round Off Addition is added.
This was referenced May 29, 2019
animeshsrivastava24 referenced this issue in animeshsrivastava24/reference-en May 31, 2019
@per1234 per1234 closed this as completed in 2e4da45 Jun 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant