Req #19621 [Csd]: Math needs a "sign()" function
Edit report at https://bugs.php.net/bug.php?id=19621&edit=1
ID: 19621
Updated by: johannes@php.net
Reported by: bill at softky dot com
Summary: Math needs a "sign()" function
Status: Closed
Type: Feature/Change Request
-Package: Feature/Change Request
+Package: *General Issues
Operating System: Mandrake linux
PHP Version: 4.2.0
-Assigned To:
+Assigned To: johannes
Block user comment: N
Private report: N
New Comment:
For usort() this is not needed. function($a,$b) { return $a['age'] - $b['age'];
} is fine.
Previous Comments:
------------------------------------------------------------------------
[2013-04-04 15:22:54] php at yopmail dot com
sign is very usefull in usort function
exemple :
<?php
$people = [
["name"=>"John","age"=>20],
["name"=>"Jack","age"=>30],
["name"=>"Paul","age"=>25],
]
usort($people,function($a, $b){
return sign($a['age'] - $b['age']);
});
?>
------------------------------------------------------------------------
[2011-09-26 14:30:22] tom at kera dot name
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?
------------------------------------------------------------------------
[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] 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.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=19621
--
Edit this bug report at https://bugs.php.net/bug.php?id=19621&edit=1
Thread (7 messages)