Re: negative numbers

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

 



I've managed to delete the original post, so apologies in advance if I got
the Subject wrong and/or am missing the threading headers.

Plus, this post isn't even strictly necessary, and could be construed as
pedantic.  Oh well.

To change -40 to 40, http://php.net/abs is probably the best answer from a
code maintenance point of view.

If, however, you need to do this in a very tight loop, and performance is
an issue, and if you are certain that the number *IS* negative, you could
also do:

$x = - $x;

which would be faster than the abs() function.

This performance difference would be moot in *MOST* cases, but could be
important to the original poster, I suppose.

If you want to mimic the behaviour of abs (allowing for positive numbers)
and performance was an issue, that:
$x = ($x < 0) ? - $x : $x;

is most likely faster than abs()

Don't do this just to be "faster" because "faster is better" though.  Code
maintenance and abs() win over "faster" except in really dire straits on
this one.

YMMV. NAIAA. IANAL. TANSTAAFL.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
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