note 58653 deleted from function.abs by hholzgra
| From: | hholzgra@php.net | Date: | Sun, 15 Apr 2012 19:27:36 +0000 |
| Subject: | note 58653 deleted from function.abs by hholzgra | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-187143@lists.php.net to get a copy of this message | ||
Note Submitter: rdk
----
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); //-2
n_abs(-2); //-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.