Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

ceil(5) = round(5.5) = 6

ceil(5) should be 5



To expand on this, if you have an integer and you add 0.5 then a round() function has to make a choice, because it is halfway between integers. The IEEE standard says that rounding in general (not just for the round function) should round-to-nearest-even. That means that rounding should go to the closest result, and if there is a tie then it should go to the even number.

So, if ceil(x) is implemented as round(x+0.5) then: ceil(4) = round(4.5) = 4 ceil(5) = round(5.5) = 6

The first of those is correct. The second is incorrect.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: