6 ms·
How is this wrong?
by SomeoneFromCA 3y ago
How is this wrong?
- javcasas 3y agohttps://docs.python.org/3/library/functions.html#in https://docs.python.org/3/library/functions.html#in For floats it truncates towards zero. positive_round(-1.2) is 0. Also, what is the value of positive_round("1")? I hope it is an exception, otherwise it will be 10.
- SomeoneFromCA 3y agoWhy? In python you cannot implicitly coerce string to number, of course it will throw exception.
- Kim_Bruning 3y agoAs it is, it's correct for positive numbers. In some languages (int) (x+0.5) is one of the simplest ways to do it, really. It just reminds me of the same sort of thinking. Might just be me though.
- reichstein 3y agoNot guessing at what the original author intended, but the formula `floor(x + 0.5)` does not currently round to the nearest integer for all floating point values. If 0.5 is just outside the bits of the mantissa, there will be an intermediate rounding before doing the `floor`. One such value is 0.49999999999999994, another is 4503599627370497.0
- Kim_Bruning 3y agoOh No. That one isn't even on https://www.ethan-shea.com/posts/falsehoods-about-floats https://www.ethan-shea.com/posts/falsehoods-about-floats yet.