Python rounds float values by converting them to string and then back
1–10 of 152 posts
Re: Python rounds float values by converting them to string and then back
#2Related thread on Twitter https://twitter.com/whitequark/status/1164395585056604160
Re: Python rounds float values by converting them to string and then back
#3I dunno, how efficient is this?
Re: Python rounds float values by converting them to string and then back
#4Re: Python rounds float values by converting them to string and then back
#5Maybe I'm missing something but what's wrong with rounding floats this way?
Re: Python rounds float values by converting them to string and then back
#6Apples 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
#7Maybe I'm missing something but what's wrong with rounding floats this way?
That's what round(3) is for
Re: Python rounds float values by converting them to string and then back
#8Maybe 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
#9Maybe 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.
Re: Python rounds float values by converting them to string and then back
#10Maybe 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.