• teambob (unregistered) in reply to Me
    It looks like the flow chart is incorrect. The decision, "is $n == 0 ?" is flipped. It should only go forward to "is $n == 1 ?" if it's False. :)

    ... so I'm not going crazy then. :)

  • Nils (unregistered)

    Hmmm, who ever except some much less divine colleagues would ever have the chance to read this self-worshiping comments. Thanks to this article now the world knows.

    Besides that, he definitely deserves the Nobel prize for solving a virulent problem of mathematics so elegantly, that has been a pain in the ass of several generations of mathematicians.

    Maybe the next step would be the definition of a datatype to handle infinite numbers.

  • Sylver (unregistered)

    Oh. MY. F*µ£C%Z£Ë%Zµ£Eµ£ZE££µEZ£. GOD.

    This has to be the best code SOD in the history of this website.

  • Carra (unregistered)

    You're assuming it should average to 0.

    It should averate to plus or min infinity.

  • (cs) in reply to AA
    AA:

    The way it's supposed to work is that you throw an exception, and then something at a higher level of logic that knows how to handle it appropriately (for instance, by picking a different equation to try in this case) can do so.

    Why would you try something and wait for it to break, then do something else, rather than look ahead to see if it might break then avoid the break ahead of time so you don't have to try it twice?

    Of course, I mostly work with real-time systems so the impact of trying something twice is significant.

  • (cs)

    This is really work of the God of PHP, genius in it's own simplicity.

    You just don't get it.

    PHPGod has to do a division by Zero.

    So he does simple php Math.

    We know that in PHP

    0 == "php" == TRUE ==(1 || -1)

    ergo

    $x/0==$x/"php"==$x/TRUE==$x/(1 || -1)

    In PHP division by Zero is equal of division by 1 or -1.

    Brilliant!

  • Jay (unregistered) in reply to too_many_usernames
    too_many_usernames:
    AA:

    The way it's supposed to work is that you throw an exception, and then something at a higher level of logic that knows how to handle it appropriately (for instance, by picking a different equation to try in this case) can do so.

    Why would you try something and wait for it to break, then do something else, rather than look ahead to see if it might break then avoid the break ahead of time so you don't have to try it twice?

    Of course, I mostly work with real-time systems so the impact of trying something twice is significant.

    That depends on how much effort it is to figure out that it will break. I suppose in the divide by zero case, you could always just test for the divisor equal to zero before attempting the division. But there are plenty of cases where in order to determine that a process is going to fail, you have to do the bulk of the work anyway. Like, take the simple problem of converting a string to a number. Sure, I could say that before I start I'll scan the string to be sure it consists of only digits plus at most one decimal point and maybe a leading minus sign. But then it if it passes these tests, I've got to scan it again and process these characters. It's easier to just start processing, and if I hit something illegal, quit at that point and return an error. Otherwise I'm doing a whole bunch of work twice.

  • Jay (unregistered) in reply to too_many_usernames
    too_many_usernames:
    The RWTF regarding divide by zero is that people forget (or, more likely, were never taught) that division by zero does not indicate an arithmetic error. If you get into a situation where you are dividing by zero it means you have a model error, because division by zero indicates "your equation no longer provides useful information."

    Checking the division operation is too late; if you have a divisor of zero that means you need to pick a different equation, not throw an exception.

    Hmm, seems to me that in the real world, the problem is more often not that you have an invalid equation, but that you have invalid data. Like, if we say that we are going to calculate velocity as distance divided by time, and then the user enters that the object travelled 50 meters in 0 seconds, the problem is not that our velocity equation is wrong, but that we were given bad data. The solution isn't to find another equation, but to give the user an error message indicating that there was a data entry error. One obvious way to do this is to trap the "divide by zero" exception and produce an error message when it happens.

  • King Reggin (unregistered)

    This code is stolen from me illegally, FBI has been contacted as the code was on a secure computer/network that was breached a few years back.

  • OMFG (unregistered)

    I'm so impressed by this that I had to come here to comment how nearly speechless I was left after seeing this brillancy.

    I guess this is the WTF of the 21st century (or is it -21st?)

  • webbiedave (unregistered)

    This is all that PHP God's code is actually doing:

    function safely_divideByZero($numerator,$denominator)
    {
    	if ($denominator == "php") {
    		$denominator = rand(0,1) ? 1 : -1;	                               
    	}
    	return $numerator/$denominator;     
    }
    

    PHP God sure likes to babble.

  • Ben (unregistered)

    The limit of x/y as y -> 0 is infinity.

    Therefore, the function is, on average, wrong.

  • AdT (unregistered) in reply to boog
    boog:
    clearly you changed the value of x when we weren't looking, sometime prior to dividing both sides by (x-1), then changed it back after.

    Clearly, you have no clue what you are talking about. You recognized it's a parlor trick but even then are unable to determine how it works.

    What he actually did was transform the true equation he started out with to the equally true equation

    0 = 0

    disguised as

    (x + 1) (x - 1) = x - 1 with x = 1

    Up to this point this is mathematically valid and irreproachable. However, the next step was to divide both sides by x - 1. Since x = 1, this is a division by zero, which is invalid. Therefore, the result of this "transformation" is invalid, too.

    Actually, it is never valid to divide the sides of an equation by zero, even if the divisor is not known to be zero. This is because the validity of the transformation is derived from the canonical

    xy = xz

    which (for real, complex numbers and in this case even quaternions) is true iff y = z, but this is only guaranteed if x != 0.

    So even if x hadn't been defined as 1 or any other particular value, to simplify

    (x + 1) (x - 1) = x - 1

    by dividing both sides by x - 1, we are required to make a special case for x - 1 = 0, therefore we get two equations

    x + 1 = 1 (for x != 1) 0 = 0 (for x = 1)

    where the latter is obtained by simple substitution. This shows us that the equation is true for either x=0 or x=1, but mixing and matching the equations from both cases in any way is invalid.

  • Derp (unregistered)

    Error in the flowchart at $n == 0. It should retry the random if true and continue if false.

    Clearly shows this flowchart is not made by a PHP god!

  • Arvind (unregistered) in reply to New Dog, Old Trick
    New Dog:
    Let x = 1 => x^2 = 1 (square both sides) => x^2 -1 = x -1 (subtract 1 from both sides) => (x-1)(x+1) = x-1 (simplify LHS - dffierence of perfect squares) => x+1 = 1 (divide both sides by (x-1)) => x=0 (subtract 1 both sides) BUT...x=1 => 1=0

    QED.

    WTF?! Needlessly twisted logic. Just do this:

    1. Let x = 1
    2. Let x = 0
    3. Hence, 1 = 0 ... from 1 and 2.

    QED

  • hugo_int (unregistered)

    OMphpG! this is a silver bullet! I will never have to worry about dividing by zero!

    I know i'm no php God but... can't i just do it like this?

    function safely_divideByZero($numerator,$denominator)
    {
       return $numerator / ( $denominator != 0? $denominator : (rand(0,1)==0? -1 : 1) );
    }
    
  • (cs)

    IMHO, the divide operator can be seen as a valid funcion from (R, R) to {R, NaN}. You just have to handle the case when it returns NaN, and keep in mind that the divide operator doesn't implement the actual mathematical division.

    If you wanted a strict application of the mathematical division, you would need a programming language that defines argument domains as actual types. R would be a type, R* would be another type, and the real division operator would be a (R, R*) -> R function. If it was the case, dividing would only compile if the denominator value was of type R*. At best, the precompiler could write an implicit cast of the value (belonging to R or a subset of it) to R*. At runtime, that would raise an InvalidCast exception when that value equals 0.

  • (cs) in reply to AdT
    AdT:
    boog:
    clearly you changed the value of x when we weren't looking, sometime prior to dividing both sides by (x-1), then changed it back after.
    Clearly, you have no clue what you are talking about. You recognized it's a parlor trick but even then are unable to determine how it works.
    Wrong, I do know what I'm talking about. While the rest of you were watching the trick, I kept my eye on his left hand, which he nonchalantly placed in his inside coat pocket about the time that he subtracted 1 from both sides. From his pocket, he drew a phony x which, with some brilliant sleight of hand, he swapped with the real x that was passed around the audience for inspection prior to the act. Using this "phony x", he was able to divide by "(x-1)" with ease, and nobody even noticed. Then, he swapped the phony x back for the real x (which he kept palmed and out of view during the division), and dropped the phony x behind the podium. While everyone was marveling at the results of the trick, he stepped on the phony x with his right foot, and hid it in a secret compartment in the bottom of his shoe.

    Seriously, how could you not know this? It's the oldest trick in the book!

  • mal (unregistered)

    I heard L'Hopital could divide by zero...

  • Scott Blomquist (sblom) (unregistered) in reply to geordish

    I love the "PHP Warning: Division by zero" that you managed to tease out of his zero-proof algorithm.

  • Sam (unregistered)

    The for loops $n==0 test has the True/False paths the wrong way round in the flow chart. Pay attention people, something nasty could happen if you get this sort of delicate code wrong.

  • luis (unregistered) in reply to frits

    by "we" I'm pretty sure hes using the "royal we" used by kings e.g. "we" meaning "God and I..."

    see http://en.wikipedia.org/wiki/Majestic_plural#The_royal_.22we.22

  • Anon (unregistered)

    What's the fuss? I just return 0 when I get a divide by zero. It's the obvious answer.

    Captcha: persto (chango!)

  • Paul (unregistered)

    The logic is undeniable, if the logic is undeniable.

  • (cs) in reply to frits

    It's known as the "Royal We". Used by royalty and other deities since the beginning of time. But, of course, we already knew that.

  • Nick (unregistered) in reply to geordish
    geordish:
    For reference, here is what happens when it is executed...
    safely_divideByZero(25,0);
    25
    safely_divideByZero(25,0);
    -25
    safely_divideByZero(25,0);
    -25
    safely_divideByZero(25,0);
    -25
    safely_divideByZero(25,0);
    -25
    safely_divideByZero(25,2);
    12.5
    safely_divideByZero(25,-2);
    -12.5 
    safely_divideByZero(25,"arse");
    PHP Warning:  Division by zero in /home/dave/argh.php on line 44
    
    safely_divideByZero(25,"php");
    -25
    

    Haha - I thought this was just regular php code. You dodgy hackers.

    Just kidding folks. Love the quote above

  • cirne (unregistered) in reply to geordish

    Anyone who uses PHP knows that data often comes in as a string; when you're reading variables from an HTTP query string, for example, they're always strings, even if it's just the string representation of a number. Depending on your database backend, even INTEGER columns from an SQL database will come back as strings.

    So there's one important case that our "PHP God" left out:

    safely_divideByZero(25,"0");

    PHP Warning: Division by zero in /tmp/sdbz.php on line 44

  • Lisa (unregistered) in reply to frits

    Oh, that's just the "royal we"!

    :-)

  • hiphopopotamous (unregistered) in reply to Anon
    Anon:
    I don't "do" PHP, but I'd propose a solution like this:
    public double safely_divideByZero(double numerator,double denominator)
    {
        if (denominator == 0)
            return numerator / double.MinValue;    // close enough
        return numerator / denominator
    }
    

    You forgot to optimise it:

    public double safely_divideByZero(double numerator,double denominator)
    {
        if (denominator==0.0)
            return double.maxValue;    // close enough
    
        return numerator / denominator;
    }
    
  • Chad (unregistered)

    The expected result of a divide by zero is infinity. A better solution, if you couldn't deal with an error, would be to just return the largest possible value. Returning an average of 0 makes no sense whatsoever.

  • (cs) in reply to haero
    haero:
    port22:
    I divided by zero and all I got was this lousy t-shirt.
    FTFY
    What if you divide Irish Girl by zero?
  • ALEX (unregistered) in reply to geordish

    safely_divideByZero(25,"arse");

    ^ I have never laughed so hard, as I did when I read the above line! Kudos mate, I am still hysterical!

  • marshviperX (unregistered) in reply to frits

    You slack-jawed peasant! It's only natural that gods and royalty refer to themselves as "we" by their divine providence!

  • Mullety (unregistered)

    The fact that management likes him just goes to show that management likes to get results. It does not really matter whether the results are correct, as long as there is an answer.

  • The Poop... of DOOM (unregistered) in reply to Some damn Yank
    Some damn Yank:
    Chris:
    The flowchart does not match the code.
    I've never seen the code match the flowchart. What's your point?
    Oh! Oh! I have! At one place I worked, I was the sole developer. There were no specs, just barkings from the boss. In the end, after development, I had to write the specs so the boss could give them to the customer, as a "manual". That way, you ALWAYS have flowcharts matching the code. As soon as you add one extra developer, the ALWAYS disappears, though (or if you're taking the piss out of the boss, and the in this case useless "project manager")

    Captcha: Populus -- Pretending to be God

  • Mike M (unregistered)

    This reminds me... unfortunately... of a function I wrote a few years ago that I still receive flak for on a daily basis. I think mine was slightly more elegant (albeit just as pointless). It always takes a good dose of humble pie to make yourself a better developer.

    function safeDivide($num, $den) { if($den == 0) return 0; else return $num / $den; }

  • Jack (unregistered)

    The quotient shouldn't average out to zero. For any constant numerator, the quotient approaches infinity (or negative infinity) as the denominator approaches zero.

  • Vlad (unregistered)

    Wow! Now this WTF posted 2011-02-18: [image] makes perfect sense!

  • Bob (unregistered)

    This seems fitting: http://www.youtube.com/watch?v=2s1MspmfEwg

  • Theraot (unregistered) in reply to frits

    " What's all this "we" shit? I can guarantee that no one else was involved in this work of art. "

    I's a defect inherited from the bible, you see, God insists in that he is more than one. Some say that two, a few that four, You'll notice that these values average out to three, which is exactly what you where expecting.

  • James (unregistered)

    What the sweet fuck is this shit?

  • GreYFoX (unregistered)

    So what this code does is determine how many orphans are abused in the arctic region ?

  • Deffy (unregistered) in reply to frits

    It's the royal we. Remember, this is a PHP god we're talking about.

  • Deffy (unregistered)

    The flowchart is slightly off. From $n==0, the true is the one that loops and false is the one that continues, not the other way around as it is here.

  • bob (unregistered) in reply to geordish
    geordish:
    For reference, here is what happens when it is executed...
    safely_divideByZero(25,0);
    25
    safely_divideByZero(25,0);
    -25
    safely_divideByZero(25,0);
    -25
    safely_divideByZero(25,0);
    -25
    safely_divideByZero(25,0);
    -25
    safely_divideByZero(25,2);
    12.5
    safely_divideByZero(25,-2);
    -12.5 
    safely_divideByZero(25,"arse");
    PHP Warning:  Division by zero in /home/dave/argh.php on line 44
    
    safely_divideByZero(25,"php");
    -25
    
    I can't see how. The flowchart is wrong. The code always executes $numerator/$denominator.
  • Joe (unregistered)

    What I want to know is, what code actually called this masterwork, and what did it expect to gain by doing so?

  • Joe (unregistered) in reply to King Reggin

    oh please oh please oh please let the original author of this code issue a DMCA takedown so we can all find out who he is. to hire him, of course.

  • eqhmcow (unregistered)

    Reimplemented in perl

    https://gist.github.com/1556389

  • Mathias (unregistered) in reply to geordish
    geordish:
    For reference, here is what happens when it is executed...
    safely_divideByZero(25,0);
    25
    safely_divideByZero(25,0);
    -25
    safely_divideByZero(25,0);
    -25
    safely_divideByZero(25,0);
    -25
    safely_divideByZero(25,0);
    -25
    safely_divideByZero(25,2);
    12.5
    safely_divideByZero(25,-2);
    -12.5 
    safely_divideByZero(25,"arse");
    PHP Warning:  Division by zero in /home/dave/argh.php on line 44
    
    safely_divideByZero(25,"php");
    -25
    

    You made me laugh as much as the article did, thank you :D

  • Dan (unregistered) in reply to geordish

    Correct me if I'm wrong, but if you were to write this in the first place, the function could be 1 line of code:

    <?php function safely_divideByZero($numerator, $denominator) { // if the denominator is falsy, then randomly use 1 or -1 // otherwise use the denominator return $numerator / (!$denominator ? (rand(0,1) == 1 ? 1 : -1) : $denominator); }

Leave a comment on “Divine by Zero”

Log In or post as a guest

Replying to comment #:

« Return to Article