• 'mfused (unregistered) in reply to TGV
    TGV:
    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.

    Looks like it is defined, just not consistent - maybe it means +INF == -INF and (in the right circumstances) 1 == INF.

    Apparently, 2+2 = 5 for large values of 2.

    http://en.wikipedia.org/wiki/2_%2B_2_%3D_5#Self-evident_truth

    I actually believed that akismet had gone away, but he doesn't like my link

  • Jonny come lately (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.

    I don;t think he actually knows whether the OP is either, he's just being himself

  • Pedantic, perhaps (unregistered) in reply to Math Math Math
    Math Math Math:
    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.

    But doesn't undefined in this sense mean too many possible solutions rather than 'no solution' as it seems some are suggesting. We cannot decide on an answer, not because one doesn't exist, but because we don't really understand what the answer should be because it could be many things.

  • I may not be a Mathemagician but I have a math degree (unregistered) in reply to Mathemagician

    Actually, given that by definition we show two sets are the same "size" by constructing a bijection between them, everything you just listed is exactly the same "size". Though you did say "sum" so maybe your weren't talking about cardinality, but if you really meant "sum" then I don't know what you were talking about.

  • Silly drongo (unregistered) in reply to haero
    haero:
    port22:
    I divided by zero and all I got was this lousy-t shirt.
    FTFY
  • Boython (unregistered) in reply to boog
    boog:
    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.

    Uhm...judging by his name, I suspect there was never any question that it was just a silly little trick.

    I guess we should be used to you feeling like you should respond to every postm though.

  • (cs) in reply to Boython
    Boython:
    Uhm...judging by his name, I suspect there was never any question that it was just a silly little trick.
    Nope. Never any question. Thanks for your valueless contribution.
    Boython:
    I guess we should be used to you feeling like you should respond to every postm though.
    You really should, my dispensable disapprover, but in the future I'll try to run all comments by you first, ok?
  • anon (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'

    My guess is that some management type created a requirement for the ability to "Safely divide by zero" and this resultant set of B.S. is meant to appease that management individual.

  • teambob (unregistered)

    The randomize loop in the flowchart doesn't seem consistent with the source code and the functionality.

    The loop should go back to the beginning if $n==0 is TRUE, whereas the flowchart shows the loop going back to the beginning if it is FALSE.

    Am I missing something here?

  • Xythar (unregistered)

    MY BRAIN

  • Smoke3723 (unregistered) in reply to frits

    PHP Gods use the royal pronoun.

    Captcha: damnum Exactly what I thought on reading this implementation.

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

    Actually that depends on the function; if you really want to divide by zero you need to take the limit of the function as it approaches zero. Sometimes this results is INF or -INF, a value, or it will fail to converge, in which case it is undefined.

  • Math Math Math (unregistered) in reply to Pedantic, perhaps
    Pedantic:
    Math Math Math:
    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.

    But doesn't undefined in this sense mean too many possible solutions rather than 'no solution' as it seems some are suggesting. We cannot decide on an answer, not because one doesn't exist, but because we don't really understand what the answer should be because it could be many things.

    Yes, you have the correct answer by the correct reasoning. It is undefined (math cannot give you an answer) because "we don't really understand what the answer should be".

    Part of the problem is as you suggest, there are an infinite number of solutions for x in the equation:

    x*0 = 0

    A few levels of abstraction up, we find out it's not that we can't pick a definition, but rather that no consistent definition can possibly exist.

    The closest analogy I can make without getting into the mathematical details:

    Bicycles and cars are both vehicles, but for bicycles "removing the gas cap" is an undefined operation. If you want to remove a gas cap, you either have to locate a car, or modify a bicycle so that it has a gas cap. However, after you rush off to tell your friends that you figured out how to remove the gas cap from a bicycle, they tell you that you've modified the bicycle, so it's no longer really a bicycle, so it doesn't count.

    If the mathematical details interest you, A Book of Abstract Algebra is about $12 brand new and written in a very conversational style.

  • Lyle Austin (unregistered)

    My understanding is that the reason why dividing by zero is "undefined" is because when you divide by zero you get results like 2+2=5 and 2=3.

    It's like those old math puzzles that were popular years ago where they step by step "proved" 2=3. When you examine one of the steps, you'd invariably find one step that would involve dividing by zero.

  • MK (unregistered)

    ...What.

    Division by 0 can mean many different things, depending on the situation. I guess the PHP God's prodigious skillset didn't include a grasp of elementary calculus. :(

    ...One other question. Why does the string 'php' mean zero? Is this some strange convention I've never heard of? Is there some kind of standards body specifying what number 'php' is? Deas 'Python' mean 24? What about 'C Pound'?

  • Gibbon1 (unregistered) in reply to Pedantic, perhaps

    [quote user="Pedantic, perhaps"][quote user="Math Math Math"][quote user="The Ancient Programmer"]But doesn't undefined in this sense mean too many possible solutions rather than 'no solution' as it seems some are suggesting. We cannot decide on an answer, not because one doesn't exist, but because we don't really understand what the answer should be because it could be many things. [/quote]

    You are close, what it means in practice is that pure division isn't sufficient, you have an overflow condition and a loss of precision. What to do about that is problem dependent. For real numbers the square root of a negative number is undefined, yet maybe if it's a tiny negative number what you want is the sqr() to return zero in that case. Rounding errors, noise, and bad user input cause problems.

  • Dr.No (unregistered)

    Well, the code is indeed well commented...

  • relet (unregistered) in reply to frits

    It's called Pluralismajestatis. Gods do that.

  • (cs)

    oh my god what is this i don't even

    ARGH

    Mike adds, "sometimes this will return 25, sometimes -25. You'll notice that these values average out to 0, which is exactly what we were trying to divide by."

    ... wow, this is the work of a genius!

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

    And dividing the same number by a negative number with progressively smaller absolute value gives a progressively smaller negative number, until dividing by 0 yields negative infinity.

    Since you can define 0 as the +0 or -0 infinitesimal with equal justification, by this reasoning x/0=INF and x/0=-INF.

    (Which all assumes that x is positive, in any case.)

  • minime (unregistered) in reply to evilspoons
    evilspoons:
    This is quite possibly the most retarded piece of code I've seen on here in ages. I am at a loss for words.

    Its fake. Has to be.

  • musk (unregistered) in reply to Ryan

    Actually if the string contains a number then the string is not equals to 0 but is converted to said number according to some algorithm I have long forgotten

  • Billy Goat Gruff #1 (unregistered) in reply to boog
    boog:
    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.

    +ABS(!"php"/"php")

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

    So to cover all bases just average the two.

    i.e. lim(x->0) ((y/x)+(y/-x))/2

    = 0

  • wtf (unregistered)

    The only thing I could think of when reading this was "What the fuck??"

    CAPTCHA: augue. augue, my eyes!

  • Arvind (unregistered)

    This was certainly a joke, and probably a dig at the manager. I can imagine a conversation.

    Manager: I got a call from the client, they see a problem when they divide by 0. Can you do something about it? PHP God: But you shouldn't divide by 0 in the first place. Manager: I know, but the client insists on it. Please do something about it. PHP God: Fine, I will write some funky method which the client can use when they want to divide by 0. I will also show the code to the client so that they are convinced. Manager: Great, and make sure you put enough comments to explain what the new code does. PHP God: Fair enough. Give me 10 minutes.

  • Chris (unregistered)

    UGH!!!!!

    This has GOT to be one of, if not EVER the worst POS i have ever seen...

    If you can't throw an exception, and handle it safely that way, at least, do the next most sane thing...

    function SafeDivideByZero($a, $b) { return( $b == 0 ? INF : $a / $b ) ; )

    ... or possibly NAN, and check for this as a result, and handle it, or better yet.

    However, the simplest and most obvious solution is - don't accept values to be divided with zero in the first place... It doesn't take a brain surgeon or rocket scientist to sort that..

  • Satan (unregistered) in reply to Sam
    Sam:
    Hey guys, I think I found a picture of the PHP God!

    http://static.thehollywoodgossip.com/images/gallery/jared-lee-loughner-mug-shot_385x341.jpg

    (In case you don't get the obscure joke: Loughner's public writings were chock full of constructions like this, "If you can create a new world currency, a new world currency is created. You can create a new world currency. A new world currency is created.")

    Can't be, he looks too normal to be a programmer

  • Peter Wolff (unregistered)
    Justin:
    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.

    Jonathan:
    What happens when something is divided by negative zero?
    Svenson:
    Come on - you know 0 is positive. Just look at the sign bit.
    There IS an answer to these. It is an IEEE thing. See http://en.wikipedia.org/wiki/Signed_zero. (But this could be considered a WTF in itself.)
    Hatshepsut:
    So to cover all bases just average the two.

    i.e. lim(x->0) ((y/x)+(y/-x))/2

    = 0

    I admit I'm too lazy to read that wikipedia article (http://en.wikipedia.org/wiki/Distribution_%28mathematics%29), but iirc maybe this should be something like 1 / (2 pi i). (In anticipation of the bashing)

  • Arkady (unregistered) in reply to Ryan
    Ryan:
    Ryan:
    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

    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.

    There are several languages where any non-empty string, or integer greater than 0, will evaluate to true. Strict type checking is required to differentiate. === vs ==.

    These all evaluate to true:

    0 != "php" "php" == true "php" !== true true == 1

    All that said, the writers idiocy is not up for debate. This is some of the most appalling code I've ever seen in ANY language.

    I just correct my previous statement: We, in the office here, just tested that in PHP the integer 0, does indeed equal any string, but any (non empty) string also equals TRUE. An empty string also equals FALSE. It appears that PHP contains a rather large logical fallacy when it comes to these sorts of logical tests. In short, he could have used ANY string instead of "php" in the WTF function. I feel like a noob.

    The integer 0 most emphatically does not equal any (as in "all") string. Only those that cannot be parsed as a number. This exists so that you can receive a string "123" (as a GET variable for example) and compare it to an actual integer. It is implicit and I would always use an explicit parse in such a case, but is a convenience method that anyone actually familiar with the language knows. Sadly there's a lot of people who only think they do... If you don't want this behavior, just use ===, same thing as in JavaScript.

  • Ru (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.

    Boooring. You can't just divide through by (x-1), because that's dividing by zero. Now, if you were trying to prove that 1 = "php", that's a different matter altogether...

  • Had a head until I saw this (unregistered)

    I simply don't believe this is real code.

    Surely nobody's that crazy.

  • Yam (unregistered)

    TRWTF is people thinking there's such a mathematical operation as division. Poor things don't know enough multiplication.

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

    Nope, the cardinality of all counting numbers is equal to the cardinality of all primes is equal to the cardinality of all integers which is equal to the cardinality of all rationals.

    Look at it this way: the set of all integers >= 0 is the size as the set of all integers >0. You can pair 0 and 1, 1 and 2, 2 and 3, and so forth. There is always a number from the first to pair with the number from the second. No matter how big X you pick from the first set, there's always X+1.

    And likewise, you can add negatives in. Your pairs are (1, 0), (2, -1), (3, 1), (4, -2), (5, 2) and so forth. Just as there's always X+1, there's also always 2X+1.

    Conversely, the set of primes can't be smaller than the set of counting numbers. No matter how large a number I can imagine, by Euclid, I can always find a set of primes that is that large.

    And for rationals, which are just integer / natural, imagine laying them out on a grid, X/Y. There will always be some integer X * Y that corresponds to X/Y.

    So even though the rationals are "two-dimensional," there is still a 1 to 1 correspondence between them and the counting numbers. They are still "countably infinite."

    You only lose that correspondence when you step up to real numbers; hard to explain and most of the explanations on the web are lousy.

    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).

    Eh, it's a computer so I wouldn't read too much into it.

    If I were writing a constraint based language, it could accept, e.g., x*x = 16, because it could try running other code with both x=4 and x=-4. It could even return two results.

    But when dividing any x by 0, you approach both infinity and -infinity, you can't say it only equals (or approaches) one of them. So there's no solution that satisfies the equality. Not sure if this is any clearer.

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

    It's been ages since I read that one...

  • Billy Goat Gruff #1 (unregistered) in reply to Ru
    Ru:
    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 - difference of perfect squares) and add 1 back onto the LHS while you're not looking => x+1 = 1 (divide both sides by (x-1)) => x=0 (subtract 1 both sides) BUT...x=1 => 1=0

    QED.

    Boooring. You can just divide through by (x-1), because that's how calculus works. Now, if you were trying to prove that 1 = "php", that's a different matter altogether...

    FTFY

  • (cs) in reply to Divine Math

    LOL! Even my 8-year-old nephew knows that dividing by zero technically gives you infinity.

  • (cs) in reply to Billy Goat Gruff #1
    Billy Goat Gruff #1:
    Ru:
    New Dog:
    Let x = 1 => x^2 = 1 (square both sides) => x^2 -1 = x -1 (subtract 1 from both sides and convert 1 to x on the right side while you're not looking) => (x-1)(x+1) = x-1 (simplify LHS - difference of perfect squares) => x+1 = 1 (divide both sides by (x-1)) => x=0 (subtract 1 both sides) BUT...x=1 => 1=0

    QED.

    Boooring. You can just divide through by (x-1), because that's how calculus works. Now, if you were trying to prove that 1 = "php", that's a different matter altogether...

    FTFY

    TRFTFY

  • Jay (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.

    True, but only for sufficiently small values of "1".

  • John (unregistered)

    I can deal with the implicit conversions and boolean tests.

    But how would the denominator ever get set to the string "php" ?

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

    Umm, I don't think that will work. I don't know PHP, but in most languages, dividing a sufficiently large number by minValue will result in an overflow.

  • Jay (unregistered)

    Maybe this is the least of what's wrong with this code, but why in the world does he generate a random integer between 0 and 2, and if the result is zero loop around and generate another? Umm, wouldn't it be easier to just generate a random integer that is either 1 or 2? Or if he can't figure out how to generate random numbers that do not start from 0, just generate 0 or 1. All he's doing is checking for ==1 or !=1 anyway. 0 is just as not-equal to 1 as 2 is.

    Well, maybe I should be careful about leaping to such a conclusion for PHP. If "php"==0 and 0==true and all that, maybe 1==0 sometimes returns true.

  • (cs) in reply to Jay
    Jay:
    If "php"==0...
    Actually, this is something I was wondering about, since I haven't used PHP in a while. (0 == "php") evaluates to true because it is a numerical comparison and when "php" is converted to a number it is 0. But would ("php" == 0) be a numerical comparison or a string comparison; would it end up converting 0 to a string, making it "0" and causing the condition to evaluate to false?

    If (0 == "php") != ("php" == 0), that would be TRWTF.

  • (cs) in reply to Ted C. Howard
    Ted C. Howard:
    This obviously confirms the existence of the Holy PHP Trinity.
    No, this confirms the existence of the Un-Holy PHP trinity
  • Anon (unregistered) in reply to Billy Goat Gruff #1
    Billy Goat Gruff #1:
    Ru:
    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 - difference of perfect squares) and add 1 back onto the LHS while you're not looking => x+1 = 1 (divide both sides by (x-1)) => x=0 (subtract 1 both sides) BUT...x=1 => 1=0

    QED.

    Boooring. You can just divide through by (x-1), because that's how calculus works. Now, if you were trying to prove that 1 = "php", that's a different matter altogether...

    FTFY

    We are talking about algebra not calculus

  • Anon (unregistered) in reply to Jay
    Jay:
    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
    }
    

    Umm, I don't think that will work. I don't know PHP, but in most languages, dividing a sufficiently large number by minValue will result in an overflow.

    Which would throw an exception / crash the software, which is an appropriate response to an attempt to divide by zero!

  • YOSPOS (unregistered)

    Unless this in turn came from someplace else, this "PHP god" is not real, but instead is copy-pasted from a prankster on the Something Awful forums who posted this to make fun of another poster (note that the prankster has a period in his name while the target of his ridicule does not):

    http://forums.somethingawful.com/showthread.php?threadid=3407474&pagenumber=20&perpage=40#post391261029

    If you get a login error, an HTML copy of original page can be found here:

    http://www.mediafire.com/?n1z8g2mx9bw324m

  • eric (unregistered) in reply to frits

    @frits:

    trinity? :)

  • AL (unregistered) in reply to frits

    It's the royal we.

  • Funky D (unregistered)

    This feature would have been much, much easier to implement in BobX

  • Me (unregistered)

    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. :)

Leave a comment on “Divine by Zero”

Log In or post as a guest

Replying to comment #349201:

« Return to Article