PHP abs note #1
If doing a single conversion to ">= 0" on a random input, in order to workout the value of the input (corrected to 0 or greater), the quickest method is:
$x = abs($input);
After a bit more testing and thinking about how the test page (in the post below) works, it appears the results are correct, but _only_ when looping over the different methods for a lot of loops. For a single test or correction, the results are wrong.
The results for option 1 and 3 are very close to each other, but option 2 is clearly the quickest of the three.
After removing the loops, the results where:
1) Result [938]: 1.5974044799805E-5 sec.
2) Result [938]: 5.9604644775391E-6 sec.
3) Result [938]: 1.5020370483398E-5 sec.
(note the E-5 and E-6 at the end)
PHP abs note #2
In trying to workout the fastest way to return a number equal to or great than 0 from any input, with < 0 turned positive as well, these are my results and how I tested:
Fastest method:
$x = floor($input);
$x = ($x < 0) ? ($x * -1) : $x;
With ?n=987 passed
1) Result [987]: 0.020493030548096 sec.
2) Result [987]: 0.072222948074341 sec.
3) Result [987]: 0.23027300834656 sec.
With ?n=-821
1) Result [821]: 0.023087024688721 sec.
2) Result [821]: 0.073657035827637 sec.
3) Result [821]: 0.24681091308594 sec.
With ?n=foo
1) Result [0]: 0.020076990127563 sec.
2) Result [0]: 0.065827131271362 sec.
3) Result [0]: 0.17472290992737 sec.
Also works with nothing passed.
The test page code:
<?php
$start = microtime(true);
$x = floor($_GET['n']);
$i = 0;
do {
$tmp = ($x < 0) ? ($x * -1) : $x;
} while($i++ < 100000);
$end = microtime(true);
echo '1) Result ['.$tmp.']: '.($end - $start).' sec.<br />';
$start = microtime(true);
$x = $_GET['n'];
$i = 0;
do {
$tmp = abs($x);
} while($i++ < 100000);
$end = microtime(true);
echo '2) Result ['.$tmp.']: '.($end - $start).' sec.<br />';
$start = microtime(true);
$x = floor($_GET['n']);
$i = 0;
do {
$tmp = str_replace('-','',$x);
} while($i++ < 100000);
$end = microtime(true);
echo '3) Result ['.$tmp.']: '.($end - $start).' sec.<br />';
?>
PHP abs note #3
If you don't have/want GMP and are working with large numbers/currencies:
<?php
function mb_abs($number)
{
return str_replace('-','',$number);
}
?>
No need to worry about encoding, as your numbers should all be basic (ANSI) strings.
PHP abs note #4
[EDIT by danbrown AT php DOT net: Merged two posts by the same author.]
This is a note to concordia's comment athttp://us.php.net/manual/en/function.abs.php#58508
I believe there is an easier way to turn a positive number into a negative one and vice versa in one line:
<?php
function change_pol($integer){
return (0 - $integer);
}
?>
It takes another additional line to check if it's really a number:
<?php
function change_pol($integer){
if(!is_numeric($integer)){ return false; }
return (0 - $integer);
}
?>
Keep in mind that if I do 0 - -5 (given that $integer is -5), it will turn into +, thus turning the result into +5. Works like a charm for me. Input like '-5' will work as well (as PHP turns strings automatically into an integer or float), even '-.9' will return as 0.9.
Another thing I'd like to note is that the variable name $integer used in my examples aren't really advised as the function goes beyond simple integers. Initially, I used this function only for the handling of integers, hence the variable name. It is only later that I've discovered it goes beyond integers. 0 will also return to 0 (if that shouldn't be obvious).
Perhaps it is better to make it return NULL instead of FALSE when it fails the is_numeric condition, so that there's no mix with the function's result to be false instead of actually 0, however only checking with '===' instead of '==' should solve this, I believe.
PHP abs note #5
[*EDIT* by danbrown AT php DOT net: Merged user's corrected code with previous post content.]
jeremys indicated one thing - there is no sgn function wich actually seems a bit strange for me. Of course it is as simple as possible, but it is usefull and it is a standard math function needed occasionally.
Well, I have solved this function in a bit different matter:
<?php
function sgn($liczba)
{
if($liczba>0)
$liczba=1;
else if($liczba<0)
$liczba=-1;
else if(!is_numeric($liczba))
$liczba=null;
else
$liczba=0;
return $liczba;
}
?>
The difference is that it returns null when the argument isn't a number at all.
PHP abs note #6
I think there are faster methods for doing an on/off.
$value = (1 - $value);
or better:
$value = (1 ^ $value); // which uses the xor function.
Below are the benchs on my computer, for loop of 10,000,000 with PHP Version 5.2.1
$value = abs($value -1); // 4.12641406059 sec
$value = (1 - $value); // 1.58847904205 sec
$value = (1 ^ $value); // 1.49518704414 sec
Note that using function call for such operation is very slow.
function on_off($value) {
return (1 ^ $value);
} // 7.10739612579 sec
PHP abs note #7
making an on/off switch with 0 and 1
function on_off($value) {
return abs($value -1);
}
if you send 0, it'll return 1, and 1 return 0.
0 - 1 = -1, absolute of -1 = 1
1 - 1 = 0, absolute of 0 = 0
PHP abs note #8
//Not for working with float
//anyways, seems cool for integers
//This works, iff is_int($num)
function nabs ($num=NULL){
$num = (int)$num;
if ($num<=0) return 0; //why bother bit flipping with 0
return ~$num + 1;
}
PHP abs note #9
If we want to into a war about speed with negative absolute numbers, I think bitwise operations should just about do it.
<?php
function n_abs($v) { return ~abs($v) + 1; }
?>
A NOT operation on any number with a sign bit will produce the negative of that number minus one. All we need to do is add one. Voila!
Speed test results (30000 iterations of various methods) produced very close dueling between the previous suggested method. Although I am on an overloaded Windows PC so I'm no benchmark, and I'll leave it up to you, the reader of this message.
PHP abs note #10
Let's say you are resizing images to a standard size that can be expressed as a ratio (width/height). The problem I came into was that I wanted to be reasonable with the proportion of the images that my customer is uploading (couldn't we all use a little less horizontal on pictures?), but I wanted to reject the horizontal pictures when they were uploading vertical ones. So I wanted to accept proportions of images that were within a reasonable threshold (+ or -) of what I will be resizing them to.
Assuming a standard of 1 to 4 (0.25) and a threshold of no more than 0.05 deviation, then the number 0.30 and 0.20 would return true and 0.19 would return false.
<?php
function threshold($given,$thresh,$standard)
{
return (abs($given-$standard)<=$thresh) ? true : false;
}
?>
PHP abs note #11
I'm unable to replicate concordia's problem with the $n = $n - $n * 2 code. I agree with the simplification to $n *= -1. But there's no reason that concordia's code should return 6 for the value -2, and it doesn't appear to. When I tried it, PHP returned 2, as it should. If PHP were somehow flipping the sign of integers randomly, that would be a *major* bug!
There doesn't seem to be a sgn() function yet. Here's some quick code to do it:
function sgn($x) {
return $x ? ($x>0 ? 1 : -1) : 0;
}
You could use $x ? abs($x)/$x : 0 too, but might as well avoid the float division.
PHP abs note #12
Even that is unnecessarily complex. Try this:
<?php
function n_abs($v) { return -abs($v) ; }
?>
Faster too.
PHP abs note #13
concordia, you seem to be overcomplicating matters. If you want to do the reverse of the abs function, the only code required is:
<?php
function n_abs($num) {
return ($num > 0) ? $num * -1 : $num;
}
n_abs(2);
n_abs(-2);
?>
If you want to switch the sign bit of a number, as your example seems to indicate...
turn_neg (2); // -2
turn_neg (-2); // 2
...you just need to do $num *= -1;
Your function also doesn't seem to work. Switching "if (is_neg ($num))" to "if (!is_neg ($num))" would make it function as indicated by your comments (i.e. toggle the sign bit), but it would still be overly complicated.
PHP abs note #14
Both of the below codes were wrong.
<?php
if(!is_numeric($range)) {
$range=1;
}
if(!is_int($range)) {
$range=intval($range);
}
$range=abs($range);
?>
The only thing I don't get is why we have to check before doing the functions. Whether or not we do the functions it will give us what we want. The only check I really understand being there is the one that sets it to one.
Why not just do:
<?php
$range = is_numeric($range) ? abs(intval($range)) : 1;
?>
Seems like a lot less code. Or If you prefer to stay with if statements:
<?php
if(is_numberic($range)) {
$range = abs(intval($range));
}
else {
$range = 1;
}
?>
Both ways seem smaller to me.
PHP abs note #15
There's another problem in the below code. The correct function to see whether a value is an integer is is_int(), not int(), so the code should look like this:
<?php
if(!is_numeric($range))
{
$range=1;
}
if(!is_int($range))
{
$range=int_val($range);
}
$range=abs($range);
?>
PHP abs note #16
In reference to the previous code sample the int_val function is actually intval() [http://us4.php.net/manual/en/function.intval.php]:
<?php
if(!is_numeric($range)){
$range=1;
}
if(!int($range)){
$range=intval($range) }
$range=abs($range);
?>
PHP abs note #17
assume we take user input from a form untreated and assign it to variable $range. We want to be sure this number is a Positive Whole number, since abs() just sets a number to positive or 0, and we dont want decimals...
<?php
if(!is_numeric($range))
{
$range=1;
}
if(!int($range))
{
$range=int_val($range);
}
$range=abs($range);
?>
for example the input "testing" would return $range =1,
the input "3.578" would return value=3
If the input is null, I am havent tested to see if it gets set to 1 courtesy of int_val or not, but I believe it will be.
I'm sure there's probably a more elegant way to do this using regex, however for an apprentice php coder, this might be a little easier to understand and use.