The really stupid way of turning a number negative



  • This is the stupid way of turning a number negative:

    [code]function is_neg ($num) {
       return $num < 0;
    }
    
    function turn_neg ($num) {
        if (is_neg ($num)) {
           return $num - $num * 2;
        } else {
           return abs ($num);
        }
    }
    
    turn_neg (2); // -2
    turn_neg (-2); // 2[/code]

    (Also, to post a code formatted text in this forum, use: <pre>[code] put codes in here [/code]</pre> It is a bit stupid, but that's the way it works)



  • That code as it is written will always return a positive number. Either you copied it wrong, you misunderstand it, or your mastery of the forums needs work.



  • Actually, for the positive case it is just returning abs($num), so the function should be named turn_positive.



  • @Welbog said:

    That code as it is written will always return a positive number. Either you copied it wrong, you misunderstand it, or your mastery of the forums needs work.

    Yes I know that. But this code is from some example in PHP web-site I know it doesn't work!! The example of what it returns is also wrong. They also say that $num-$num*2 will return 6 if $num==-2 but of course that isn't true either. They claim it is the reverse of the abs function but of course it isn't. Who wrote that???



  • @zzo38 said:

    Yes I know that. But this code is from some example in PHP web-site I know it doesn't work!!
    See, you should have said that. Without saying that, you look like you don't know what you're doing. With that information, though, that snippet you posted becomes a WTF.



  •  @Welbog said:

    That code as it is written will always return a positive number. Either you copied it wrong, you misunderstand it, or your mastery of the forums needs work.

    Actually, provided my assumptions about operator precedence in PHP are solid, positive numbers will be returned with no alteration, and negative numbers will be tripled (by having twice its value subtracted from it). The function would actually work if the condition was changed to "!is_negative($num)". Actually, scratch that, it suggests that it turns all numbers negative. I don't know about PHP's ternary operator behaviour, but maybe "$num < 0 ? $num : $num * -1" would work better.

    Still how hard is it to use "value * (-1)"?



  • @zzo38 said:

    <FONT face="Lucida Console" size="2">...
    return $num - $num * 2;
    ...</FONT>

    Now, TRWTF(tm) is that they should've optimized their math:

    <FONT face="Lucida Console" size="2">...
           return $num - ( $num << 1 );
    ...</FONT>


  • Is this PHP? Because I've seen something similar:

       return "-$num";

    I replaced it with:

       return $num/-1;

    ...and the original author complained that this wouldn't work with decimals. I never really understood his argument, but he left about a week later so we got to keep it my way.



  • @drinkingbird said:

    Still how hard is it to use "value * (-1)"?


    Not sure if that's what you were saying, but that would flip the sign bit, not "turn it negative". I.e. it might turn a number positive, contrary to the function name.

    return - abs(value);

    as the complete body of the function would ensure negativity.



  • @CDarklock said:

       return "-$num";

    I replaced it with:

       return $num/-1;

    Wait... WHAT? What's wrong with plain simple -$num? Why do you want to divide anything if you're just changing sign? 



  • @viraptor said:

    @CDarklock said:

       return "-$num";

    I replaced it with:

       return $num/-1;

    Wait... WHAT? What's wrong with plain simple -$num? Why do you want to divide anything if you're just changing sign? 

     

    I'm a php n00b, but what would turn_neg(-4) return in that case?  "4", or "--4"? 



  • TRWTF is that you read the user contributed comments in the PHP manual.  Seriously, you could have dozens of sidebar posts a day from just a few pages.

     

    function turn_neg($n) {

        return ($n < 0) ? $n : -$n;

    }

     

    That's the fastest way to do this as a function in PHP (it saves you from calling abs()), but it really doesn't seem like the kind of thing you would need a function for.  Seriously, most of the other comments on that page are LOL-worthy.  I love this one:

     function turn_neg($n) {

        return ~abs($n) + 1; 

     }

     

    Not only is it crystal clear, it must be faster because it uses a bitwise NOT!



  • @vt_mruhlin said:

    I'm a php n00b, but what would turn_neg(-4) return in that case?  "4", or "--4"? 

    Yes, it does return "--4".  Unfortunately this kind of garbage isn't uncommon in the PHP world. 



  • Craziest i've seen, in C:

    [code]float makePos( float in )
    {
    return in & 0x7FFFFFFF;
    }
    float makeNeg( float in )
    {
    return in | 0x10000000;
    }
    [/code]



  • probably this entire thread should be posted on the front page...



  • Have your friend explain to me how his solution is any better than

    @even stupider code said:

     

    <font face="Lucida Console" size="2">function is_neg ($num) {
    return $num < 0;
    } // end is_neg

    function turn_neg ($num) {
    if (is_neg ($num)) {
    return 2 * $num - $num * 4;
    } else {
    return abs ($num);
    }
    } // end turn_neg</font>
     


  • int turn_neg(int origNum)
    {
       int negNum = origNum;
       for (int i = 0; i < (origNum * 2); i++)
       {
          if (origNum > 0)
             negNum--;
          else
             negNum++;
       }
       return negNum;
    }

     



  • That's nothin'!  I've got an absolute peach from the world of Actionscript, have a look...

    turn_negative(2); // -2 

    function turn_negative(num:int):int{
    return int(String("-"+num));
    }

    and the possibly even more awesome turn_positive function:

     

    turn_positive(-2); // 2 

    function turn_positive(num:int):int{
    return int(String(num).split("-").join(""));
    }

    The thing is, it's not written that badly :(



  • @morbiuswilters said:

    @vt_mruhlin said:

    I'm a php n00b, but what would turn_neg(-4) return in that case?  "4", or "--4"? 

    Yes, it does return "--4".  Unfortunately this kind of garbage isn't uncommon in the PHP world. 

    Aaand... that's why you should use statically defined data types. ;)



  • @danixdefcon5 said:

    @morbiuswilters said:

    @vt_mruhlin said:

    I'm a php n00b, but what would turn_neg(-4) return in that case?  "4", or "--4"? 

    Yes, it does return "--4".  Unfortunately this kind of garbage isn't uncommon in the PHP world. 

    Aaand... that's why you should use statically defined data types. ;)

     

    Pray tell, how would static typing help this function? 



  • @fourchan said:

    Pray tell, how would static typing help this function? 
     

    Or the programmer who wrote it?



  • @CDarklock said:

    Is this PHP? Because I've seen something similar:

       return "-$num";

    I replaced it with:

       return $num/-1;

    ...and the original author complained that this wouldn't work with decimals. I never really understood his argument, but he left about a week later so we got to keep it my way.

    I really wonder why Mr. "return -$num" was left out, is it some PHP enterprisey thing?


  •  @yirkha said:

    @CDarklock said:

    Is this PHP? Because I've seen something similar:

       return "-$num";

    I replaced it with:

       return $num/-1;

    ...and the original author complained that this wouldn't work with decimals. I never really understood his argument, but he left about a week later so we got to keep it my way.

    I really wonder why Mr. "return -$num" was left out, is it some PHP enterprisey thing?

    If you're just flipping the sign (and not going for a negative abs()), then why would you even use a function for it?  Just do it inline.. 



  • @fourchan said:

    @danixdefcon5 said:

    @morbiuswilters said:

    @vt_mruhlin said:

    I'm a php n00b, but what would turn_neg(-4) return in that case?  "4", or "--4"? 

    Yes, it does return "--4".  Unfortunately this kind of garbage isn't uncommon in the PHP world. 

    Aaand... that's why you should use statically defined data types. ;)

     

    Pray tell, how would static typing help this function? 

    $num set as an integer, -$num would act as a sign-flipper. Though it would still turn already-negative numbers into positive :D


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.