Hacker News new | comments | ask | show | jobs | submit login

code iterating along some line segments that forgot to account for the possibility of a zero-length segment

Out of curiosity, could you describe what you were doing and why 1/0 = 0 would have returned the correct result?

Hah, you know what, looking more closely at the code, the bug was not what I thought, and while the code doesn't crash now, it will still do the wrong thing!

So the code was given a path described by line segments p0-p1-p2-...pN, and what it wanted to do was find the point s distance along the path.

It's iterating over pairs of points (pI, pJ) and looking at the segment length. Is the segment length > the remaining length? If so, then we know our point is somewhere on the segment (pI, pJ). In particular, it is (remaining length) divided (segment length) pct along the segment.

This code was dying on zero-length segments (i.e., a repeated point) because of the divsion above. I wasn't thinking too clearly and fixed the problem by just skipping 0 length segments.

But actually, the only way that code can even trigger is if the original distance along the path was negative, in which case we actually just want to always return the start of the path.

So, in summary, my particular case was buggy either way, and 1/0 behavior doesn't hurt or help (except it caused a crash instead of an incorrect line in some cases; not really sure if that's better or worse).

For the case where we do have a zero-length segment, 1/0 = 0 would give desired behavior (just scale to first point in segment). (But of course, when it's a zero length segment, any value would be OK, since it would just get multiplied by zero again.)

Applications are open for YC Summer 2019

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact