我想要一个函数为负数返回-1,为正数返回+1。 http://en.wikipedia.org/wiki/Sign_function 我自己编写它很容易,但它似乎应该存在于某个标准库中。
编辑:具体来说,我正在寻找一个函数工作在浮动。
我想要一个函数为负数返回-1,为正数返回+1。 http://en.wikipedia.org/wiki/Sign_function 我自己编写它很容易,但它似乎应该存在于某个标准库中。
编辑:具体来说,我正在寻找一个函数工作在浮动。
当前回答
不,它在c++中不存在,就像在matlab中一样。我在程序中使用宏来实现这一点。
#define sign(a) ( ( (a) < 0 ) ? -1 : ( (a) > 0 ) )
其他回答
有一种不用分支的方法,但不太好。
sign = -(int)((unsigned int)((int)v) >> (sizeof(int) * CHAR_BIT - 1));
http://graphics.stanford.edu/~seander/bithacks.html
那一页上还有很多其他有趣的、过于聪明的东西……
在C/ c++中是否存在标准符号函数(signum, sgn) ?
是的,这取决于定义。
C99及以后版本在<math.h>中有signbit()宏
Int符号位(实浮点x); signbit宏当且仅当其参数值的符号为负时返回一个非零值。C11§7.12.3.6
然而OP想要一些不同的东西。
我想要一个函数,返回-1的负数和+1的正数. ...处理浮点数的函数。
#define signbit_p1_or_n1(x) ((signbit(x) ? -1 : 1)
更深层次的问题:
OP的问题在以下情况下不是特定的:x = 0.0, -0.0, +NaN, -NaN。
经典的signum()在x>0时返回+1,在x<0时返回-1,在x==0时返回0。
许多答案已经涵盖了这一点,但没有涉及x = -0.0, +NaN, -NaN。许多都是针对通常缺少Not-a-Numbers (NaN)和-0.0的整数观点。
典型答案函数如signnum_typical()在-0.0,+NaN, -NaN上,它们返回0.0,0.0,0.0。
int signnum_typical(double x) {
if (x > 0.0) return 1;
if (x < 0.0) return -1;
return 0;
}
相反,我建议这样的功能:在-0.0,+NaN, -NaN上,它返回-0.0,+NaN, -NaN。
double signnum_c(double x) {
if (x > 0.0) return 1.0;
if (x < 0.0) return -1.0;
return x;
}
如果您只想测试符号,则使用signbit(如果参数有负号则返回true)。 不知道为什么你特别想要返回-1或+1;复印件更方便 但在某些平台上,它会在负0时返回+1 仅部分支持负0,其中signbit可能会返回true。
类型安全的c++版本:
template <typename T> int sgn(T val) {
return (T(0) < val) - (val < T(0));
}
好处:
Actually implements signum (-1, 0, or 1). Implementations here using copysign only return -1 or 1, which is not signum. Also, some implementations here are returning a float (or T) rather than an int, which seems wasteful. Works for ints, floats, doubles, unsigned shorts, or any custom types constructible from integer 0 and orderable. Fast! copysign is slow, especially if you need to promote and then narrow again. This is branchless and optimizes excellently Standards-compliant! The bitshift hack is neat, but only works for some bit representations, and doesn't work when you have an unsigned type. It could be provided as a manual specialization when appropriate. Accurate! Simple comparisons with zero can maintain the machine's internal high-precision representation (e.g. 80 bit on x87), and avoid a premature round to zero.
警告:
It's a template so it might take longer to compile in some circumstances. Apparently some people think use of a new, somewhat esoteric, and very slow standard library function that doesn't even really implement signum is more understandable. The < 0 part of the check triggers GCC's -Wtype-limits warning when instantiated for an unsigned type. You can avoid this by using some overloads: template <typename T> inline constexpr int signum(T x, std::false_type is_signed) { return T(0) < x; } template <typename T> inline constexpr int signum(T x, std::true_type is_signed) { return (T(0) < x) - (x < T(0)); } template <typename T> inline constexpr int signum(T x) { return signum(x, std::is_signed<T>()); } (Which is a good example of the first caveat.)
有点跑题了,但我用了这个:
template<typename T>
constexpr int sgn(const T &a, const T &b) noexcept{
return (a > b) - (a < b);
}
template<typename T>
constexpr int sgn(const T &a) noexcept{
return sgn(a, T(0));
}
我发现第一个函数-有两个参数的函数,比“标准”sgn()更有用,因为它最常在这样的代码中使用:
int comp(unsigned a, unsigned b){
return sgn( int(a) - int(b) );
}
vs.
int comp(unsigned a, unsigned b){
return sgn(a, b);
}
无符号类型没有强制转换,也没有额外的减号。
这段代码是用sgn()写的
template <class T>
int comp(const T &a, const T &b){
log__("all");
if (a < b)
return -1;
if (a > b)
return +1;
return 0;
}
inline int comp(int const a, int const b){
log__("int");
return a - b;
}
inline int comp(long int const a, long int const b){
log__("long");
return sgn(a, b);
}