You're looking at Less Wrong's discussion board. This includes all posts, including those that haven't been promoted to the front page yet. For more information, see About Less Wrong.

Vladimir_M comments on Specification failure - Less Wrong Discussion

5 Post author: cousin_it 04 November 2010 06:56PM

You are viewing a comment permalink. View the original post to see all comments and the full post content.

Comments (18)

You are viewing a single comment's thread.

Comment author: Vladimir_M 04 November 2010 09:12:58PM *  1 point [-]

Tangentially related: a blogger asks visitors to write a correct binary search routine without testing it even once, then test it and report the results.

Haha, it really is more tricky than it sounds. I succeeded (it passed a bunch of tests including various corner cases on first compilation), but I'm not sure if it would be correct if I hadn't been more cautious than usual.

I'm not surprised, though. The human capacity for formal logical inference is extremely unreliable and short-sighted, even for very intelligent people.

I succeeded - look for "Vladimir Slepnev" - but it was a surreal experience.

Did you consider that comparing floating point numbers with '==' doesn't make much sense? (I assume it implements exact value comparison in this language you used, like in C.)

Comment author: cousin_it 05 November 2010 10:32:05AM *  1 point [-]

Yeah, I did the same thing with tests. Wrote about ten of them and rigged the suite to scream "YOU FAIL" at me without even printing the line number. Couldn't believe it when they all passed on the first try :-)

Did you consider that comparing floating point numbers with '==' doesn't make much sense?

No. What do you propose? Use some sort of "comparison tolerance"?

Comment author: Sniffnoy 05 November 2010 09:00:53PM 2 points [-]

In this context, I think the answer is don't write it for floats!

Comment author: Vladimir_M 05 November 2010 08:07:00PM *  1 point [-]

cousin_it:

No. What do you propose? Use some sort of "comparison tolerance"?

That depends on the concrete numerical application. Typically you'd define some threshold for the difference, and use "abs(x-y)<threshold" instead of "x==y", or some variation on that theme. But exact bit-by-bit equality comparisons of floats practically never make sense, since even very simple arithmetic will likely produce rounding errors, so seeing '==' between floats almost always means that something nonsensical is going on. (See e.g. here for some elaboration.)

These issues with limited floating point precision make numerical programming extremely tricky. You have a whole alternative set of rules for arithmetic, very different from those you normally follow on paper.

Comment author: wnoise 09 November 2010 03:18:06AM 0 points [-]

I think I'd have a different implementation for floats that would return the nearest two it's between, and punt the tolerance decision up a level to the calling code.