Edit report at https://bugs.php.net/bug.php?id=19621&edit=1
ID: 19621
Comment by: tom at kera dot name
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:
Patrick, adding a new library function _absolutely_ breaks BC.
For example:
<?php
function abs($x) {
return 3;
}
// Output: Fatal error: Cannot redeclare abs()
?>
Do you want every script that declares a function `sign` to suddenly not work
any more? How would this *not* be a compatibility issue?
Previous Comments:
------------------------------------------------------------------------
[2011-04-09 16:51:11] bogdan at moongate dot ro
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()?
------------------------------------------------------------------------
[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] [email protected]
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 https://bugs.php.net/bug.php?id=19621&edit=1