Python rounds float values by converting them to string and then back
21–30 of 152 posts
Re: Python rounds float values by converting them to string and then back
#22Re: Python rounds float values by converting them to string and then back
#23Earlier quoted context omitted.
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
#24Apples libc used to shell-out to perl in a function: https://github.com/Apple-FOSS-Mirror/Libc/blob/2ca2ae7464771...
/* XXX this is _not_ designed to be fast */Re: Python rounds float values by converting them to string and then back
#25Earlier quoted context omitted.
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…
Re: Python rounds float values by converting them to string and then back
#26Apples libc used to shell-out to perl in a function: https://github.com/Apple-FOSS-Mirror/Libc/blob/2ca2ae7464771...
Re: Python rounds float values by converting them to string and then back
#27Re: Python rounds float values by converting them to string and then back
#28Maybe 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…
>>> round(56294995342131.5, 2)
56294995342131.5
>>> round(56294995342131.5, 3)
56294995342131.5
>>> np.round(56294995342131.5, 2)
56294995342131.5
>>> np.round(56294995342131.5, 3)
56294995342131.51Re: Python rounds float values by converting them to string and then back
#29Earlier quoted context omitted.
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…