• Imperion (unregistered) in reply to frits
    frits:

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

    He's the PHP God... it makes sense that he would use the royal "we".
  • anonymous (unregistered)

    Isn't the answer "File Not Found"?

  • monkeyArms (unregistered)
    function safely_divideByZero($numerator, $denominator)
    {
    	return $numerator/(($denominator == 0) ? (rand(0,1) == 0 ? -1 : 1) : $denominator); // <-- this part is clever; we don't want to waste lines of code
    }
  • (cs) in reply to Pr0gramm3r
    Pr0gramm3r:
    TRWTF is nobody here knows the correct solution to the problem,
     $x/0 = INF 
    That's because it's false. The limit for division by 0 isn't even defined. Try these

    lim(x->0) sin(x) / x = 1

    lim(x v 0) e(x) / x -> INF

    lim(x ^ 0) e(x) / x -> -INF

    Division by zero is just totally undefined.

  • Lolcat (unregistered) in reply to Joe
    Joe:
    All much too complicated.
    function kitteh_divideByZero($numerator, $denominator) {
       return void; /* De NOMin8r iz in ur functshun, eated ur noomer8r */
    }
    

    Perfectly safe. --Joe

    FTFY.

  • Roman (unregistered) in reply to AA
    AA:
    eVil:
    Basically, the guy is relying on some rather shifty non-typesafe casting behaviour in php, that should probably be avoided at all costs.

    PHP should basically be avoided at all costs.

    Lies! LIES!!!

  • Matt (unregistered) in reply to frits

    Clearly the PHP god is a Trinity. It's obvious he could also make 3===1.

    nimis: Sure glad we didn't hire PHP god. That was a ni`mis.

  • geordish (unregistered)

    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
    
  • Anon (unregistered)

    Holy fucking Jesus on a bicycle. I feel like this code has just raped my mind.

    This is probably the worse code I've ever seen here because rather than just being plain stupid or wrong, it's deviously stupid and wrong on so many levels.

  • Anon (unregistered)

    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
    }
    
  • nick (unregistered) in reply to too_many_usernames

    or else Renormalise :)

  • Cap'n Spanky (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
    }
    
    Isn't double.MinValue going to be something approaching negative infinity...which would result in
    numerator / -oo
    ...which would result in a quotient approaching zero from the left?
  • Wonk (unregistered)

    I propose a solution of Napalm with a bit of nerve gas included. That way, he will lose control of most orifices while he writhes on fire. but that's just me

  • Anon (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
    }
    

    I think you'd just have to go:

    function safely_divideByZero($numerator, $denominator)
    {
        if ($denominator == 0)
            return $numerator / 4.94065645841246544e-324;
        return $numerator / $denominator;
    }
    

    Value taken from Java documentation for 64-bit fp, couldn't find for php.

  • Robert (unregistered) in reply to frits

    It's all the people in his head getting together to make this monstrosity.

  • (cs) 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.

    The one time I remember this coming up was in (wait for it) Fortran. Doing some heavy math computations, I found myself needing the arctangent of some ratio, the denominator of which could be and often was zero. Straightforward calculation of the ratio followed by a call to the ATAN function blew up on the divide, which is where I learned that Fortran comes with another function: ATAN2. Two arguments, such that ATAN2(x,y) is the same as ATAN(x/y), except that when y is zero it returns π/2 (or -π/2 if x is negative).

  • wawb (unregistered) in reply to monkeyArms

    return $numerator / ( $denominator == "php" ? 1-(2*rand(0,1)) : $denominator );

  • gr (unregistered) in reply to frits
    frits:
    What's all this "we" shit? I can guarantee that no one else was involved in this work of art.

    That's the Omnipotent We. It's similar to the Royal We.

  • David F. Skoll (unregistered)
    function divide($num, $denom) {
        if ($denom) return $num / $denom; # Boring case
        if ($num < 0) return "-Inf";
        if ($num > 0) return "Inf";
        return 42;
    }
    
  • Anon (unregistered) in reply to gr
    gr:
    frits:
    What's all this "we" shit? I can guarantee that no one else was involved in this work of art.

    That's the Incompotent We. It's similar to the Royal We.

  • ryan (unregistered)

    Lol at "somewhat obvious here"

  • (cs) in reply to Justin
    Justin:
    Jonathan:
    Justin:
    Pr0gramm3r:
    TRWTF is nobody here knows the correct solution to the problem,
     $x/0 = INF 

    By INF do you mean infinity? If so, then that is incorrect. Infinity is defined. We do not know what happens when something is divided by 0 therefore it is undefined.

    What happens when something is divided by negative zero?

    Ha ha, really?

    How is being negative defined? Less than 0, right?

    There are two equivalent ways to do it. Less than 0, or less than or equal to 0. In the first case, it is neither. In the latter, it is both.

    No real problem results from this, except that mathematicians tend to prefer defining things in terms of less-that-or-equal (because doing so implies certain things about join-like operations on ordered sets).

    Sometimes people prove that a number must be zero by showing that the number is both positive and negative. If the "less than" definition is in place, this is a proof by contradiction (in other words, non-constructive. A computer can't do it). If the "less than or equal to" definition is in place, it is a direct proof (which a computer can do).

  • (cs) in reply to Toddsa
    Toddsa:
    Hahaha I feel sorry for the person whom took over that code base.
    I feel sorry for people who try to use whom but can't.
  • (cs) in reply to Sutherlands
    Sutherlands:
    Toddsa:
    Hahaha I feel sorry for the person whom took over that code base.
    I feel sorry for people who try to use whom but can't.

    Way to nit-pick on the ESL student...

  • Anon (unregistered) in reply to Cap'n Spanky
    Cap'n Spanky:
    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
    }
    
    Isn't double.MinValue going to be something approaching negative infinity...which would result in
    numerator / -oo
    ...which would result in a quotient approaching zero from the left?

    Shit. I meant double.Epsilon (or equivalent in other languages). Smallest positive double value greater than zero.

    Man I even fucked up by deliberate WTF, which is TRWTF.

  • Pietje (unregistered) in reply to frits

    I'm pretty sure this went into production because the guy spent so much time on it.

  • (cs) in reply to C-Octothorpe
    C-Octothorpe:
    Sutherlands:
    Toddsa:
    Hahaha I feel sorry for the person whom took over that code base.
    I feel sorry for people who try to use whom but can't.

    Way to nit-pick on the ESL student...

    Didn't realize it was an ESL.

    Regardless, if you don't know when to use whom, just use who... that way you won't sound pretentious.

  • Spoe (unregistered) in reply to Pr0gramm3r
    Pr0gramm3r:
    TRWTF is nobody here knows the correct solution to the problem,
     $x/0 = INF 

    $x / 0 == undefined, not infinity. You can't even say the limit of $x/$n is infinity as $n -> 0 because it's negative infinity coming from the negative side and positive infinity coming from the positive.

  • Spoe (unregistered) in reply to Spoe
    Spoe:
    Pr0gramm3r:
    TRWTF is nobody here knows the correct solution to the problem,
     $x/0 = INF 

    $x / 0 == undefined, not infinity. You can't even say the limit of $x/$n is infinity as $n -> 0 because it's negative infinity coming from the negative side and positive infinity coming from the positive.

    Where $x if positive, of course. If $x is negative, it's the other way.

  • Gabriel (unregistered) in reply to frits

    [quote user="frits"][quote user="PHP God"]What's all this "we" shit? I can guarantee that no one else was involved in this work of art.[/quote] Work of art? How dare you compare this filth with the likes of cave paintings You do damage to true paleolithic art everywhere.

  • coyo (unregistered) in reply to frits
    frits:
    PHP God:
    /* if the denominator == "php" we  can safely set the value of the denominator to "php"  */
    

    /* if the denominator == TRUE we can set the value of the denominator to TRUE */

    /* this part is clever, we don't know which is the right answer, so we can use a for loop to randomize */

    /* we don't want zero as the answer (back to square one if it is!) so we randomize from 0 to 2, but the for loop throws out results that == 0 and makes it try again */ $n = rand(0,2);

    /* if the answer == 1, then we set the denominator to 1, somewhat obvious here */

    /* we return as the result a simple division, except now we can divide by zero without error */

    .

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

    I'm surprised the we isn't capitolized

  • (cs)

    I can't quite tell if people are trolling regarding what x/0 evaluates to or if they're just stupid.

    Seriously people, it's not hard. It's undefined.

  • Math Math Math (unregistered) in reply to The Ancient Programmer
    The Ancient Programmer:
    Justin:
    Pr0gramm3r:
    TRWTF is nobody here knows the correct solution to the problem,
     $x/0 = INF 

    By INF do you mean infinity? If so, then that is incorrect. Infinity is defined. We do not know what happens when something is divided by 0 therefore it is undefined.

    Dividing by an increasinly smaller number gives an increasingly bigger answer until dividing by zero gives an answer of infinity.

    Dividing zero by zero gives an undefined answer...

    You've described taking the limit from above:

    lim[a->0+] x/a = INF

    This is nice if you're looking at it from a Calculus point of view. You can also come up with an answer for 0/0 in some cases using L'Hopital's Rule.

    However, from an abstract algebra point of view, division is not an operator over the real numbers precisely because division by zero is undefined. Calculus tricks notwithstanding, poor old division still doesn't get to join the operator club.

  • haero (unregistered) in reply to port22
    port22:
    I divided by zero and all I got was this lousy t-shirt.
    FTFY
  • (cs) in reply to hatterson
    hatterson:
    I can't quite tell if people are trolling regarding what x/0 evaluates to or if they're just stupid.
    You seem to imply that these conditions are mutually-exclusive. Why is that?
  • FuBar (unregistered) in reply to Sutherlands
    Sutherlands:
    C-Octothorpe:
    Sutherlands:
    Toddsa:
    Hahaha I feel sorry for the person whom took over that code base.
    I feel sorry for people who try to use whom but can't.
    Way to nit-pick on the ESL student...
    Didn't realize it was an ESL.

    Regardless, if you don't know when to use whom, just use who... that way you won't sound pretentious.

    Actually it's probably not an ESL. In my experience, other languages' speakers don't get as confused by declension as English's do, and they carry that awareness into their use of English. You don't catch native German speakers confusing wer, wessen, wem, wen (unless they're 2 years old). So I side with Sutherlands's original point.

  • Harrow (unregistered) in reply to Wonk
    Wonk:
    ...he will lose control of most orifices while he writhes on fire...
    But then won't he just put himself out?

    -Harrow.

  • Warren Young (unregistered)

    My head asplode.

  • John (unregistered)

    I'm sure someone's already said it.

    The flowchart does not represent the code properly. Other than there being only 1 false condition in the flow, the loops true and false conditions are missed (the loop exits when $n != 0, not $n==0 (swapping the true/false branches will fix).

    This has to be fake - it doesn't even do remotely what it's meant to....

    It will work provided $denominator is not 0, but that it is a number.

  • Uri (unregistered) in reply to The_Abominator
    The_Abominator:
    PHP god? This guy needs to be sent to Hades.

    Anyone who adds comments indicating how clever their code is, should usually be avoided, shot or buried naked and waist down in a red ant pit.

    At the end of that cluster of dog piss that he calls a function, the only possible comment is "Whats the F'ing Point"?

    If I saw this code, I'd crucify the coder, without burial so he won't rise again.

    But then again, it is PHP, don't they all think they are gods...'wink'

    Yup. "This is a clever trick' or 'We cleverly....' always starts alarm bells flashing and lights dinging that someone has decided to reinvent the wheel because their blissfully unaware that there is a simpler way to do things (or that there exist libraries to do such things already).

  • Noni (unregistered) in reply to MC
    MC:
    Definitely a joke. This guy was just messing with anybody that actually looked at his code.

    I would agree with you, except why would you bother commenting it in that case...

    This does look fake, and it is possible that some dick did it as a laugh, however the way it is commented suggests to me that the person who did it really was a nonce.
    Must admit, tho, that the stupidity of the code does beg the question (oops) how the dick would have gotten anywhere...Maybe he didn't have a degree, or maybe his mates in the union insisted he be hired...

  • New Dog, Old Trick (unregistered) in reply to Migala
    Migala:
    AA:
    Sunday Ironfoot:
    (in PHP $denominator can't both be equal to TRUE AND "php" can it?)

    Clearly you don't know PHP.

    0 == "php" "php" == TRUE TRUE == 1

    So basically this guy has just proven that 0 == 1. That, to me, is one brillant miracle.

    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.

  • samy (unregistered)

    I can't stand that code... And I can't understand why php allows people to create something like that...

  • Zhip (unregistered) in reply to synp
    synp:
    It's the royal plural.

    Besides, it's more enterprisey. You're not just writing this for yourself. There's this huge corporation with its thousands of employees standing behind you on this.

    If you're working for the government, 'we' is the entire $nationality people. How's that for awesome.

    captcha: uxor - unsigned XOR. That's some advanced bit logic

    Plus, comments always have to say "we" no matter how good I am.
    When the shyte hits the fan, it's a lot easier to say "WE need to fix this" when the comments all say "we". If they say "I" it means it's my problem...

  • old trick (unregistered) in reply to New Dog, Old Trick

    turns out that dividing by x-1 is only allowed if x!="1php"

  • Donny (unregistered) in reply to Steve The Cynic
    Steve The Cynic:
    Mike P:
    I only have one testicle :(
    If that's the case, then every English schoolboy knows that your name isn't MikeP, provided that the other one is in the Albert Hall. (No more hints provided, sorry.)

    Oh...I always thought it was Kitchen Wall....

  • Same dog, different leg action (unregistered) in reply to old trick
    old trick:
    turns out that dividing by x-1 is only allowed if x!="1php"
    "1php" = 1 x php = 1 x 0 = 0 ????

    Okay then assume i = sqrt(-1) => i = srqt(-1/1) = sqrt (1/-1) = sqrt(1)/sqrt(-1) => i = 1/i => -1 = 1

  • Mathemagician (unregistered) in reply to Meep
    Meep:
    Pr0gramm3r:
    TRWTF is nobody here knows the correct solution to the problem,
     $x/0 = INF 

    Sure we do, there is no value for $x that would satisfy the postcondition.

    Although I agree with "limit as x->0 = sideways 8", I also thought most people accept that we actually get to infinity when the denominator is 0 (although noone really knows where that is - although we can prove that there are inifities of different sizes {the sum of primes is clearly smaller than the sum of Integers, which is clearly smaller than the sum of all rational numbers etc). I thought what they meant there by "no solution" is in fact infinitely many solutions (although I now notice they say "no solutions" - which is more interesting).

    Also, I think "The Phantom Tollbooth" is a good book...

  • (cs) 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.

    Um, no, we are running in the correct universe. Your "proof" is nothing but a cheap parlor trick; 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.

    Also, the rabbit was already in the hat, and the woman from the audience that "you've never met" was in the top half of the box the whole time; the feet at the bottom were mechanical.

  • Yousef (unregistered) in reply to gs
    gs:
    Justin:
    Ha ha, really?

    How is being negative defined? Less than 0, right?

    So you are saying that it is possible for 0 < 0? or 0 > 0?

    0 == 0 plain and simple. It is neither positive nor negative.

    Ha ha, really? I guess he should have put <irony> tags, so everyone would get it... ;-)

    Not for the first time, I've wondered whether everyone thinks irony == sarcasm == comedy...

Leave a comment on “Divine by Zero”

Log In or post as a guest

Replying to comment #349186:

« Return to Article