RE: Round

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I believe there is some confusion on what ABS actually does.  Ignoring all
the rounding that you are trying to do ABS is a very simple function.

ABS definition: Returns the absolute value of number.

What that means is.

Abs(1) = 1
Abs(2) = 2
Abs(3) = 3
Abs(0) = 0
Abs(-1) = 1
Abs(-2) = 2
Abs(-3) = 3

Simply put, returns the positive value of the number given.  Thus if you put
it on a negative number it will always return positive.

This most likely doesn't help what your trying to do, but I wanted to
clarify this as you keep stating you are expecting a -1 when you use the ABS
function.  Unless someone has some trick that I don't know about, ABS will
NEVER return a negative number.


-----Original Message-----
From: Jim Lucas [mailto:lists@xxxxxxxxx] 
Sent: Wednesday, August 29, 2007 1:43 PM
To: Daniel Brown
Cc: Zoltán Németh; php-general@xxxxxxxxxxxxx
Subject: Re:  Round

Daniel Brown wrote:
> On 8/29/07, Zoltán Németh <znemeth@xxxxxxxxxxxxxx> wrote:
>>> Think this through before you respond...
>>>
>>> Try this
>>>
>>> <?php
>>> var_dump( round(-0.26) );
>>> var_dump( abs( round(-0.26) ) );
>>> var_dump( round(-1.26) );
>>> var_dump( abs( round(-1.26) ) );
>>> ?>
>>>
>>> does this give you the desired results?
>>>
>>> What if I expected -1 for the last answer?
> 
>     It didn't take much thinking this time.... if you were expecting
> -1 for the last answer, you'd be wrong.  ;-P
> 
>     The very nature of abs() is to return an absolute number, which is
> never a negative.
> 

Exactly my point, abs() is not the answer

if he had any negative number that did not round to zero, say it would round
to -2, then having the 
abs() in the calculations would return 2 instead of -2, which would be
wrong.

 From what I read from the OP, I don't think this is what he was looking
for.

the op was asking why he got -0 instead of 0.  not for a solution to fix it.

ok, better example.

<plaintext><?php

$list[] =  1;		# I expect to get  1 and I get 1
$list[] = -1;		# I expect to get -1 but I get 1
$list[] = -1.2;		# I expect to get -1 but I get 1
$list[] = -0.23;	# I expect to get  0 and I get 0
$list[] = -0.75;	# I expect to get -1 and I get 1

foreach ($list AS $value) {
	var_dump( abs( round($value) ) );
}

?>

But, from what the OP says, he would get -0 instead of 0 for the 4th entry.
Am I correct with this?

if so, you could try casting it as an int like so

var_dump( (int)round(-0.26) );

That might fix the problem

-- 
Jim Lucas

    "Some men are born to greatness, some achieve greatness,
        and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
     by William Shakespeare

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux