- 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 many of you are wrong. Celsius and Centigrade are not exactly the same. To convert Celsius to Kelvin, you subtract 273. To convert Centigrade to Kelvin, you subtract 273.15. (Centigrade is obsolete.)
Admin
Argh, make that add, not subtract!
Admin
Well to give the Excel-guy the benefit of the doubt: he might just have used that nifty "pull down to increment"-feature instead of typing each value manually. My guess is that is why he did it that way...
Admin
Actually, I liked the first one better :)
Admin
In C "by default" everything return a value. Is what make C.. C. And what "Think in C" is all about. Pascal has procedures and functions, but C only have functions, and functions defined "void", so if theres return values, are random undefined values from AX. Pascal != C.
Admin
No you would have to convert the delta with a C2K function.
Admin
The reason the programmer had used Excel is it's "Fill series" -feature. He had not typed all values by hand, but typed the first value and then dragged the rest with mouse. How clever of him!
Admin
Being a C++ fanboy, I just have to post a templated version of this WTF:
template< int N > struct K2C_factory { static int K2C( int T ) { if( T == N ) return T-273; else return K2C_factory< N-1 >::K2C( T ); } };
template<> struct K2C_factory< 273 > { static int K2C( int T ) { if( T<273 ) printf( "Temp out of range!\n" ); return 0; } };
int K2C( int T ) { return K2C_factory< 303 >::K2C( T ); }
The actual code that is produced by this construct is just as WTF'ed as the original...
Admin
Correction: f( T == N ) return T-273; should read: f( T == N ) return N-273;
Admin
Because that's how it works in Perl (and pressumably other languages too): The lvalue of an expression is its successful completion. So if the assignment succeeded, then its lvalue would be "true". The fact that a failure to assign would generate an exception and fail the program does not change anything; it just means that, in practical terms, assignments would always return "true".
However, in C, the lvalue of an expression is the inherent value of the expression itself.
Anyway, that's how it got started.
Admin
Actually that doesn't look strange at all to me. In my experience, high performance embedded code often has sequences of if..then..else, or lookup tables rather than arithmetic operations, because "if x=y, then a=b" is quicker than a subtract operation. (Although admittedly this depends on the processor).
And the Excel thing - he won't have typed things in manually, he will typed in one line of code and then dragged the row downwards whereupon excel auto-fills the other rows. I do it a lot!
As for the redundant use of the converstion routine -perhaps he found the code ran quicker when subtracting 8-bit integers. If the operation was done in Kelvin, he's dealing with two numbers that don't fit into an 8-bit int. If the processor is an 8-bit one, this may be the case.
Admin
no, a C2K. I think it was in excel so they could automagically populate the numbers.
Admin
Isn't the real WTF the fact that he's using "=" as a comparison operator instead of "==" ? O_o
Admin
They may be more efficient, but as we learned a few days ago they are also very unstable ;)
Admin
well, the excel file isn't necessarily typed by hand - you could just type one of those lines, then select it and drag down: excel automatically increases both numbers on every line (I just tested in oocalc, I assume excel's not braindead). but still...
(also, it should probably be 'return Ktemp-273;', not 'return Ktemp-270;')
Admin
Hmmm...
Admin
I think I know why the programmer used a spread sheet! I think I've got it! I haven't bothered to read the rest of the comments because I'm fairly sure I'm the only person in the whole world who could have realised this, so no one will have posted this before me.
He used excel becaMASSIVE FAIL
Admin
BTW, it's "return Ktemp-273;" not "return Ktemp-270;"
=)
Admin
Looks like someone need to upgrade their retro encabulators.
Admin
Clearly, the real WTF here is why, why, WHY did he not use a case-switch statement here, instead of else-if?
Admin
I wonder if anyone realized that already but he probably didn't type all that manually, he just used the mouse drag / auto fill feature in excel.
But anyway it wouldn't work because he used buttignment operator ( = ) instead of a comparison operator ( == )
Admin
Admin
So, could we simply send out tactical mini vans to anyone mentioning Excel's auto-fill feature from this point on?
Admin
That should be "capacitive duractance", not "capacitive directance".
Admin
I don't know, if you had really spent too much time in physics you would know that water freezes at 0 Celsius only when it has an exact amount of impurities, and is at an exact pressure.
Pure water can be "supercooled" to well below the freezing point. Getting pure water is left as an exercise for the reader (it is almost impossible).
Salt is the best known impurity that changes the freezing point of water. There are plenty of others.
Admin
The real wtf is that the code will always return 0 and he didn't notice. Or he typed it in wrong.
Admin
Interestingly I found excel great for generating large blocks of C code when developing poker machine games. Especially when all the math and symbol data already exists in excel.
Admin
Coming soon - K2C floating point edition
The professional edition of K2C(f) will correctly convert Kelvin to Celcius with 4 decimal points of accuracy.
The enterprise edition of K2C(f) will work up to 8 decimal points.
Mega !!!
Admin
Oh how I wish I could vote on comments! :-)
Admin
Admin
Hum, if that fragment is C, it won't compile of course; with minor modifications it would always return 0 for all inputs. :)
Admin
I get what happened here.
The reason that stuff's in the excel spreadsheet is the guy made a few rows and then used "fill down" to continue the pattern. That's what smart developers do.
Why do the subtraction in code when you can have Excel do all the smart work for you and generate a handy table! And then you can copy and paste it INTO the code! Look, he even filled in the "return" statements. I bet this guy felt so smart.
Admin
Note that the previous 23742374 comments didn't show up before I posted that one; I SWEAR
Admin
Uhm I also suppose they want to compare the temparature, not assign to it ?
Admin
No, no, no! You didn't get the true purpose of this thread. The minivans should go to all the other people who fail to mention the excel feature and the =/== typo in their posts!
Admin
wouldn't that be C2K?
Admin
"Someone had apparently typed each Kelvin and degree Celsius value in by hand" Or typed 2, then clicked and dragged.
Admin
he/she/it used excel to generate n+1 by typing 1[enter]2[enter] highlighting and dragging square handler :)
anyway interesting way to generate C code;)
Admin
Oh coarse they didn't type all the numbers out in excel, they used excels auto range function 1,2 click+drag, simple and efficient (well compared to typing it all out by hand. Still pointless mind ;-)
Admin
Umm, sure you don't need K2C at all if all you ever want to do is calculate the difference.
But, did anyone else notice that K2C always returns 0?
Admin
Hmmm, there weren't 4 pages of comments showing when I originally wrote this comment, only two comments. Go figure.
feeling foolish.
Admin
(Prays that that was sarcasm.)
Admin
a database fronted, of course by a web service found by doing a UDDI lookup from a delegate fronted by an adapter found by a factory. THAT might be enterprisey enough.
Admin
Ah, I see the right solution now! Yes, a database would be a much more elegant solution. Something like:
Admin
Huh? Unless there's something going on with macros, that doesn't even look like valid C code: http://codepad.org/3g1fefQj. Even if you fix that one, those are assignments in the "if" conditions: http://codepad.org/cu1tv3EA.
Admin
I've got to say something. I'm starting to think the WTF stories are being made up. anyone else think so?
Admin
But have you never used Excel before? You can quickly copy blocks of text for repeating, and you can also put a number in a box, drag the cross-hair down a number of cells, and Excel just increments as you go. Very useful. But it would have been better to think about why you can avoid K2C all along.
Admin
Ktemp-273 not 270
Admin
Yeah, the "=" instead of "==" does not have anything to do with wrong function of that procedure.
Admin
You forgot your <sarcasm> tags !