我想要一个函数为负数返回-1,为正数返回+1。 http://en.wikipedia.org/wiki/Sign_function 我自己编写它很容易,但它似乎应该存在于某个标准库中。
编辑:具体来说,我正在寻找一个函数工作在浮动。
我想要一个函数为负数返回-1,为正数返回+1。 http://en.wikipedia.org/wiki/Sign_function 我自己编写它很容易,但它似乎应该存在于某个标准库中。
编辑:具体来说,我正在寻找一个函数工作在浮动。
当前回答
比上述解决方案更快,包括评级最高的一个:
(x < 0) ? -1 : (x > 0)
其他回答
这个问题很老了,但现在有了这种理想函数。我用not, left shift和dec添加了一个包装器。
您可以使用C99中基于signbit的包装器函数来获得确切的期望行为(请参阅下面的代码)。
返回x的符号是否为负。 这也可以应用于无穷大,nan和零(如果零是无符号的,它被认为是正的
#include <math.h>
int signValue(float a) {
return ((!signbit(a)) << 1) - 1;
}
注意:我使用operand not("!")是因为符号位的返回值没有指定为1(即使例子让我们认为它总是这样),但对于负数是真的:
返回值 如果x的符号为负,则为非零值(true);否则为零(false)。
然后我左移乘以2(“<< 1”),这将给我们一个正数2,一个负数0,最后减去1,分别得到1和-1的正数和负数,这是OP要求的。
如果您只想测试符号,则使用signbit(如果参数有负号则返回true)。 不知道为什么你特别想要返回-1或+1;复印件更方便 但在某些平台上,它会在负0时返回+1 仅部分支持负0,其中signbit可能会返回true。
有一个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。
类型安全的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.)
一般来说,在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.