- 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
Will this even compile? There is no ErrorHandler tag to go to incase of an error.....
Admin
Multiplying by -1 would switch the sign? I KNEW I should have paid more attention to that advanced arithmetic class in the third grade... I mean, I tried, but the teacher was too theoretic and political.
Admin
Why multiply when you can use the unary minus operator! :)
(BTW, great find!)
Admin
I really like the line :
dblTempValue = dblTempValue - dblMagnitude
Assuming the fact that at this time of the function, dblTempValue == 0, it means
dblTempValue = 0 - dblMagnitude
Admin
After all that very carefully documented, fully typed, well structured coded. How could the programmer not think to initialize dblTempValue. A professional language & compiler shouldn't even let that build!
Okay my defense: floating point is tricky stuff, maybe subtract from zero really is not the same thing as multiply by -1?
Admin
It's a performance optimization
;-)
Admin
dblTempValue isn't initialized to zero. On the off chance that it's not zero, this routine's result will be wrong.
Admin
2nd tounge-in-cheek defense: didn't an older version of VB syntax not allow the C/++ style statement: "value = -value"? If true, maybe the programer got frustrated, banged the wall with his head, and decided "Fine! I'll farking do it myself".
Admin
As far as I can recall, unary minus has existed at least back to CBM Basic 2.0 era BASICs... heck, even as far back as the LET-is-required era, it should have worked.
I'm SURE that GWBasic, Basic PDS, and VB1.0 had at least that idiom...
Admin
When posting images can you post it with the full path instead of the absolute path because in an RSS reader like RSS Bandit the image comes up as broken.
Thanks
Admin
It seems that .TEXT changes them when i switch from HTML -> Design view. I'll try ... but why can't RSS Bandit figure that out?
Admin
Given that the code actually is quite good, there should be a reason for it. I can think of two:
He's being paid/judged on LOC
Doubles are tricky. They're not exact numbers. Maybe * -1 gave a different result to the one the function gives, and the one the function gives was closer to his requirements. Of course the solution to that is not use doubles, but that wasn't always an ioption ib VB....
Admin
I'm using RSS Bandit, and it came through fine. Is your network cable plugged in? Oh, wait, that's tomorrow's WTF for handling a failed database connection.
Admin
Hmm wondering how many ways there are to flip a sign.
1. return 0 - n;
2. return ~((double)n & (1<<63)) | ((double)n & ~(1<<63));
Admin
I can't figured why he assumed Abs() would work. He should have implemented that inline.
Admin
Hey, this coulda been code out of my inherited app, except thats Visual Basic, and mine is written in ASP / VBScript. AWESOME!!!!!!!! Wonder if the authors were related.... maybe....
Admin
You guys need to think out of the box here.
Maybe he wanted the option to add support for imaginary numbers [Sqrt(-1)] in the future!
Admin
Like Simon, I was thinking that he got paid by the LOC written. But if that's the case, why do the "if dblAmount < 0 then ..." on one line of code? He could have easily bumped that to three by needing an end if statement.
Admin
so why
n = -n
does not work?
Admin
Nicely commented by the way. I wonder how many revisions did it take to finally make it "right"?
Admin
wx: If you are refering to my post, Jon Choy corrected me.
Admin
Shouldn't he be converting the double to a string, checking for a "-" character, removing it or prepending it accordingly, and converting back to a number? In my book, that's they ONLY algorithm that should ever be used.
Seriously, I've considered all of the rounding problems that I know about when it comes to IEEE floating point numbers, and I can't really think of any problems with subtracting a number from zero, since any positive number can be represented exactly as a negative number (due to the existance of an individual sign bit).
Is this code possibly in a project with the "Remove Floating Point Error Checks" and "Allow Unrounded Floating Point Operations" compiler optimizations enabled? If so, there MIGHT be a problem that I'm not aware of, but I seriously doubt it.
Admin
I believe it could however be implemented because of the computer implementation of real numbers. ... probably. But most probably it's not the case - because of the line "if dblAmount=0" - the guy should have known that comparing a double value to ZERO is not a good option :)
However, this post really gets a "WTF?!" from me - it took quite a long time to understand what's being done in the function.
Admin
asdfs - well, the code is mighty fine, just the idea...
otoh, if you chech the posts about database driven code.... that's WTFFFFF....
Admin
-- because of the line "if dblAmount=0" - the guy should have known that comparing a double value to ZERO is not a good option :) --
Why is comparing a IEEE floating point value to zero a bad idea in this case? That is what he wants to do and doing an epsilon test would in fact damage the results. Changing the sign of a IEEE floating point number can/should/must be reversible. (I don't know what the standard says for changing the sign of such things as a NaN.) By doing an epsilon test not only are you making the operation non-reversible but you will now have to provide an epsilon value to the function since there is no way a change sign routine can magically decided what value epsilon should be. There is a HUGE difference between an epsilon in celestial mechanics and an epsilon in sub-atomic research.
Admin
Ralph, upgrade RSSBandit. I use RSSBandit and there's nothing wrong with this feed.
Admin
mightydog,
Do you swear to God this is real code and not a hoax?
Admin
Kennedy, the code I saw for manipulating database values as strings for report generation had a very similar routine for fixed-precision decimal representation, which was also badly broken is some cases. Before scrapping the entire infrastructure, I had to go in and fix it. I wonder if the same guy went on to make mightydog's mess.
By the way, just converting everything to floats sped it up by nearly an order of magnitude.
I wish I'd kept the original source, it had a variety of excellent submissions to this site.
Admin
To Ron, who wrote:
return ~((double)n & (1<<63)) | ((double)n & ~(1<<63));
Umm... WTF? Have you ever thought about the portability issues that code is going to create?
Admin
The client must have been paying by the KLOC.
Admin
Unless they're planning on changing it at a later date to support star signs instead...
Admin
There is a "negative zero" in IEEE floating point representation.
Admin
I improved it a little.
Public Function changeSign(byVal dblAmount As Double) As Double On Error EatMyShorts Dim sValue As String Dim sReturnValue As String sValue = dblAmount.ToString If Mid(sValue, 0, 1) = "-" Then sReturnValue = Mid(sValue, 1) Else sReturnValue = "-" & sValue End If changeSign = sReturnValue.ToDouble Exit Function
(I don't work with VB so this probably won't compile as-is)
Admin
In any case, if for some reason one wanted to avoid unary - or the use of *(-1), another option that would work is below (it's in C since I can't be bothered to look up the functions for changing the type of a variable between float and int without changing the binary, although such functions do exist):
There, isn't that so much better? ;-)
Admin
It does work, that's the point.
The body of the function could be changed to
ChangeSign = -dblAmount
But the function shouldn't have ever existed.