php 如何获得一个数字的符号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7556574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get sign of a number?
提问by hakre
Is there a (simple) way to get the "sign" of a number (integer) in PHP comparable to gmp_sign
Docs:
是否有一种(简单的)方法可以在 PHP 中获取与gmp_sign
Docs相当的数字(整数)的“符号” :
- -1 negative
- 0 zero
- 1 positive
- -1 负
- 0 零
- 1 阳性
I remember there is some sort of compare function that can do this but I'm not able to find it at the moment.
我记得有某种比较功能可以做到这一点,但我目前无法找到它。
I quickly compiled this (Demo) which does the job, but maybe there is something more nifty (like a single function call?), I would like to map the result onto an array:
我很快编译了这个(Demo)来完成这项工作,但也许还有更漂亮的东西(比如单个函数调用?),我想将结果映射到一个数组上:
$numbers = array(-100, 0, 100);
foreach($numbers as $number)
{
echo $number, ': ', $number ? abs($number) / $number : 0, "\n";
}
(this code might run into floating point precision problems probably)
(此代码可能会遇到浮点精度问题)
回答by Milosz
Here's a cool one-liner that will do it for you efficiently and reliably:
这是一个很酷的单线,可以高效可靠地为您完成:
function sign($n) {
return ($n > 0) - ($n < 0);
}
回答by kdojeteri
In PHP 7 you should use the combined comparison operator(<=>
):
在 PHP 7 中,您应该使用组合比较运算符( <=>
):
$sign = $i <=> 0;
回答by hakre
A variant to the above in my question I tested and which works as well and has not the floating point problem:
在我测试的问题中,上述问题的一个变体也可以正常工作并且没有浮点问题:
min(1, max(-1, $number))
Edit:The code above has a flaw for float numbers (question was about integer numbers) in the range greater than -1
and smaller than 1
which can be fixed with the following shorty:
编辑:上面的代码对于大于-1
和小于范围内的浮点数(问题是关于整数)有一个缺陷1
,可以用以下短文修复:
min(1, max(-1, $number == 0 ? 0 : $number * INF))
That one still has a flaw for the float NAN
making it return -1
always. That might not be correct. Instead one might want to return 0
as well:
那个仍然有一个浮动的缺陷,NAN
使它-1
总是返回。那可能不正确。相反,人们可能也想返回0
:
min(1, max(-1, (is_nan($number) or $number == 0) ? 0 : $number * INF))
回答by rocksportrocker
You can nest ternary operators:
您可以嵌套三元运算符:
echo $number, ': ', ($number >= 0 ? ($number == 0 ? 0 : 1) : -1 )
This has no problem with floating point precision and avoids an floating point division.
这对浮点精度没有问题,并避免了浮点除法。
回答by zzzzBov
What's wrong with this form?
这个表格有什么问题?
if ( $num < 0 )
{
//negative
}
else if ( $num == 0 )
{
//zero
}
else
{
//positive
}
or ternary:
或三元:
$sign = $num < 0 ? -1 : ( $num > 0 ? 1 : 0 );
Not sure of the performance of abs
vs value comparison, but you could use:
不确定abs
vs 值比较的性能,但您可以使用:
$sign = $num ? $num / abs($num) : 0;
and you could turn any of them into a function:
你可以把它们中的任何一个变成一个函数:
function valueSign($num)
{
return $sign = $num < 0 ? -1 : ( $num > 0 ? 1 : 0 );
//or
return $sign = $num ? $num / abs($num) : 0;
}
I suppose you could be talking about gmp_cmp
, which you could call as gmp_cmp( $num, 0 );
我想你可能在谈论gmp_cmp
,你可以称之为gmp_cmp( $num, 0 );
回答by Rolf
I think that gmp_sign is not very efficient because it expects a GMP or string. ($n ? abs($n)/$n : 0) is mathematically correct, but the division costs time. The min/max solutions seem to get unnecessary complex for floats.
我认为 gmp_sign 不是很有效,因为它需要一个 GMP 或字符串。($n ? abs($n)/$n : 0) 在数学上是正确的,但除法需要时间。最小/最大解决方案似乎对浮动变得不必要的复杂。
($n > 0) - ($n < 0) always does 2 tests and one subtraction ($n < 0 ? -1 : ($n > 0 ? 1 : 0) does one or two tests and no arithmetic, it should be most efficient. But I don't believe that the difference will be relevant for most use cases.
($n > 0) - ($n < 0) 总是做 2 个测试和一个减法 ($n < 0 ? -1 : ($n > 0 ? 1 : 0) 做一两个测试而不是算术,它应该是最有效的。但我认为这种差异与大多数用例无关。
回答by sansig
I know this is late but what about simply dividing the number by the abs() of itself?
我知道这已经晚了,但是简单地将数字除以自身的 abs() 怎么样?
Something like:
就像是:
function sign($n) {
return $n/(abs($n));
}
Put whatever error handling you want for div by zero.
将 div 所需的任何错误处理归零。
回答by Toto
回答by Yeroon
Here's one without loop:
这是一个没有循环的:
function sign($number){
echo $number, ': ', $number ? abs($number) / $number : 0, "\n";
}
$numbers = array(-100, 0, 100);
array_walk($numbers, 'sign');