• Todd (unregistered)

    Will this even compile? There is no ErrorHandler tag to go to incase of an error.....

  • Jake H (unregistered)

    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.

  • Phil (unregistered)

    Why multiply when you can use the unary minus operator! :)

    (BTW, great find!)

  • Bruno Bord (unregistered)

    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

  • Qwerty (unregistered)

    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?

  • Miles Archer (unregistered)

    It's a performance optimization
    ;-)

  • Randy Glenn (unregistered)

    dblTempValue isn't initialized to zero. On the off chance that it's not zero, this routine's result will be wrong.

  • Qwerty (unregistered)

    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".

  • Jon Choy (unregistered)

    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...

  • Ralph (unregistered)

    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

  • Alex Papadimoulis (unregistered)

    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?

  • Simon Smith (unregistered)

    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....

  • Jeffrey Palermo (unregistered)

    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.

  • Ron (unregistered)

    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));

  • Michael Giagnocavo (unregistered)

    I can't figured why he assumed Abs() would work. He should have implemented that inline.

  • Tim Cartwright (unregistered)

    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....

  • Barry Etter (unregistered)

    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!

  • Phil Scott (unregistered)

    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.

  • wx (unregistered)

    so why

    n = -n

    does not work?

  • stick (unregistered)

    Nicely commented by the way. I wonder how many revisions did it take to finally make it "right"?

  • Qwerty (unregistered)

    wx: If you are refering to my post, Jon Choy corrected me.

  • Matthew W. Jackson (unregistered)

    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.

  • asdfs (unregistered)

    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.

  • wx (unregistered)

    asdfs - well, the code is mighty fine, just the idea...

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

  • Tim Smith (unregistered)

    -- 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.

  • [email protected] (Michael Earls) (unregistered)

    Ralph, upgrade RSSBandit. I use RSSBandit and there's nothing wrong with this feed.

  • Kennedy (unregistered)

    mightydog,

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

  • foxyshadis (unregistered)

    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.

  • Michael (unregistered)

    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?

  • Jonathan (unregistered)

    The client must have been paying by the KLOC.

  • mighty-muffin (unregistered)

    Unless they're planning on changing it at a later date to support star signs instead...

  • Less than Zero (unregistered)

    There is a "negative zero" in IEEE floating point representation.

  • 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)

  • cbhacking (unregistered) in reply to Less than Zero
    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? ;-)

  • Anonymous (unregistered) in reply to wx
    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.

Leave a comment on “When "n * (-1)" Won't do ...”

Log In or post as a guest

Replying to comment #:

« Return to Article