Live data from Hacker News

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

github.com

41–50 of 152 posts

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

#41
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…

[deleted]

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

#43
post #6

Apples libc used to shell-out to perl in a function: https://github.com/Apple-FOSS-Mirror/Libc/blob/2ca2ae7464771...

I thought this is what the Unix philosophy is supposed to be all about.

(Realistically, calling wordexp should just abort the program. Now I actually want to make a hacked up musl that aborts in all the various "libc functions no one should ever use" and see how far I get into a Ubuntu boot..)

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

#45
post #6

Apples libc used to shell-out to perl in a function: https://github.com/Apple-FOSS-Mirror/Libc/blob/2ca2ae7464771...

Which raises the question what libc functions perl calls...

And imagine the debug errors:

>perl error X

"But I'm just calling libc ?!?"

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

#46
post #22

Earlier quoted context omitted.

It seems to just be layman shorthand for storing (not huge) integers as binary isn't lossy.

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

I believe that's where "natively" comes in. If you store it in binary, you can operate on the values as numbers. If you do some decimal conversion, it's a blob of data. (Yes, there are BCD instructions but they're massively limited)

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

#47
post #17

Earlier quoted context omitted.

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…

How does truncating a positive number ever round up?

Add 0.5 before rounding towards negative infinity, and you'll get standard rounding.

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

#48
post #5

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

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

The memory management will virtually never kick in. You'd need a number which expands to more than 100 characters for that to happen.

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

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

round() is specifically about rounding to decimal places, and there are other, faster functions for the more common cases

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

#50
post #22

Earlier quoted context omitted.

It seems to just be layman shorthand for storing (not huge) integers as binary isn't lossy.

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

Decimal numbers are still "stored as binary" at the silicon level.
Post reply on HN