Live data from Hacker News

Python rounds float values by converting them to string and then back

github.com

11–20 of 152 posts

Re: Python rounds float values by converting them to string and then back

#12
post #9
post #5

Maybe I'm missing something but what's wrong with rounding floats this way?

Python already doesn't have the best performance. If you need to round a lot of floats in a loop you better bring some time.

Or use numpy, like the rest of us.

Re: Python rounds float values by converting them to string and then back

#13
post #7
post #5

Maybe I'm missing something but what's wrong with rounding floats this way?

That's what round(3) is for

round(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

Re: Python rounds float values by converting them to string and then back

#14
Seems to be one of the best ways to go about it.

From the comment in protobuf source (which does the same thing as Python), mentioned in the Twitter thread:

(...) An arguably better strategy would be to use the algorithm described in "How to Print Floating-Point Numbers Accurately" by Steele & White, e.g. as implemented by David M. Gay's dtoa(). It turns out, however, that the following implementation is about as fast as DMG's code. Furthermore, DMG's code locks mutexes, which means it will not scale well on multi-core machines. DMG's code is slightly more accurate (in that it will never use more digits than necessary), but this is probably irrelevant for most users.

Rob Pike and Ken Thompson also have an implementation of dtoa() in third_party/fmt/fltfmt.cc. Their implementation is similar to this one in that it makes guesses and then uses strtod() to check them. (...)

https://github.com/protocolbuffers/protobuf/blob/ed4321d1cb3...

Re: Python rounds float values by converting them to string and then back

#17
post #5

Maybe I'm missing something but what's wrong with rounding floats this way?

Rounding 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/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.

Re: Python rounds float values by converting them to string and then back

#18
post #5

Maybe I'm missing something but what's wrong with rounding floats this way?

The 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?

This approach is used specifically because of correctness. Doing things the 'obvious' way with round(3) or truncation introduces precision problems in corner cases.

Re: Python rounds float values by converting them to string and then back

#20
post #5

Maybe I'm missing something but what's wrong with rounding floats this way?

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.

> 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.

Post reply on HN