views:

521

answers:

6

Hay Guys, I have quick question.

I have a few floats:

-4.50
+6.25
-8.00
-1.75

How can I change all these to negative floats so they become:

-4.50
-6.25
-8.00
-1.75

Also i need a way to do the reverse

If the float is a negative, make it a positive.

Thanks

+6  A: 

How about something trivial like:

  • inverting:

    $num = -$num;
    
  • converting only positive into negative:

    if ($num > 0) $num = -$num;
    
  • converting only negative into positive:

    if ($num < 0) $num = -$num;
    
Gumbo
Why did I get a down vote?
Gumbo
+8  A: 

A trivial

$num = $num <= 0 ? $num : -$num ;

or, the better solution, IMHO:

$num = -1 * abs($num)

EDIT:

the explicit multiplication can be avoided for shortness as @VegardLarsen has posted, but I prefer readability over shortness :)

EDIT2:

I suggest to avoid if/else (or equivalent ternary operator) especially if you have to manipulate a number of items (in a loop or using a lambda function), as it will affect performance.

EDIT3:

"If the float is a negative, make it a positive."

In order to change the sign of a number you can simply do:

$num = 0 - $num;

or, multiply it by -1, of course :)

AlberT
Is this not backwards?
Dan Tao
Ops, yes sorry, just reversed the order:)
AlberT
Ha, ok... for a second there I was doubting my own sanity.
Dan Tao
Why should getting the absolute value of a variable, inverting it and reassigning it to that variable no matter what value it is be more performant?
Gumbo
maybe it is not, you are right. It would depend on the number of already OK numbers :)
AlberT
-1 `-1 * (0 - $num)` is equal to `(-1 * 0) - (-1 * $num)` is equal to `1 * $num`.
Gumbo
Your last answer is not correct! If $num is positive then -1 * (0 - $num) will return a positive number.
Dan Tao
I just realized it, my bad sorry, removed ! Thank you
AlberT
I could be wrong, but I'm pretty sure that since you're performing an operation under certain conditions ($num is positive) and not others ($num <= 0), you're always going to be using an if statement somewhere. Does abs($num) not simply perform $num = ($num >= 0) ? $num : -$num; under the hood?
Dan Tao
You are right, but it would be performed at low level, in C. _By PHP, not in PHP_.
AlberT
+15  A: 
$float = -abs($float);
Vegard Larsen
+3  A: 

I think Gumbo's answer is just fine. Some people prefer this fancy expression that does the same thing:

$int = (($int > 0) ? -$int : $int);

EDIT: Apparently you are looking for a function that will make negatives positive as well. I think these answers are the simplest:

/* I am not proposing you actually use functions called
   "makeNegative" and "makePositive"; I am just presenting
   the most direct solution in the form of two clearly named
   functions. */
function makeNegative($num) { return -abs($num); }
function makePositive($num) { return abs($num); }
Dan Tao
why to wrap _abs()_ when used as is? nah.
AlberT
+2  A: 

re the edit: "Also i need a way to do the reverse If the float is a negative, make it a positive"

$number = -$number;

changes the number to its opposite.

SilentGhost
+1  A: 

This was a popular funny question on reddit a couple of weeks back. Some people post ridiculous answers involving for loops and such. I was tempted at post a solution like that here but I think some may not have seen the humor in it and I would have gotten down votted fairly quick.

jdelator