Timeline for Rounding off to nearest number
Current License: CC BY-SA 4.0
7 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| May 17, 2018 at 7:29 | history | edited | ilkkachu | CC BY-SA 4.0 | let's also fix the explanation here then |
| May 17, 2018 at 7:21 | comment | added | αғsнιη | Yes, my bad. you are right | |
| May 17, 2018 at 7:20 | comment | added | ilkkachu | as a floor function, it was broken. 1.3 should floor to 1, not to 0. Again, the point is that the float to int conversion done by %d truncates, i.e. lops off the whole fractional part. It doesn't do any rounding, that's why you need the + 0.5 to begin with. If you want to round towards zero to the nearest integer, it's enough to just use printf("%d", x) | |
| May 17, 2018 at 7:12 | history | edited | αғsнιη | CC BY-SA 4.0 | deleted 54 characters in body |
| May 17, 2018 at 7:03 | comment | added | ilkkachu | Here, too, printf("%d", x - .5) doesn't round x down correctly: %d truncates, so if x=1.3, x-.5=.8, then the truncated value is 0. | |
| May 17, 2018 at 6:43 | history | edited | αғsнιη | CC BY-SA 4.0 | edited body |
| May 17, 2018 at 6:37 | history | answered | αғsнιη | CC BY-SA 4.0 |