Live data from Hacker News

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

github.com

121–130 of 152 posts

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

#121
post #14

Seems to be one of the best ways to go about it. From the comment in protobuf source (which does the same thing as Python), mentioned in the Twitter thread: (...) An arguably better strategy would be to use the algorithm described in "How to Print Floating-Point Numbers Accurately" by Steele & White, e.g. as implemented by David M. Gay's dtoa(). It turns out, however, that the following implementation is about as fas…

>Seems to be one of the best ways to go about it. The C/C++ standards do not require formatting to round correctly or even be portable. I recently had an issue where a developer used this method to round floats for display, and there were differences on PC and on Mac. It literally rounded something like 18.25 to 18.2 on one platform and 18.3 on the other. This led to all sorts of other bugs as some parts of the progr…

IEEE754 defines 5 rounding modes. This one sounds like nearest, ties to even. Not all decimal values are representable as float. Depending on if you compile for x87 (80-bit internal repreaentation) or SSE (64-bit) you might get slightly different results.

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

#123

Earlier quoted context omitted.

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

There's much truth in this.

Whenever I've helped older people with technology they've never used before (a new tablet or similar), if I started off with any suggestion that it's less than simple, they'll almost certainly frame the problem scope in their mind as difficult, and give up, because they're already exhibiting some animosity toward learning a new thing.

If instead you phrase it as "this is really easy, let me show you how...", you short-circuit this process by framing their expectations differently, and that little bit of extra confidence ("this is easy") can help them through the learning process.

I've found you can't simply show them, either. It's almost better to say "It's easy" and then go through the process, because it's absolutely necessary to establish expectations first. They're already afraid of it (it's new), so doing something to get their guard down can go a long way toward helping them explore on their own. I tried this experiment with my mother, and some weeks later she'd have a problem and discover the solution herself specifically because she was convinced it was easy to do. This can (and will) backfire if you're not careful about how you do it, but I've had far more success using this tool than other techniques individually (e.g. writing down instructions).

This doesn't broadly apply to areas outside education and support (or even to all areas in education), but for simple things that people may express an irrational fear over, it works and it works well. A good teacher will use this technique successfully with their students, so if you're teaching someone, use it!

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

#124

A bit on topic... Is there a phrase for the ratio between the frequency of an apparent archetype of a bug/feature and the real-world occurrences of said bug/feature? If not then perhaps the "Fudderson-Hypeman ratio" in honor of its namesakes. For example, I'm sure every C programmer on here has their favored way to quickly demo what bugs may come from C's null-delimited strings. But even though C programmers are quic…

What's the 'class' of the second thing? Numerics/fp bugs of all stripes are super common. Just often less crashy or noticeable.

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

#125

Earlier quoted context omitted.

Fun fact: floor(x + 0.5) rounds 0.49999997 to 1.0 (this is 32 bit floats, the same principle applies to 64). Most libraries have slower than ideal round conversion because of historical dross; modern chips have a very fast SIMD round instruction but its behavior doesn't exactly match libc round. See https://github.com/rust-lang/rust/issues/55107 for a deeper discussion.

I just tried this on Python3 on a 64-bit x86 system: import math x = 0.49999999999999994 print(x-0.5) print(math.floor(x+0.5)) I got these printouts: -5.551115123125783e-17 1 So yes, something less than 1/2, with 1/2 added to it, has a floor of 1 in floating point math. Yet another reminder that floating point calculations are approximations , and not exact.

[deleted]

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

#126
post #68
post #43

Earlier quoted context omitted.

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

I seem to recall that perl used to shell out to /bin/sh for some related task...

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

#127
post #93
post #85

Earlier quoted context omitted.

Why not have optimized versions that use native instructions when available, and then fall back to the portable version when they are not?

I'm unsure as to why they don't do that, I suspect it's because nobody using Python has found floating-point rounding to be a bottleneck yet.

I have encountered situations where irregular rounding became solvable but annoyingly problematic to detect / calculate, in the LANL Earthquake dataset on Kaggle, it had a column with samples and a column with (incorrectly incrementing) sample times that were rounded. In order to create a corrected column, I noticed quite a lot of irregularities in the python rounding (or the underlying mechanism)

I also consider simultaneously deterministic, fast, portable rounding or binary formats to be important for decentralized delegation of computation, say a TrueBit style platform:

https://people.cs.uchicago.edu/~teutsch/papers/truebit.pdf

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

#128
post #121

Earlier quoted context omitted.

>Seems to be one of the best ways to go about it. The C/C++ standards do not require formatting to round correctly or even be portable. I recently had an issue where a developer used this method to round floats for display, and there were differences on PC and on Mac. It literally rounded something like 18.25 to 18.2 on one platform and 18.3 on the other. This led to all sorts of other bugs as some parts of the progr…

IEEE754 defines 5 rounding modes. This one sounds like nearest, ties to even. Not all decimal values are representable as float. Depending on if you compile for x87 (80-bit internal repreaentation) or SSE (64-bit) you might get slightly different results.

They already had the same float, though. That's not very likely if the rounding modes were different.

For what it's worth, it looks like different standard libraries make different choices on whether float->string conversion cares about the current rounding mode.

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

#129
post #6

Apples libc used to shell-out to perl in a function: https://github.com/Apple-FOSS-Mirror/Libc/blob/2ca2ae7464771...

Die ganzen Zahlen hat der liebe Gott gemacht, alles andere ist Menschenwerk.

https://en.wikipedia.org/wiki/Leopold_Kronecker

Post reply on HN