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…
Python rounds float values by converting them to string and then back
41–50 of 152 posts
Re: Python rounds float values by converting them to string and then back
#42Re: Python rounds float values by converting them to string and then back
#43Apples libc used to shell-out to perl in a function: https://github.com/Apple-FOSS-Mirror/Libc/blob/2ca2ae7464771...
(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
#44Re: Python rounds float values by converting them to string and then back
#45Apples libc used to shell-out to perl in a function: https://github.com/Apple-FOSS-Mirror/Libc/blob/2ca2ae7464771...
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
#46Earlier 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.
Re: Python rounds float values by converting them to string and then back
#47Earlier 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?
Re: Python rounds float values by converting them to string and then back
#48Maybe 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.
Re: Python rounds float values by converting them to string and then back
#49Maybe 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.