Live data from Hacker News

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

github.com

21–30 of 152 posts

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

#22
post #15
post #4

https://0.30000000000000004.com/

Computers can only natively store integers, so they need some way of representing decimal numbers. Really? How do computers "natively" store integers?

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

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

#23
post #7

Earlier 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

That's the first thing I'd try, multiply by 10^digits, round, divide by 10^digits. Thanks for the link.

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

#25

Earlier 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…

This is one of the core ground/figure themes in Godel Escher Bach.

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

#27
post #11
post #3

I dunno, how efficient is this?

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.

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

#28
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 numpy approach sacrifices correctness for speed (you sometimes get unexpected results in some corner cases, see below), the cpython way sacrifices speed for correctness.

  >>> 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.51

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

#29

Earlier 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…

I think the best way to think about it is as a symbolic representation. We have processes for manipulating symbols to achieve the correct results. Purely numeric (binary based) operations just happen to allow for some quicker shortcuts but sometimes lead go lost information.

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

#30
Well, the problem is precisely that rounding as it is generally conceived, is expressed in base 10 - as we generally conceive numbers including floating point ones in base 10. Yet at the lowest level, the representation of numbers is in base 2, including floating point ones. It is imaginable, would be more correct and efficient to perform rounding (or flooring or ceiling, for that matter) in base 2, but it would be that more difficult to comprehend when dealing with non integers in code. Rounding in base 10 needs some form of conversion anyway, going for the string is one way that is, at least, readable (pun intended).
Post reply on HN