PDA

View Full Version : [php] Using the abs() function ?


Vaupell
01-03-2010, 01:09 PM
Doing some calculations that may wary depending on the user, and
content calculated ofcourse..

But ewentually its bound to happen i will hit values below 0
and using integers as whole numbers no decimals

one calc could be as simple as

$result=$contentA-$ContentB

But i feel like i dont have control over it,
it might end up being 100 - 10 and will result in -90

So ive found the php manual and read about abs();
But got worried reading all the comments below
http://php.net/manual/en/function.abs.php

-----------------
What i want, is if the case is that $result ends up as a negative value i just
want to turn it positive. so in the case above the -90 would be 90.
was thinking doing something like this


$result=$contentA-$ContentB
if ($result <= 0)
{
$result=$abs3($result);
}



Is that far fetched ? or is there a better solution ?

winstone
01-03-2010, 01:47 PM
not sure about abs function but you can also use a simple math to achieve that
$result = ($result*(-2))/2;

if -6, it will become 6
if 0, it will become 0

with abs, apparently 0 turns into 1

Vaupell
01-03-2010, 02:07 PM
Thank you winstone.

Dygear
01-03-2010, 08:05 PM
$result = abs($contentA - $ContentB);

Vaupell
01-03-2010, 09:29 PM
$result = abs($contentA - $ContentB);

Thank you it works, went back to abs
cause the math solution came up with some strange numbers..

Dygear
01-04-2010, 02:07 AM
Thank you it works, went back to abs
cause the math solution came up with some strange numbers..

My old solution before I found out about the abs function was this:

function absolute_sub($i1, $i2) {
# Ensure Absolute Value after Subtraction.
if ($i1 == $i2)
return 0;
else if ($i1 > $i2)
return $i1 - $i2;
else
return $i2 - $i1;
}

Boy, did I feel stupid after I found out about abs.

consolegaming
01-04-2010, 11:02 AM
Wouldn't the non abs method just be multiply by -1 if the value is less than 0?

if ($result <= 0)
{
$result = $result * -1;
}

Wouldn't that be the way to do it if abs wasn't available?

Vaupell
01-04-2010, 12:45 PM
Wouldn't the non abs method just be multiply by -1 if the value is less than 0?

if ($result <= 0)
{
$result = $result * -1;
}

Wouldn't that be the way to do it if abs wasn't available?

muiltiply by 1 does not do anything

-500 * 1 = -500 still..
try it on your calculator in your OS.

consolegaming
01-04-2010, 01:45 PM
muiltiply by 1 does not do anything

-500 * 1 = -500 still..
try it on your calculator in your OS.

Maybe you should re-read what I posted? :)

-500 * -1 = 500. Hence the if statement to make sure the result is less than 1. If result is positive then it wouldn't need to do anything to it.

Though really looking back at it what I put was slightly wrong. It should be if ($result < 0) rather than <= 0 as there's no point multiplying 0 by -1.

Vaupell
01-04-2010, 01:55 PM
right got it :D year that would work..

Dygear
01-04-2010, 04:12 PM
Wouldn't the non abs method just be multiply by -1 if the value is less than 0?

if ($result <= 0)
{
$result = $result * -1;
}

Wouldn't that be the way to do it if abs wasn't available?

That's a really cool trick. I love it! In some of my other programming languages I don't have access to an abs function, I'll be sure to use this.