I dunno, how efficient is this?
Python rounds float values by converting them to string and then back
11–20 of 152 posts
Re: Python rounds float values by converting them to string and then back
#12Re: Python rounds float values by converting them to string and then back
#13Maybe I'm missing something but what's wrong with rounding floats this way?
That's what round(3) is for
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
#14From 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
#15https://0.30000000000000004.com/
Computers can only natively store integers,
so they need some way of representing decimal numbers.
Really? How do computers "natively" store integers?Re: Python rounds float values by converting them to string and then back
#16I dunno, how efficient is this?
Re: Python rounds float values by converting them to string and then back
#17Maybe I'm missing something but what's wrong with rounding floats this way?
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
#18Maybe 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?
Re: Python rounds float values by converting them to string and then back
#19I dunno, how efficient is this?
Re: Python rounds float values by converting them to string and then back
#20Maybe 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.
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.