Live data from Hacker News

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

github.com

61–70 of 152 posts

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

#62
I was looking once at Python and Redis and how numbers get stored. I remember Python would in the end send Redis some strings. I dove pretty deep and found that Python floats when turned into a string and then back are exactly the same float.

I remember even writing a program that tested every possible floating point number (must have only been 32 bit). I think I used ctypes and interpreted every binary combination of 32 bits as a float, turned it into a string, then back and checked equality. A lot of them were NaN.

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

#63
post #24
post #6

Apples 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 */

it is hard to do without a subshell). It is probbably just plan a Bad Idea to call in anything setuid, or executing remotely. */

laughing so hard :')

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

#64
post #33
post #4

https://0.30000000000000004.com/

Somewhat offtopic, but is there a reason some many explanations of this issue lump together the fundamental principle of how numbers are represented (integers vs. fractions vs. exact reals (technically impossible) vs. IEEE 754) and the base (decimal vs. binary)? Every time I read something like the explanation on that site, I wonder if I would understand it if I didn't knew it already.

The "why does this happen" on that page hits on it, at least tangentially. A lot of what's confusing about IEEE floats isn't the inability to represent all rationals in and of itself, it's more that the particular patterns of inaccuracy end up being different between the computer approximation and the approximations we'd make on paper, because of the different numeric bases.

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

#66

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…

>The decimal representation of a number is really a string representation

It is incorrect to speak of "the" decimal representation of a number, as many numbers have non-unique decimal representations, the most famous example being 1.000...=0.999...

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

#67
post #4

https://0.30000000000000004.com/

The worst way to explain something is to begin with "It's actually pretty simple."

Not always! E.g Let's say someone begin to explain me a topic and use e.g two words that I didn't knew before: e.g supervenience and congency.

Humans generally are afraid of new words (especially weird sounding ones) and often will assume that the subject is complex and might intimidate them.

But unknown words can have extremely simple meanings, or be synonyms of already known words.

By asserting: "It's actually pretty simple" You give them confidence that there's not reason to be afraid of the topic or of the words.

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

#68
post #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..)

Would be pretty awesome if Perl called wordexp(3) somewhere along this code path

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

#70
post #66

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

>The decimal representation of a number is really a string representation It is incorrect to speak of "the" decimal representation of a number, as many numbers have non-unique decimal representations, the most famous example being 1.000...=0.999...

> It is incorrect to speak of "the" decimal representation of a number, as many numbers have non-unique decimal representations, the most famous example being 1.000...=0.999...

That's true and I'll concede that point, but it's not really relevant to what I said. That just means some numbers have different string representations that represent the same object. That doesn't really contradict anything in my post except the use of the definite article.

Post reply on HN