php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #19621 Math needs a "sign()" function
Submitted: 2002-09-26 13:52 UTC Modified: 2013-04-04 15:32 UTC
From: bill at softky dot com Assigned: johannes (profile)
Status: Closed Package: *General Issues
PHP Version: 4.2.0 OS: Mandrake linux
Private report: No CVE-ID: None
 [2002-09-26 13:52 UTC] 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!

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-10-03 02:32 UTC] 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.

 [2011-02-25 10:14 UTC] 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".
 [2011-04-09 16:51 UTC] 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-09-26 14:30 UTC] 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?
 [2013-04-04 15:22 UTC] 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']);
});
?>
 [2013-04-04 15:32 UTC] johannes@php.net
For usort() this is not needed. function($a,$b) { return $a['age'] - $b['age']; } is fine.
 [2013-04-04 15:32 UTC] johannes@php.net
-Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: johannes
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sat Sep 26 04:01:25 2020 UTC