- 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
Ah OK, I stand corrected, too much time spent with static, strongly typed languages
Admin
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?
Admin
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.
Admin
I only have one testicle :(
Admin
Admin
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?
Admin
Well duh:
http://en.wikipedia.org/wiki/Majestic_plural
Admin
Thank you!
Admin
Admin
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.
Admin
Never ascribe to cunning what can adequately be explained by stupidity.
Admin
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.
Admin
Hahaha I feel sorry for the person whom took over that code base.
Admin
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:
Admin
Admin
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.
Admin
Admin
This function was just a placeholder until he could write one that would contact Chuck Norris.
Admin
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.
Admin
I am just going to go with WOW on this one. That should just about cover it.
Admin
FTFY.
Admin
Congratulations... You just created Skynet.
Admin
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.
Admin
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).
Admin
This obviously confirms the existence of the Holy PHP Trinity.
Admin
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.
Admin
Admin
What happens when something is divided by negative zero?
Admin
Apparently you're invoiced for it...
Admin
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.
Admin
I'm no PhP coder, but isn't that equal to
?
I assume if $denominator == "php" it will not == TRUE.
Admin
the flow chart contradicts the code and common sense.
if $n==0, how can $n==1 at the next step?
Admin
Which one?
Admin
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...
Admin
Admin
I would seriously consider discretely sneaking over to his desk and dumping a pot of coffee on his keyboard to save future victims.
Admin
No, he's just proven that equality is not transitive in PHP. Which is, of course, the real WTF.
Admin
TRUE == 1 => TRUE TRUE === 1 => FALSE
Get it by now? As in every language, you have to know how to use it.
Admin
Make sure he's using it when you do that...
Admin
Come on - you know 0 is positive. Just look at the sign bit.
Admin
Any imaginary number squared is negative including imaginary zero squared.
Admin
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.
Admin
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?
Admin
Not according to Brahmagupta.
Admin
It's God. He's one and three at the same time.
Admin
All much too complicated.
Perfectly safe. --Joe
Admin
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...
Admin
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.
Admin
I feel dumber having read through that.
Admin
TRWTF is he's not using mt_rand().