我想要一个函数为负数返回-1,为正数返回+1。 http://en.wikipedia.org/wiki/Sign_function 我自己编写它很容易,但它似乎应该存在于某个标准库中。

编辑:具体来说,我正在寻找一个函数工作在浮动。


当前回答

显然,最初的帖子的问题的答案是否定的。没有标准的c++ sgn函数。

其他回答

一般来说,在C/ c++中没有标准的signum函数,缺少这样一个基本函数说明了很多关于这些语言的信息。

除此之外,我相信关于定义这样一个函数的正确方法的两种主流观点在某种程度上是正确的,而且一旦你考虑到两个重要的警告,关于它的“争议”实际上是没有争议的:

A signum function should always return the type of its operand, similarly to an abs() function, because signum is usually used for multiplication with an absolute value after the latter has been processed somehow. Therefore, the major use case of signum is not comparisons but arithmetic, and the latter shouldn't involve any expensive integer-to/from-floating-point conversions. Floating point types do not feature a single exact zero value: +0.0 can be interpreted as "infinitesimally above zero", and -0.0 as "infinitesimally below zero". That's the reason why comparisons involving zero must internally check against both values, and an expression like x == 0.0 can be dangerous.

对于C语言,我认为使用整型的最佳方法确实是使用(x > 0) - (x < 0)表达式,因为它应该以一种无分支的方式进行转换,并且只需要三个基本操作。最好定义强制返回类型与实参类型匹配的内联函数,并添加C11 define _Generic来将这些函数映射到公共名称。

With floating point values, I think inline functions based on C11 copysignf(1.0f, x), copysign(1.0, x), and copysignl(1.0l, x) are the way to go, simply because they're also highly likely to be branch-free, and additionally do not require casting the result from integer back into a floating point value. You should probably comment prominently that your floating point implementations of signum will not return zero because of the peculiarities of floating point zero values, processing time considerations, and also because it is often very useful in floating point arithmetic to receive the correct -1/+1 sign, even for zero values.

有一个C99数学库函数叫copysign(),它从一个参数取符号,从另一个参数取绝对值:

result = copysign(1.0, value) // double
result = copysignf(1.0, value) // float
result = copysignl(1.0, value) // long double

会给你一个+/- 1.0的结果,这取决于值的符号。注意,浮点零是有符号的:(+0)将产生+1,(-0)将产生-1。

这是一个分支友好的实现:

inline int signum(const double x) {
    if(x == 0) return 0;
    return (1 - (static_cast<int>((*reinterpret_cast<const uint64_t*>(&x)) >> 63) << 1));
}

除非你的数据有一半是0,否则分支预测器会选择一个最常见的分支。两个分支都只涉及简单的操作。

另外,在一些编译器和CPU架构上,完全无分支的版本可能更快:

inline int signum(const double x) {
    return (x != 0) * 
        (1 - (static_cast<int>((*reinterpret_cast<const uint64_t*>(&x)) >> 63) << 1));
}

这适用于IEEE 754双精度二进制浮点格式:binary64。

我不知道它的标准函数。这里有一种有趣的写法:

(x > 0) - (x < 0)

这里有一个更容易理解的方法:

if (x > 0) return 1;
if (x < 0) return -1;
return 0;

如果你喜欢三元运算符,你可以这样做:

(x > 0) ? 1 : ((x < 0) ? -1 : 0)

虽然接受的答案中的整数解决方案相当优雅,但它不能为双类型返回NAN,因此我对它进行了稍微修改。

template <typename T> double sgn(T val) {
    return double((T(0) < val) - (val < T(0)))/(val == val);
}

请注意,返回一个浮点NAN而不是硬编码NAN会导致在某些实现中设置符号位,因此val = -NAN和val = NAN的输出无论如何都是相同的(如果你更喜欢“NAN”输出而不是-NAN,你可以在返回之前放一个abs(val)…)