Req #19621 [Com]: Math needs a "sign()" function
| From: | bogdan at moongate dot ro | Date: | Sat, 09 Apr 2011 14:51:12 +0000 |
| Subject: | Req #19621 [Com]: Math needs a "sign()" function | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-160071@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=19621&edit=1
ID: 19621
Comment by: bogdan at moongate dot ro
Reported by: bill at softky dot com
Summary: Math needs a "sign()" function
Status: Closed
Type: Feature/Change Request
Package: Feature/Change Request
Operating System: Mandrake linux
PHP Version: 4.2.0
Block user comment: N
Private report: N
New Comment:
Seconded. I've always rolled my own in PHP, but this is typically used
in small loops executed a lot, so moving this logic into the PHP core
would probably make a difference. If you're concerned sign() might
already be used by various existing scripts, why not implement is as the
more math-like sgn()?
Previous Comments:
------------------------------------------------------------------------
[2011-02-25 10:14:51] patrick at ibuildings dot nl
I also searched for a sign() function and ended up here. But I disagree
with Andrey's arguments not to include it into PHP.
Since when is it a policy to *not* include a function, simply because it
does not exist in a number of other languages? Doesn't PHP have its own
'vision'? Secondly, a new function should not break BC.
As Bill stated, it would complement the abs() function and make the Math
list a more complete list, even though it's a simple function. It seems
a better idea to me to add "sign()" then let's say "goto".
------------------------------------------------------------------------
[2002-10-03 02:32:28] andrey@php.net
Quick search showed that there is no well know scripting language that
has such function. C++/C# has this but they are not scripting
languages.
The following code does the same. Also we have to think about
BC(backward compatibility) with older scripts.
function sign($x){
return (int)((abs($x)-$x)? -1:$x>0);
}
Thank you for you suggestion.
------------------------------------------------------------------------
[2002-09-26 13:52:07] bill at softky dot com
The wonderful math-function list is missing a very important and simple
function: sign(). The is the comlpementof abs(), and together with
abs() allows you to separate the magnitude and sign of a number, e.g.
for graphing purposes (it's hard to graph a negative number of pixels,
and displaying money as "$-99" looks dumb).
It's a one-line function, so I've already written my own, but it really
ought to be built-in.
Thanks!
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=19621&edit=1