4 ms·
Can you give an example of when this wouldn't work? I'm definitely not qualified to write a float library (lol) but I also can't think of any cases where that w
by Exuma 6y ago
Can you give an example of when this wouldn't work? I'm definitely not qualified to write a float library (lol) but I also can't think of any cases where that wouldn't work?
- brianush1 6y agoceil(5) = round(5.5) = 6 ceil(5) should be 5
- brucedawson 6y agoTo 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.
- zelphirkalt 6y agoThis is probably about number precision and loosing digits, when you add something with digits only in the beginning like 0.5 to something small like 0.1000000000000000000000000000013124142579275927597245205 or whatever a barely representable floating point number is. Then the result becomes inprecise and rounding to nearest will result in the wrong value. Such effects and similar should be well known to anyone taking on the responsibility to develop a floating point library.
- NelsonMinar 6y agoEvery odd integer? The original article discusses it in great detail.