9 ms·
Maybe I'm missing something but what's wrong with rounding floats this way?
by ChrisSD 7y ago
Maybe I'm missing something but what's wrong with rounding floats this way?
- f00zz 7y agoThat's what round(3) is for
- masklinn 7y agoround(3) can only round to an integer. Python's round works to an arbitrary decimal position. It would previously scale up, round (ceil/floor really) then scale down. That turned out to induce severe precision issues: https://bugs.python.org/issue1869 https://bugs.python.org/issue1869
- f00zz 7y agoThat's the first thing I'd try, multiply by 10^digits, round, divide by 10^digits. Thanks for the link.
- bottled_poe 7y agoThe two concerns I have are performance and correctness. I don’t know enough about the implementation of round(3) to know... perhaps someone else does?
- dagw 7y agoThis approach is used specifically because of correctness. Doing things the 'obvious' way with round(3) or truncation introduces precision problems in corner cases.
- emsy 7y agoPython already doesn't have the best performance. If you need to round a lot of floats in a loop you better bring some time.
- yjftsjthsd-h 7y agoI don't know that it is "wrong", just unexpected. I suspect most people expect all math functions to be purely implemented in numerical terms, so finding string manipulation is surprising/interesting.
- CogitoCogito 7y ago> I don't know that it is "wrong", just unexpected. I suspect most people expect all math functions to be purely implemented in numerical terms, so finding string manipulation is surprising/interesting. You kind of got me thinking now. The decimal representation of a number is really a string representation (in the sense of a certain sequence of characters). Hence rounding to a certain decimal is essentially a string operation. You can of course do it by (say) dividing by 10^whatever or something else in some numerical fashion, but the more I think about it, the more natural it is to just think of the whole thing as a string. Or you could flip it around and consider that the string manipulation can also be described numerically so whether you consider the operation as a string operation or a numerical operation is sort irrelevant. It's just a point of view.
- kthejoker2 7y agoThis is one of the core ground/figure themes in Godel Escher Bach.
- Frost1x 7y agoI think the best way to think about it is as a symbolic representation. We have processes for manipulating symbols to achieve the correct results. Purely numeric (binary based) operations just happen to allow for some quicker shortcuts but sometimes lead go lost information.
- xamuel 7y ago>The decimal representation of a number is really a string representation It is incorrect to speak of "the" decimal representation of a number, as many numbers have non-unique decimal representations, the most famous example being 1.000...=0.999...
- ben509 7y agoRounding a number is, in the common case, multiplying it by some base, truncating to an integer, and dividing by the base. You do have to handle extremely high exponents, but even the logic for that is not complex. Example of implementing it the sane way: https://github.com/numpy/numpy/blob/75ea05fc0af60c685e6c071dae1f04c489a3ce93/numpy/core/src/multiarray/calculation.c#L667 https://github.com/numpy/numpy/blob/75ea05fc0af60c685e6c071d... Every step of this function is complex and expensive, especially printing a float as a decimal is very complex. And round is routinely used in a tight loop.
- dagw 7y agoThe numpy approach sacrifices correctness for speed (you sometimes get unexpected results in some corner cases, see below), the cpython way sacrifices speed for correctness. >>> round(56294995342131.5, 2) 56294995342131.5 >>> round(56294995342131.5, 3) 56294995342131.5 >>> np.round(56294995342131.5, 2) 56294995342131.5 >>> np.round(56294995342131.5, 3) 56294995342131.51
- Armisael16 7y agoHow does truncating a positive number ever round up?
- NohatCoder 7y agoAdd 0.5 before rounding towards negative infinity, and you'll get standard rounding.
- deleted 7y ago[deleted]
- deleted 7y ago[deleted]
- deleted 7y ago[deleted]
- phkahler 7y ago
- Aardwolf 7y agoThey're using base 10 which is much slower to use here than power of 2 bases would be. Plus then also the memory management for the string.
- viraptor 7y agoThe memory management will virtually never kick in. You'd need a number which expands to more than 100 characters for that to happen.