- 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
This is great! Almost as good as:
<FONT face="Times New Roman">And I assume that the n!=0 is because of Division by Zero errors^H^H^H^Hexceptions that were originally generated by that function.</FONT>
Admin
Admin
It's javascript, so some sins must really be forgiven.
I've often had to do things I'm not proud of to work around some obscure problem that some browser somewhere had, esp in the 'old days' of Netscape 4 and IE 3.
BTW, I think IT workers should have evolved past sexism right?
Admin
Actually I prefer this:
<FONT face="Courier New">function isNegative( n ) {
return n != Math.abs(n);
}</FONT>
(Or equivalent)
Admin
Darn, I should've read all the other posts first before posting that gem...WTF is wrong with me?
Admin
n is a complex number or quarternion?
Pity the type info isn't there.
Admin
!=, when used to compare booleans, is exclusive or. That is,
(A != B) == ((A || B) && !(A && B))
So this expression requires exactly one of them to be an empty string. That just seems like an incredibly odd requirement, I expect || would do what's intended. Perhaps, though, both empty is allowed for a silent no-op or something.
Admin
This thread has made me want to yell Read The Damn Thread First a lot more than usual, like some old codger. But I'm in an irritable mood today.
Thank god someone else here understands floating point arithmetic beyond an 8th-grade algebra level. (VBScript, being the 'friendly' half the MS scripting language family, would fix up the miss. JScript has pretentions to being IEEE-754 compliant.) In fact, I can't remember offhand, but JScript/ECMAScript might promote int->float for division, and then you've got a real mess on your hands.
Okay, that's the first time I've ever had a 'freetextbox not installed' error. This is getting like bingo, whoever sees all the board problems first wins. =D
Admin
This thread has made me want to yell Read The Damn Thread First a lot more than usual, like some old codger. But I'm in an irritable mood today.
Thank god someone else here understands floating point arithmetic beyond an 8th-grade algebra level. (VBScript, being the 'friendly' half the MS scripting language family, would fix up the miss. JScript has pretentions to being IEEE-754 compliant.) In fact, I can't remember offhand, but JScript/ECMAScript might promote int->float for division, and then you've got a real mess on your hands.
Okay, that's the first time I've ever had a 'freetextbox not installed' error. This is getting like bingo, whoever sees all the board problems first wins. =D
Admin
So to summarize the IMHO best responses so far:
(2) is not really a WTF. It's probably broken if used with floating point, and awkward, because it could have been written better. But its original genesis was to work around < and > bugs in implementations of JavaScript embedded in HTML.
(3) is not really a WTF. It's probably C# code, so the comparisons should work properly. The != is the desired operator, because the search should fail iff exactly one of FirstName and LastName is supplied.
(1) is a colossal WTF.
All in all, though, a subpar day for WTF.
Admin
Actually this is either VB.NET or C# code :)
Admin
Ugly enough alright, but it also assumes 0 is a negative number...
So it may be a little less ugly, but as opposed to the original WTF it's actually wrong.
Fabian
Admin
Ehr, where I said said 'negative' I meant 'positive'.
Fabian
Admin
You see, the problem is that, as unknown to many people, "if (n < 0)..." might not work for those negative numbers that are either greater than zero, or completely off the number line.
(How'd I do? :)
dZ.
Admin
Excatly, and as someone pointed out earlier, n is probably a complex number, and there is no "<" and ">" ordering of complex numbers. Math.abs(n) is must be the norm of n. So if you view complex numbers as vectors in the complex-plane, he is making sure it isn't the zero-vector (to avoid divide by 0) and that the vector has a specific direction. Trivial stuff, i'm surprised so many of you missed that.
Admin
No, no, no. We had this many times before. I think we agreed that the best way was defined by wajo:
bool IsNegative(double d)
{
int n = Math.Ceiling(d);
try
{
Object[] o = new Object[n];
}
catch (ArgumentOutOfRangeException e)
{
return true;
}
return false;
}
Admin
Why would you even write a function called isNegative for complex numbers? Negativity has no meaning for complex numbers. If what he really wanted was to check that the imaginary part was zero, and the real part was < zero, then, well, maybe he should have done that. It would be more efficient, and would avoid ugly floating point rounding errors. Trivial stuff, I'm surprised you missed that.
Admin
um...I think that was a joke...
Admin
I've got a great Java WTF that I just haven't had time to submit. Fear not!
Admin
I see a lot of "Paid by the Hour" in this forum; however I've come to the conclusion now that it's not that these people are paid by the hour... I recently come across a lot of this code and have even been able to speak to some of the perpitrators.
What I've discovered is pure cluelessness... they actually think this is a better way to do it.
sigh...
Admin
Thats steam-powered when it comes to modern languages. It didn't have one until the last version when some dumbass added the keywords "true" and "false" and the object java.lang.Boolean.
Of course, now this fellows code is broken, because they didn't consult him/her and now false != "not true".
And for those that were actually thinking of flaming this post at the first paragraph, new word for you: sarcasm
Admin
hungarian? why that?
Admin
"hungarian? why that?"
You have heard of Hungarian Notation, haven't you?
Admin
one more error:
function isNegative( n ) {
<FONT size=+0>return</FONT> n != 0 && n / <FONT size=+0>Math</FONT>.<FONT size=+0>abs</FONT>( n ) == -1 <FONT color=#0000ff>; // he forgot the semicolon</FONT>
}
[<:o)]
Admin
Nope, it wouldn't be portable.
Admin
No, of course not... zero has it's own case...
function isZero(n) {
return (isNegative(n) == isPositive(n));
}
Admin
// Cos JavaScript does not require them (although it is neater to add them)
Drak
Admin
The first is a "why should I use build in features": the toString() method of a boolean returns its value as a word.
The second one is a real nice one. I thought about it for a while, because it irritated me. Why didn't he use a simple "< 0"? Hmm... we'll never find out.
The last one is a typical validating problem. I would've done it like this:
I usually try to avoid "==". Don't ask why, it's a tradition for me :-)
Strange enough: I've seen similar code snipplets more than once.
Admin
You are better off doing && instead of *. Multiplication is slooooooow.
Admin
Branches are only a problem when they are unpredicted. That is why branch prediction is so important. Only unpredicted branches cause cache flushes and the like. Also you are forgetting that in order to multiply you likely have to do something like:
push ax mov ax, 32 push bx mov bx, 2 mul ax, bx at this point, half your result is ax, and the other half is typically in dx. now you still have to do something with the result, which could include both ax and dx. eventually you have to pop ax and bx
Not quite as simple as: and ax, 4 jge _wherever
Admin
I think that this:
should have been:The difference? Just the word 'OR'. He definitely wants the user to fill in either first name or the last name, but not both. And one of these fields must be filled. So this is just a spelling error... [:)]
Admin
Katja: "The difference? Just the word 'OR'. He definitely wants the user to fill in either first name or the last name, but not both. And one of these fields must be filled. So this is just a spelling error..."
Nope, no spelling mistake. This error condition will execute if (and only if) one or other of the two are blank.
The interesting thing is that it doesn't trigger if both are blank. Is this because previous logic guarantees that at least one is not blank? Or does the following logic not mind if both are blank? Who knows?
And Teddy (yes, that's his entire name) is still excluded.
Admin
No it would be XOR. The number of people who have misinterpreted that if in this thread alone qualifies it as a WTF, regardless of whether it works as intended.
Admin
What about this :)
Admin
Um, shouldn't res1 and res2 BOTH be equal to one? Oh wait, you just proved problem #1 with the multiplication solution... OVERFLOW!
Also, the multiplier won't optimize for the zero case, whereas the five branches will. So on average, it will be 10/2 instructions compared to a constant 7. Moreover, You forget that if the comparison is in a for loop, the branch prediction table will be able to properly pipeline all the branches, which would improve it further.
Admin
If the math major minored in physics he would be well aware that quantim effects can flip bits randomly. There is no known proof of correctness in face of this problem, so such a math major would never write one.
Admin
Admin
Actually it should be:
vldSearch.ErrorMessage = <FONT size=+0>"NOT First XOR Last names are required to conduct a search."</FONT>;
My mistake.
Admin
Actually, (this.txtLastName.Text == null && this.txtLastName.Text.Length == 0) is the preferred way to do blank-string checks in the FCL languages.
Admin
Yeah, well. In normal English language you won't use the word XOR to make clear you only mean one option. The word OR should be clear enough for that. If you show just a message to the user, the word OR should just be clear enough.
Admin
Except that it's still not what it means. The natural English explanation would be: "You must both first name and last name or neither."
Admin
Since this is javascript (and supposably embedded in HTML), maybe the programmer wanted to avoid the < and > operators since they would mess the syntax highlighting of his IDE.
Admin
dubwai: maybe your natural English.
this whole XOR/OR/AND stuff gets confussing due to language we are all used to || in JavaScript etc meaning the same as OR in VB.
In english OR generally means XOR ie 'do you want tea OR coffee' yes some people do answer 'yes I want tea or coffee' but generally they don't get made anything for being 'annoying'
come on if english used the same AND and OR as logic does then user requirements would start to mean the same thing to developers and users and what help would that be?
Admin
And another person here demonstrates that he/she doesn't understand what that code does.
Admin
To continue down this path of sillyness...
Admin
I agree. Especially considering that Java has no "bool" data type (it's "boolean"), and neither does JavaScript (it's "var").
Admin
Doh! My last post was supposed to include this...
Admin
I haven't seen the most obvious:
<FONT color=#0000ff>function</FONT> isNegative( n ) {
<FONT color=#0000ff>return</FONT> <FONT color=#000080>Math</FONT>.signum( n ) == -1
}
Admin
Scratch that... [:S]
Admin
I'm not sure if JScript doesn't have some additional features above and beyond what JavaScript/ECMAScript has, but in the latter all numbers are of type "number", and are floating point. So JavaScript doesn't promote int to float, because it doesn't have any ints to begin with.
Since IEEE floating point has a sign bit, there's no way that n / Math.abs(n) can end up with a rounding error. I think it even does the right thing with infinities (haven't tested it). It doesn't detect -0.0 as negative, but you could argue that that's a feature. The interesting cases, to me, are:
If only the code had been written "!= 1" instead of "== -1", then NaN or a string value such as "abc" that would be converted to NaN would be detected as a negative number.
If only the code was written in Java or C# or some language that has fixed-size two's-complement integers, and doesn't automatically promote them to bignums like Smalltalk and python and ruby, then -2^31 (or whatever the value of INT_MIN is) would NOT be detected to be negative.
(Well shut my mouth! I just tried it, and Math.Abs(int.MinValue) throws System.OverflowException in C#. Whereas Math.abs(Integer.MIN_VALUE) just returns Integer.MIN_VALUE in Java. Take that, you MSFT bashers!)