Comment On When "n * (-1)" Won't do ...

... just come up with an overly complicated function that achieves the same thing, like this one discovered by mightydog: [expand full text]
« PrevPage 1Next »

re: When "n * (-1)" Won't do ...

2004-08-12 14:16 • by Todd
Will this even compile? There is no ErrorHandler tag to go to incase of an error.....

re: When "n * (-1)" Won't do ...

2004-08-12 14:43 • by Jake H
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.

re: When "n * (-1)" Won't do ...

2004-08-12 14:51 • by Phil
Why multiply when you can use the unary minus operator! :)

(BTW, great find!)

re: When "n * (-1)" Won't do ...

2004-08-12 14:55 • by Bruno Bord
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

re: When "n * (-1)" Won't do ...

2004-08-12 15:03 • by Qwerty
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?

re: When "n * (-1)" Won't do ...

2004-08-12 15:07 • by Miles Archer
It's a performance optimization
;-)

re: When "n * (-1)" Won't do ...

2004-08-12 15:10 • by Randy Glenn
dblTempValue isn't initialized to zero. On the off chance that it's not zero, this routine's result will be wrong.

re: When "n * (-1)" Won't do ...

2004-08-12 15:13 • by Qwerty
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".

re: When "n * (-1)" Won't do ...

2004-08-12 15:16 • by Jon Choy
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...

re: When "n * (-1)" Won't do ...

2004-08-12 15:18 • by Ralph
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

Absolute Images

2004-08-12 15:24 • by Alex Papadimoulis
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?

re: When "n * (-1)" Won't do ...

2004-08-12 15:56 • by Simon Smith
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....

re: When "n * (-1)" Won't do ...

2004-08-12 16:10 • by Jeffrey Palermo
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.

re: When "n * (-1)" Won't do ...

2004-08-12 16:12 • by Ron
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));

re: When &quot;n * (-1)&quot; Won't do ...

2004-08-12 16:19 • by Michael Giagnocavo
I can't figured why he assumed Abs() would work. He should have implemented that inline.

re: When &quot;n * (-1)&quot; Won't do ...

2004-08-12 16:40 • by Tim Cartwright
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....

re: When &quot;n * (-1)&quot; Won't do ...

2004-08-12 16:40 • by Barry Etter
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!

re: When &quot;n * (-1)&quot; Won't do ...

2004-08-12 17:14 • by Phil Scott
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.

re: When &quot;n * (-1)&quot; Won't do ...

2004-08-12 17:54 • by wx
so why

n = -n

does not work?

re: When &quot;n * (-1)&quot; Won't do ...

2004-08-12 18:03 • by stick
Nicely commented by the way. I wonder how many revisions did it take to finally make it "right"?

re: When &quot;n * (-1)&quot; Won't do ...

2004-08-12 22:11 • by Qwerty
wx: If you are refering to my post, Jon Choy corrected me.

re: When &quot;n * (-1)&quot; Won't do ...

2004-08-12 23:52 • by Matthew W. Jackson
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.

re: When &quot;n * (-1)&quot; Won't do ...

2004-08-13 01:57 • by asdfs
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.

re: When &quot;n * (-1)&quot; Won't do ...

2004-08-13 03:53 • by wx
asdfs - well, the code is mighty fine, just the idea...

otoh, if you chech the posts about database driven code.... that's WTFFFFF....

re: When &quot;n * (-1)&quot; Won't do ...

2004-08-13 09:42 • by Tim Smith
-- 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.

RE: When &quot;n * (-1)&quot; Won't do ...

2004-08-13 14:52 • by mearls@hotmail.com (Michael Earls)
Ralph, upgrade RSSBandit. I use RSSBandit and there's nothing wrong with this feed.

re: When &quot;n * (-1)&quot; Won't do ...

2004-08-13 15:57 • by Kennedy
mightydog,

Do you swear to God this is real code and not a hoax?

re: When &quot;n * (-1)&quot; Won't do ...

2004-08-15 02:47 • by foxyshadis
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.

re: When &quot;n * (-1)&quot; Won't do ...

2004-10-20 16:54 • by Michael
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?

re: When &quot;n * (-1)&quot; Won't do ...

2004-10-27 15:58 • by Jonathan
The client must have been paying by the KLOC.

Re: When "n * (-1)" Won't do ...

2007-04-26 09:29 • by mighty-muffin (unregistered)
Unless they're planning on changing it at a later date to support star signs instead...

Re: When "n * (-1)" Won't do ...

2008-04-29 00:32 • by Less than Zero (unregistered)
There is a "negative zero" in IEEE floating point representation.

Re: When "n * (-1)" Won't do ...

2008-06-23 06:33 • by Milano (unregistered)
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)

Re: When "n * (-1)" Won't do ...

2010-02-03 03:36 • by cbhacking (unregistered)
297904 in reply to 191984
Less than Zero:
There is a "negative zero" in IEEE floating point representation.

I was going to point out exactly this. The floating point specification is bloody hard to read (makes twos-complement integers look completely straightforward) but the one very easy part to understand is that it has a specific bit (63 in a double, as it happens) reserved as a sign bit, and no other purpose. Among other things, this allows the presence of both -0 and -Infinity. I'm not sure about -NaN.

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):
unsigned long long* ptr = (unsigned long long*)&dblAmount;

*ptr ^= (1ull << 63);
return *((double*)ptr);

There, isn't that so much better? ;-)

Re: re: When &quot;n * (-1)&quot; Won't do ...

2011-07-01 12:39 • by Anonymous (unregistered)
351972 in reply to 23212
wx:
so why

n = -n

does not work?


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.
« PrevPage 1Next »

Add Comment