- 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
I kinda like it. It's funny.
Admin
Well, at least his cs degree is justified. He obviously paid attention during classes...
Admin
The code itself looks like it works correctly though.
I'm trying to imagine what this programmer would come up with if you told him the next version needs to support Unicode.
Admin
The same solution of course. He might take take a bit longer and his file might become a bit larger, but he'll still be finished after finite time, so no need to change a working solution ;)
Admin
Did anyone see the comment above arg == NULL: If the string is empty, then it does not contain a float. Looks like someone has been copy-n-pasting.
I also like that he uses a "sink" state, instead of immediately quitting as soon as the prefix is illegal. But well, that's my obsession, I guess.
Admin
The behaviour is different than with atoi. atoi("123blah") would be 123, but this function would return 0, because of the non-numeric characters.
This code makes me cry; it reminds me too much of my own workplace where everybody is reinventing the wheel!
Admin
Lovely. Truly his understanding of pointers --- and casting thereof --- is unsurpassed. On those platforms which support signed chars, what is the value of negative-fifty-four cast to be an unsigned char?
And bonus points: the only reason for casting is to serve as an index in an array. We cannot but admire this mind.
Admin
A crash.
Admin
Except that state 2 and state 3 are identical, so he couldn't have been paying that much attention.
Admin
Admin
I thought that at first myself, but the difference is that if the code ends in state 2 it's not a valid integer (it's just "+")
Admin
I think it's quite a nice hack in theory.
More of a "WhyTF did he do this?"
Admin
You sure about that?
I'm not about to do the twos-complement math but I'm pretty confident that whatever 8 bits represent -54 will make a perfectly good unsigned char.
Admin
I can't help wondering whether this was actually all hand-entered or generated by some finite state automation program from a specification for unsigned numbers.
Admin
but you have to admit.. they used a finite automata to do that! I don't understand why they don't use a turing machine.
Admin
Why on earth do you think the concept meaningful?
That's two. Anyone else going to make that mistake?Admin
is not finite state arg. is finite state argh.
Admin
The Real WTF (tm) is that he initialized a 256-sized array writing 20 numbers per line thus leaving some space on the last line. That's ugly. Everyone knows 256 sized array should be initialized with a nice 16x16 block.
Admin
I'm sorry, but that rocks. It may not be the kind of code you want in production, but from a mathematical/CS point of view, it's just cool.
Admin
If efficiency was his goal, shouldn't he have put
before allocating that big array?I would have just made it a global variable.
Admin
Can someone explain this code? I dont totally understand how it works.
Admin
He he,
Though I would perhaps question the wisdom in using a state table on a per-character basis as done here, I did once use a state table for checking the correctness of a php array before it was upload to a produciton environment.
Though it didn't deal with single characters but regular expression patterns, i.e what pattern could legally occur after a given previous pattern had been found.
Admin
Admin
Admin
Yeah, sure.
From the C++ standard, §4.7 Integral conversions: (2) If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2n where n is the number of bits used to represent the unsigned type). [Note: In a two's complement representation, this conversion is conceptual and there is no change in the bit pattern (if there is no truncation).]
Admin
States 2 and 3 are necessary, to catch a lonely "+" sign, or sequences of pluses ("++...+"). That said, too bad he did not exit the loop as soon as state 0 is entered.
IMO the approach certainly is not stupid, it's very fast and robust. The next best thing would have been to use a regex.
Admin
Arrgh! The goggles they do nothing! Dear lordy, W-H-Y, was this code written to win a bet or something??!
Admin
@dpm:
Science v.s. brute force.
Now try to detect a real, including in scientific notation.
;-)
Admin
You know what, people?
Artificial Intelligence exists. It lives on the net, takes up mundane jobs like freelance programming to earn its server space and bandwidth, and tries to act and look like human but sometimes fails miserably.
Admin
I especially like how maintainable it is; for example, adapting this to support recognizing hex numbers would only involve hunting down and modifying, what, 36 constants?
Admin
It can do anything it likes. But I'm talking about what it will do.
Admin
Maybe it's just me, but it doesn't seem to me like this code "works" (in the sense that it accurately accomplishes the task at hand). Unless I'm mistaken, since he never exits his loop when an incorrect character is found then any string that ends "correctly" would be considered an unsigned int, no?
wouldn't 'wtf123' return "true" ?
Admin
And carefully, because as we know, if there's too much static you can get a shock as soon as you try to touch it!
Admin
From my copy of the (C99) standard, par. 6.3.1.3:
There's nothing undefined about this. Now, what you might have said is that the result is implementation-defined, because the actual value depends on the size of a char. That's right, boys and girls, the C standard doesn't say a char has to be 8 bits. If you actually see an implementation where a char is not 8 bits, though, congratulations, you're probably working on embedded software, and good luck with that.Admin
202, if characters are eight bits. 458 if it's nine bits. etc, etc, 4294967242 if it's 32 bits. And, before anyone says anything, yes this IS guaranteed on non-twos-complement systems - the negative-to-unsigned conversion is well-defined in C.
Admin
Admin
Nevermind... I of course missed the "genius" of his sink state.
Admin
Admin
And if it's being converted to _Bool, it's _True if it's positive, _False if it's zero, and _File_Not_Found if it's negative, right?
Admin
No. The first 'w' would drop the state to 0. It is not possible to switch to any other states from 0.
I, for one, think this is actually quite cool. Even cooler would be generating the state machine from a regex and making the generator available somewhere.
Admin
This is functionally equivalent I believe (above code handles +'s differently):
Admin
Intrigued by this comment, I checked the c99 standard, which states for signed-to-unsigned conversion :
Which, in layman's terms, for like-sized chars/ints/longs, flips the most significant bit on/off (which in turn means "nothing to do" : the unsigned value's MSB is at the same position as the signed value's sign bit). It works as expected for Mr CodeSOD, and is a perfectly defined behavior.Concerning the unsigned-to-signed conversion, the behaviour is indeed implmentation-defined (aka "undefined" as dpm pointed out). However, I can't think of any C compiler that wouldn't simply read the bits as a signed value : same code as for the signed-to-unsigned conversion (aka "nothing to do"), and for error-checking, a compile-time warning is usefull and certainly enough.
Admin
Definitely better than mine. Thanks.
Admin
Admin
Looks to me like someone was bored.
Admin
Actually I'm feeling silly, because I just realised I misread your code, I'm sorry dpm
Admin
Admin
Another wtf is that the TDWTF homepage reports this article as having 1222 words....
Admin
Is everyone here an expert in all aspects of computers? I mean, it's like you guys know the exact details of every single submission. I forgot about FSAs and shit as soon as class ended. Do you people do this for your jobs? My biggest concern at work is timing my lunch break to coincide with co-worker because he makes noises when he eats. I have to leave the room. And why does everyone in the office have to shuffle their feet when they walk? What the fuck! Lift your fucking legs.
Admin
Integral conversions are similarly defined in C89 -- see A6.1. What is dpm going on about?