note 59648 added to function.abs
| From: | jeremysathangdashwiredotcom at osu1 dot php dot net | Date: | Mon, 12 Dec 2005 16:05:08 +0000 |
| Subject: | note 59648 added to function.abs | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-99996@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/function.abs.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+59648
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59648&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59648&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59648&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59648&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59648&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59648&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59648&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+59648&report=yes
Search -- http://master.php.net/manage/user-notes.php