6 ms·
Divine By Zero
- deleted 15y ago[deleted]
- knieveltech 15y agoOh man, this gave me a flashback to 4 years spent working for a large newspaper company trying to keep the affiliate papers' "developers" in check. Needless to say I don't miss that job at all. After spending a couple years specializing with Drupal I'd almost forgotten how ridiculous it can get out in the trenches.
- aditya42 15y agoDon't mean to be a buzzkill here, but I don't think anyone with an ounce of a brain will find this funny, which I believe was the original intent.
- wccrawford 15y agoDaily WTF is all about cringe-worthy programming stories. If you find them funny, so be it... But they're almost all cringe-worthy.
- deleted 15y ago[deleted]
- Revisor 15y agoIs there an opposite website to The Daily WTF - a collection of short, elegant and clever code?
- epenn 15y agohttp://rosettacode.org http://rosettacode.org is a wiki with concise algorithms/solutions/snippets in as many languages as you can think of. The algorithms listed range from mundane to clever to anywhere in between. If you haven't already been there it's definitely worth a look.
- zitterbewegung 15y agoThere is a book called Beautiful Code by O'Rielly http://oreilly.com/catalog/9780596510046 http://oreilly.com/catalog/9780596510046
- Revisor 15y agoThank you for your answers. To add something myself, the book Programming Pearls contains exactly this - short, elegant solutions, plus some extra food for thought and exercises. http://www.amazon.com/Programming-Pearls-2nd-Jon-Bentley/dp/0201657880 http://www.amazon.com/Programming-Pearls-2nd-Jon-Bentley/dp/...
- steve_b 15y agoI was confused about the "php" == 0, until I found http://php.net/manual/en/types.comparisons.php http://php.net/manual/en/types.comparisons.php. So "php" == 0 is true and "php" == true is true, but 0 == true is false. Yikes! It seems like comparisons in PHP are as bad as they are in JS. I have a hard time believing this is real. Does WTF do any verification?
- graywh 15y agoThose PHP "features" are the real WTF.
- rrrazdan 15y agoThe comments on this page are better than the code. Ok no they are not.
- dhimes 15y agoI actually LOL'd at geordish's, though.
- deleted 15y ago[deleted]
- VilleSalonen 15y agoI love the flowchart. "Ah, now it all makes sense!" I even printed it on the wall at my work right next to Ballmer Peak strip.
- StavrosK 15y agoCan someone explain why he does two more if checks after he's set the denominator to "php"?
- softbuilder 15y agoHe's an idiot.
- ary 15y agoCan't tell if you're being sarcastic or not. :) Aside from the whole function being asinine the only way to know "why" would be to ask him. The answer would probably also qualify as asinine.
- p4bl0 15y agoDon't you think this is made up? I can't believe someone actually wrote that.
- ary 15y agoLets say for a moment that this isn't made up. The questions that struck me immediately are as follows: 1. What was the motivation for creating this function? Was it (hopefully) to show off how "smart" he was? Was the company he worked for doing any kind of calculations related to (dear god no) financial or medical data that he thought he could improve? 2. How does anyone in this world escape grade school without knowing that one cannot divide by zero? 3. Someone thought dividing by zero should be a recoverable error?
- jmelloy 15y agoI've had situations where I'm calculating a ratio or something for each member of a result set, and needed a "safe_divide" function. Mine was much less complex than this one, but returned 0 if the denominator was 0.
- blahblahblah 15y agoRegarding #3: Division by zero should be a recoverable error in many programming situations. That's part of the beauty of NaN as defined by the IEEE standard for floating point numbers. It's quite a handy thing to be able to just do the operation and sort out the NaNs afterward. Suppose, for example, that you want to calculate the pixel-wise percent change in intensity between two grayscale images A and B. In a language with good built-in support for matrix datatypes, such as MATLAB, the ability to handle division by zero gracefully can quite often simplify the code. result = (A-B)./A*100; Now I can just use the isfinite function to produce a matrix of logical values that tells me which pixel locations have valid values (simultaneously handling the case of numerical overflow). Granted, this doesn't do much to simplify the code above. We could have just checked beforehand to find the pixels of A that had zero values. However, if you have a complicated expression involving multiple divisions, logarithms, or other functions that are undefined for some portion of the real numbers, treating these situations as "recoverable" in some sense allows you to write cleaner, more readable code if your implementation language permits you to just do the operation and check for NaN values (+Inf and -Inf too) afterward.
- kgermino 15y agoAs much as I love TDWTF I have to say I'm disappointed to see a submission from there get 46 upvotes on the front page. Great story though
- Natsu 15y agoMaybe they're hoping that someone can explain what this person was thinking? I doubt it, but I guess it's at least possible that someone here works with the criminally insane.
- lhnz 15y agoYeah, agreed. The whole premise of the function is insane but I'm pretty sure the story is a lie because I just can't see how anybody would write the code this way unless they were intentionally trying to obfuscate code for humour/annoyance.