- 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
Very clever. But I dare you to paste the code into a program, compile to assembly with full optimization, and see if it isn't replaced with a direct assignment.
Admin
That's as may be... but some prefer the transparency of wetware compiler optimization.
Admin
Admin
Now that's a whole 'nother WTF. What did Knuth say about optimization?
Admin
You cared to realize that it was declared as the following?
Now if you insist on finding a way out of this by claiming that this could be a fixed point number in spite of being declared a float, that would be an even bigger WTF. Maybe that was some kind of language where you can redefine base types? ;-) Maybe it would be clever then to redefine a string type as 'boolean'? Holy crap!
And if the author of this piece of crap has found it clever to represent what we could think are amounts of money as floating point numbers, this is very, very frightening. Just imagine this is a banking software...
Admin
And even that can be dicey if the result of GetState() can change between calls. The second GetState() call returns the state originally stored in nOldState - now update, or not?
Admin
I actually encountered some code like this in some avionics code I was reviewing and inquired about it. It was for debugging. They could set a trap on assignments to a variable and only wanted to trap when the value changed. So this is what they did.
Admin
In de.comp.lang.php.misc somebody asked for a better way to find the inverse of a boolean. His current implementation looks like:
<?php
switch($var) {
case false:
$var = true;
break;
case true:
$var = false;
break;
}
?>
Admin
A friend of mine did consulting work at a company a couple of years ago. In their codebase, he found this little piece of beauty:
BOOL IsZero(float Number)
{
char String[256];
sprintf(String,"%0.1f", Number);
if( strcmp(String,"0.0") == 0)
return TRUE;
return FALSE;
}
/Mikael
Admin
This obviously tests for abs(Number)<0.05, in a very inefficient way.
Admin
Negative numbers?
Admin
The correct way would be:
if (twoBeer || !twoBeer)
{
return that_is_the_question();
}
Admin
So, am I correct in saying that the main reason not to use "float" to represent currency is that a number that can be represented exactly in decimal floating-point, like "0.01", can never be represented exactly in binary floating-point, because it would have to be represented by a repeating decimal (i.e. 0x1.47AE147AE147AE... and so on)?