The Daily WTF: Curious Perversions in Information Technology
Welcome to TDWTF Forums Sign in | Join | Help
in Search

Every heard of Unary Negative... guess not?

Last post 07-06-2008 1:59 AM by mattm. 10 replies.
Page 1 of 1 (11 items)
Sort Posts: Previous Next
  • 07-03-2008 5:45 AM

    Every heard of Unary Negative... guess not?

    I never thought I'd see code that warranted a WTF?

    But here it is... spotted in an Access VBA project while working today.

    '   4...    Convert to positive number
            If x <> 0 Then
                gGetRecoveryCashPayments = x - (x * 2)
            End If

     

    x was known to be negative as it was the sum of a bunch of negative numbers.  But the report should have displayed a positive to make more sense to humans.

     

    Needless to say, the code is now:

     

    gGetRecoveryCashPayments = -x

     

    Typical of this project - when I finish reviewing & updating a module there is typically about 25% of the code left, more functionality and less bugs.

     

    ps. laughed for about 15 minutes on this one!

    pps. even worse, I had to test the logic... and it works!

  • 07-03-2008 7:09 AM In reply to

    • DOA
    • Top 75 Contributor
    • Joined on 06-26-2007
    • Posts 325

    Re: Every heard of Unary Negative... guess not?

    I don't know what he was smoking but I'd like to know where I can get some. 

    Hey, shouldn't this be in the sidebar WTF forum? 

  • 07-03-2008 8:01 AM In reply to

    Re: Every heard of Unary Negative... guess not?

    I'd like to know what the <> 0 check is for. Optimization?
    ╩юфют√ь ёЄЁрэшЎрь яюЁр эр яхэёш■.

    Visit #TDWTF @ SlashNET - the semi-official WTF IRC channel.
  • 07-03-2008 9:22 AM In reply to

    Re: Every heard of Unary Negative... guess not?

    mightydrew:

    Needless to say, the code is now:

     

    gGetRecoveryCashPayments = -x

    No, you got it all wrong.

    gGetRecoveryCashPayments -= 2 * x * -1 /2

    EDIT:  Shit, I can't read.   fixed

    I guess I'm back.

    Please continue to spam the addresses below.

    PLEASE SPAM:
    jtobin@ohioinstituteofhealthcareers.edu
    jtobin@ohiobusinesscollege.edu
  • 07-03-2008 9:45 AM In reply to

    • ammoQ
    • Top 10 Contributor
    • Joined on 04-13-2005
    • Vienna.Austria.Europe.Earth
    • Posts 3,326

    Re: Every heard of Unary Negative... guess not?

     Thread moved to side bar wtf.

    beanbag girl 4ever
  • 07-03-2008 9:48 AM In reply to

    Re: Every heard of Unary Negative... guess not?

    Does VBA not have an absolute value function?

  • 07-03-2008 9:54 AM In reply to

    Re: Every heard of Unary Negative... guess not?

    Someone You Know:

    Does VBA not have an absolute value function?

     

    http://www.google.com/search?&q=vba+abs&btnG=Search

  • 07-03-2008 3:25 PM In reply to

    Re: Every heard of Unary Negative... guess not?

     The first thing I thought, when seeing this, was "why doesn't he simply used x*x to make it positive, or better sqrt(x*x)"

    (that happens when you work to much which vector length definitions...)

  • 07-03-2008 6:33 PM In reply to

    Re: Every heard of Unary Negative... guess not?

    mightydrew:

            If x <> 0 Then

    Heh. That line cracks me up! if x is positive, you'd be churning out negative numbers. Even if x is supposed to be positive, you can't code with that assumption. That kind of stuff is what usually leads to someone withdrawing -500 bucks... ;)

  • 07-04-2008 1:28 AM In reply to

    Re: Every heard of Unary Negative... guess not?

     After studying IEEE floatint point formats for a week I once implemented your "x = -x" code as something like "x ^= 1<<31".... and then I realised wtf I was doing...

  • 07-06-2008 1:59 AM In reply to

    • mattm
    • Not Ranked
    • Joined on 07-06-2008
    • Posts 2

    Re: Every heard of Unary Negative... guess not?

    ActionMan:

     After studying IEEE floatint point formats for a week I once implemented your "x = -x" code as something like "x ^= 1<<31".... and then I realised wtf I was doing...

    God forbid you decide x should be an integer or double instead of a float!

Page 1 of 1 (11 items)
Powered by Community Server (Non-Commercial Edition), by Telligent Systems