 Feature Articles
 CodeSOD
 Error'd
 Forums

Other Articles
 Random Article
 Other Series
 Alex's Soapbox
 Announcements
 Best of…
 Best of Email
 Best of the Sidebar
 Bring Your Own Code
 Coded Smorgasbord
 Mandatory Fun Day
 Off Topic
 Representative Line
 News Roundup
 Editor's Soapbox
 Software on the Rocks
 Souvenir Potpourri
 Sponsor Post
 Tales from the Interview
 The Daily WTF: Live
 Virtudyne
Admin
... so I'm not going crazy then. :)
Admin
Hmmm, who ever except some much less divine colleagues would ever have the chance to read this selfworshiping 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.
Admin
Oh. MY. F*µ£C%Z£Ë%Zµ£Eµ£ZE££µEZ£. GOD.
This has to be the best code SOD in the history of this website.
Admin
You're assuming it should average to 0.
It should averate to plus or min infinity.
Admin
Of course, I mostly work with realtime systems so the impact of trying something twice is significant.
Admin
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!
Admin
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.
Admin
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.
Admin
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.
Admin
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?)
Admin
This is all that PHP God's code is actually doing:
PHP God sure likes to babble.
Admin
The limit of x/y as y > 0 is infinity.
Therefore, the function is, on average, wrong.
Admin
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.
Admin
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!
Admin
WTF?! Needlessly twisted logic. Just do this:
QED
Admin
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?
Admin
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.
Admin
Seriously, how could you not know this? It's the oldest trick in the book!
Admin
I heard L'Hopital could divide by zero...
Admin
I love the "PHP Warning: Division by zero" that you managed to tease out of his zeroproof algorithm.
Admin
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.
Admin
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
Admin
What's the fuss? I just return 0 when I get a divide by zero. It's the obvious answer.
Captcha: persto (chango!)
Admin
The logic is undeniable, if the logic is undeniable.
Admin
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.
Admin
Haha  I thought this was just regular php code. You dodgy hackers.
Just kidding folks. Love the quote above
Admin
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
Admin
Oh, that's just the "royal we"!
:)
Admin
You forgot to optimise it:
Admin
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.
Admin
Admin
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!
Admin
You slackjawed peasant! It's only natural that gods and royalty refer to themselves as "we" by their divine providence!
Admin
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.
Admin
Captcha: Populus  Pretending to be God
Admin
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; }
Admin
The quotient shouldn't average out to zero. For any constant numerator, the quotient approaches infinity (or negative infinity) as the denominator approaches zero.
Admin
Wow! Now this WTF posted 20110218: [image] makes perfect sense!
Admin
This seems fitting: http://www.youtube.com/watch?v=2s1MspmfEwg
Admin
" 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.
Admin
What the sweet fuck is this shit?
Admin
So what this code does is determine how many orphans are abused in the arctic region ?
Admin
It's the royal we. Remember, this is a PHP god we're talking about.
Admin
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.
Admin
Admin
What I want to know is, what code actually called this masterwork, and what did it expect to gain by doing so?
Admin
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.
Admin
Reimplemented in perl
https://gist.github.com/1556389
Admin
You made me laugh as much as the article did, thank you :D
Admin
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); }