• (cs) in reply to eVil
    eVil:
    Sunday Ironfoot:
    Unless I'm fundamentally misunderstanding PHP, surely the only code that will EVER run is the line:

    $result = $numerator/$denominator;

    ...since if $denominator == "php" then it'll fail the second check and the line above just runs (in PHP $denominator can't both be equal to TRUE AND "php" can it?). Whereas if $denominator != "php" then it'll just run the above line of code again.

    Surely this PHP God has an EPIC misunderstanding of how variables work.

    I think (i'm no expert) that the $denominator variable, containing the string "php", will be implicitly cast to a boolean, and a string cast to a boolean results in 1.

    Basically, the guy is relying on some rather shifty non-typesafe casting behaviour in php, that should probably be avoided at all costs.

    Ah OK, I stand corrected, too much time spent with static, strongly typed languages

  • (cs)

    Ok fine, maybe he's not god, but PHP Jesus he most definitely is!

    See? Instead of water to wine, it's int to string, string to boolean, and boolean to int.

    After all, we all know that god is the PM and is too busy to get his hands dirty with code, right?

  • Greg (unregistered) in reply to Ryan

    In PHP, any string that can't be implicitly converted to a number is equal to zero when type-shifted.

    So, 0 != "php" does not evaluate to true in PHP.

    And strangely enough, 0 == "php" is true as well as "php" == TRUE. And well, TRUE is either 1 or -1 as a value. So yeah, this code actually executes down the path if the user puts in 0 as a denominator.

  • Mike P (unregistered)

    I only have one testicle :(

  • (cs) in reply to Ryan
    Ryan:
    AA:
    0 == "php" "php" == TRUE TRUE == 1
    Incorrect on the first one... I think the writer of the code was expecting someone to pass in the string "php" as the denominator value.
    This comment seems to contradict your assumption:
    /* check to see if the denominator == "php" - remember, 0 == "php" */
    That said, if the author wanted to compare to zero, he should have just-fucking-compared to zero.
  • Brian (unregistered) in reply to frits

    So, I know a guy that in his comments uses the word "I". "OK, so here I'm adding the suffix to the name so that... "

    Every other developer I've known or worked on code from uses "we". Is that true else where?

  • Ton (unregistered) in reply to frits

    Well duh:

    http://en.wikipedia.org/wiki/Majestic_plural

  • Brian (unregistered) in reply to Ton
    Ton:
    Well duh:

    http://en.wikipedia.org/wiki/Majestic_plural

    Thank you!

  • (cs) in reply to Mike P
    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.)
  • Banaan (unregistered)
    Mike adds, "sometimes this will return 25, sometimes -25.

    How's that? I'm seeing mutual exclusivenesses...es. everywhere.

    The only thing I see happing is that a division by zero exception will be thrown. Or maybe I'm reading the code wrong.

  • CBM (unregistered) in reply to tragomaskhalos

    Never ascribe to cunning what can adequately be explained by stupidity.

  • Dima (unregistered)

    I'm not really into php and don't really know the syntax or the side effects so forgive me if this is stupid but

    ($denominator=="php"), ($denominator==TRUE) and ($denominator==1||$denominator==-1)

    are they not mutually exclusive? Or is this a side effect where the second boolean comparison succeeds when the string is not empty and then the third comparison succeeds because php's boolean TRUE is defined as integer 1 or -1?

    I ask because a call to

    safely_divideByZero(25,"php")

    seems to always result in error due to division by string "php" instead of 25 or -25.

    So if I understood this correctly there are 2 wtfs here: the weird function and Mike P's comment at the end.

  • Toddsa (unregistered)

    Hahaha I feel sorry for the person whom took over that code base.

  • (cs)

    Someone please correct me if I'm wrong, but I seem to remember that in PHP, if you use a string as a number (in a comparison, for example), then it automatically tries to convert it. There seems to be a lot of "strings are zero in PHP" discussion here, but isn't it just because these strings don't start with numbers (only one person seems to have noted this so far)?

    If you had 0 == "2blah", it would convert "2blah" to 2, and return false, right? Similarly, if you did 0 == "blah", it would convert "blah" to 0, and return true.

    If that's the case, this guy coded this method assuming that "php" would always be compared to a number, and would therefore convert to 0. Consider what the following code would do:

    safely_divideByZero(3.14159, "shitsnacks")
  • Some damn Yank (unregistered) in reply to Chris
    Chris:
    The flowchart does not match the code.
    I've never seen the code match the flowchart. What's your point?
  • Toddsa (unregistered)

    This is why code reviews are so important. Do not accept a develepment job unless they do code reviews or you plan to implement them.

  • (cs) in reply to C-Octothorpe
    C-Octothorpe:
    Imagine a place so twisted that you can "cleverly safely" divide an apple amongst zero people...
    FTFY
  • (cs)

    This function was just a placeholder until he could write one that would contact Chuck Norris.

  • Meep (unregistered) in reply to Pr0gramm3r
    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.

    BTW, here's what happens when you divide by 0. And here's what Mathematica thinks 1/0 is.

  • Somebody (unregistered) in reply to Falcon

    I am just going to go with WOW on this one. That should just about cover it.

  • Meep (unregistered) in reply to Sunday Ironfoot
    Sunday Ironfoot:
    eVil:
    Sunday Ironfoot:
    Unless I'm fundamentally misunderstanding PHP, surely the only code that will EVER run is the line:

    $result = $numerator/$denominator;

    ...since if $denominator == "php" then it'll fail the second check and the line above just runs (in PHP $denominator can't both be equal to TRUE AND "php" can it?). Whereas if $denominator != "php" then it'll just run the above line of code again.

    Surely this PHP God has an EPIC misunderstanding of how variables work.

    I think (i'm no expert) that the $denominator variable, containing the string "php", will be implicitly cast to a boolean, and a string cast to a boolean results in 1.

    Basically, the guy is relying on some rather shifty non-typesafe casting behaviour in php, that should probably be avoided at all costs.

    Ah OK, I stand corrected, too much time spent with real languages

    FTFY.

  • Justin (unregistered) in reply to Larry
    Larry:
    Anon:
    What's all this "we" shit?
    It refers to the cooperation between two minds: mine and the computer's.

    Congratulations... You just created Skynet.

  • QJo (unregistered)

    Perfectly okay to divide by zero, as long as you're using integer division which returns quotient and remainder. In which case x / 0 = 0 remainder x.

  • vbtwo (unregistered) in reply to tom103

    This actually makes some sense. If you try to compare an int and a string in PHP, it tries to convert to string to an int (with same logic as intval function). Any string that doesn't start with numbers will be converted to 0. So "php" will become 0, but "42php" will become 42. What would make more sense is to just convert the int to a string and then do string comparison.

    When comparing a string to a boolean, any non-empty string evaluates to true. When comparing ints to booleans, anything except 0 evaluates to true.

    Thus "php" == 0 is true And "php" == true is true But 0 == true is false

    In any case, to avoid all these troubles, unless you really do want the conversions taking place, it's best to use strict comparison operator (===).

    Also, for anyone wondering why the flowchart doesn't have the false options in it, it's because once it passes the first test ($denominator == "php"), it will automatically pass all the other tests. There is no way to fail them. "php" == true. true == 1.

    The flowchart does have an error with the for loop (true and false should be reversed there).

  • Ted C. Howard (unregistered) in reply to frits

    This obviously confirms the existence of the Holy PHP Trinity.

  • Justin (unregistered) in reply to Pr0gramm3r
    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.

  • vbtwo (unregistered) in reply to vbtwo
    vbtwo:
    This actually makes some sense.
    Oops, I fail at quoting. Wanted to quote someone saying PHP makes no sense. I did not mean that the code the dude wrote makes any sense.
  • (cs) in reply to Justin
    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?

  • (cs) in reply to Jonathan
    Jonathan:
    What happens when something is divided by negative zero?

    Apparently you're invoiced for it...

  • Justin (unregistered) in reply to Jonathan
    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?

    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.

  • Anonymous (unregistered)

    I'm no PhP coder, but isn't that equal to

    <?
    function safely_divideByZero($numerator,$denominator)
    {
        $result = $numerator/$denominator;  
        return $result;      
    }
    ?>
    

    ?

    I assume if $denominator == "php" it will not == TRUE.

  • ElGoorf (unregistered)

    the flow chart contradicts the code and common sense.

    if $n==0, how can $n==1 at the next step?

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

    Which one?

  • (cs)

    I've read every single article posted on this site for several years now, and I can safely say this is the first time I have ever uttered "What the FUCK" out loud...

  • gs (unregistered) in reply to Justin
    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... ;-)

  • Rage (unregistered)

    I would seriously consider discretely sneaking over to his desk and dumping a pot of coffee on his keyboard to save future victims.

  • Anonymous (unregistered) in reply to Migala
    Migala:
    AA:
    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.

    No, he's just proven that equality is not transitive in PHP. Which is, of course, the real WTF.

  • (cs) in reply to Anonymous

    TRUE == 1 => TRUE TRUE === 1 => FALSE

    Get it by now? As in every language, you have to know how to use it.

  • (cs) in reply to Rage
    Rage:
    ... dumping a pot of coffee on his keyboard...

    Make sure he's using it when you do that...

  • Svenson (unregistered) in reply to Justin
    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.

    Come on - you know 0 is positive. Just look at the sign bit.

  • (cs) in reply to Svenson
    Svenson:
    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.

    Come on - you know 0 is positive. Just look at the sign bit.

    Any imaginary number squared is negative including imaginary zero squared.

  • Zelf (unregistered) in reply to Svenson

    Perhaps he was just trying to invent a new field of Mathematics.

    Since Calculus is just a fancy way of dividing by 0, and often gives a result other that infinity.

  • Matt (unregistered)

    I really love how he never accounts for any other case until right at the end. It's like this one thing and nothing else can happen. I don't know PHP well enough for that, but it seems foolish, but this whole code is...

    Won't this return numbers that will potentially fuck up the Maths somewhere else and really cause a problem? Wouldn't it be more sane to always return 0 when a 0 is involved in a division?

  • (cs) in reply to Justin
    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.

    Not according to Brahmagupta.

  • Ramiro (unregistered) in reply to frits

    It's God. He's one and three at the same time.

  • Joe (unregistered)

    All much too complicated.

    function safely_divideByZero($numerator, $denominator) {
       return $numerator / 8; /* Divide numerator by de number eighter */
    }
    

    Perfectly safe. --Joe

  • The Ancient Programmer (unregistered) in reply to Justin
    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...

  • php god (unregistered)

    The code shown was correct, it was missing, however the most important statement as quoted:

    require_once('transfinite_math.php');

    A package I myself wrote (very cleverly) which would make it clear to anybody with an ounce of god-ness that not only this, but every other line of code I wrote was correct.

  • Jimmy (unregistered)

    I feel dumber having read through that.

  • nick (unregistered)

    TRWTF is he's not using mt_rand().

Leave a comment on “Divine by Zero”

Log In or post as a guest

Replying to comment #349122:

« Return to Article