Live data from Hacker News

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

github.com

51–60 of 152 posts

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

#51
My quick impression is that the choice of a rounding algorithm is relative to the purpose that it serves. For instance, floor(x + 0.5) is good enough in many applications.

In some cases, rounding is performed for the primary purpose of displaying a number as a string, in which case it can't be any less complicated than the string conversion function itself.

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

#52
post #4

https://0.30000000000000004.com/

Whoa. In FF, I just see a screenfull of blank boxes. I scroll down and see content wrongly rendered. On Chrome its a dated design with an uncomfortable text size, and the narrow column creates boxes where I have to use horizontal scrollbars. The Motherfucking Website revolution cant come too soon enough!

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

#54
post #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 ve…

The problem is the division after truncation. That division by a power of 10 can produce errors in binary.

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

#55
post #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 fas…

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

The C/C++ standards do not require formatting to round correctly or even be portable. I recently had an issue where a developer used this method to round floats for display, and there were differences on PC and on Mac. It literally rounded something like 18.25 to 18.2 on one platform and 18.3 on the other. This led to all sorts of other bugs as some parts of the program used text to transmit data, which ended up in weird states.

The culprit was this terrible method. If you want anything approaching consistency or predictability, do not use formatting to round floating point numbers. Pick a numerically stable method, which will be much faster of done correctly.

Coincidentally, C/C++ do not require any of their formatting and parsing routines to round-trip floating point values correctly (except the newly added hex formatted floats which are a direct binary representation, and some newly added function allowing an obscure trick I do not recall at the moment... )

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

#56
post #42

This is what we are promised will make trucks drive themselves and usher in the 4th industrial revolution.

I am not sure if people understand what kind of hellhole is IT in general.

There is an XKCD for that: https://www.xkcd.com/2030/

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

#57
post #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 fas…

>Seems to be one of the best ways to go about it. The C/C++ standards do not require formatting to round correctly or even be portable. I recently had an issue where a developer used this method to round floats for display, and there were differences on PC and on Mac. It literally rounded something like 18.25 to 18.2 on one platform and 18.3 on the other. This led to all sorts of other bugs as some parts of the progr…

> The C/C++ standards do not require formatting to round correctly or even be portable.

The linked-to method uses PyOS_snprintf(). Its documentation at https://docs.python.org/3/c-api/conversion.html says:

"""PyOS_snprintf() and PyOS_vsnprintf() wrap the Standard C library functions snprintf() and vsnprintf(). Their purpose is to guarantee consistent behavior in corner cases, which the Standard C functions do not."""

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

#58
post #4

https://0.30000000000000004.com/

Whoa. In FF, I just see a screenfull of blank boxes. I scroll down and see content wrongly rendered. On Chrome its a dated design with an uncomfortable text size, and the narrow column creates boxes where I have to use horizontal scrollbars. The Motherfucking Website revolution cant come too soon enough!

Renders just fine for me in Firefox on Ubuntu.

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

#59

Earlier quoted context omitted.

Storing integers as decimal (which computers can do easily) isn't lossy either.

Decimal numbers are still "stored as binary" at the silicon level.

Unless you're on an IBM 1401... :D

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

#60
post #27
post #11

Earlier quoted context omitted.

So efficient that nobody really cared about or mentioned it all those decades, so there's that...

It's Python, it's not like anybody is going to complain about performance, because nobody expects any.

If this implementation was a bottleneck in anything, people would have complained, as they have in other areas in Python. There are performance improvements in places people complain about all the time.

Besides you missed the part where this is used in other speed-critical places, like protobuf, the K&P implementation, etc.

Post reply on HN