有没有在C / C ++标准的符号函数(Signum的,SGN)? [英] Is there a standard sign function (signum, sgn) in C/C++?

查看:800
本文介绍了有没有在C / C ++标准的符号函数(Signum的,SGN)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,对于正数返回-1为负数和+1功能。
http://en.wikipedia.org/wiki/Sign%5Ffunction
这是很容易写我自己,但似乎喜欢的事,应该是在一个标准库的某个地方。

I want a function that returns -1 for negative numbers and +1 for positive numbers. http://en.wikipedia.org/wiki/Sign%5Ffunction It's easy enough to write my own, but it seems like something that ought to be in a standard library somewhere.

编辑:具体来说,我一直在寻找的彩车功能工作

Specifically, I was looking for a function working on floats.

推荐答案

惊讶没有人张贴的网点,类型安全的C ++版本尚未:

Surprised no one has posted the branchless, type-safe C++ version yet:

template <typename T> int sgn(T val) {
    return (T(0) < val) - (val < T(0));
}

好处:


  • 实际上实现正负号(-1,0或1)。实施方式这里使用复制符号仅返回-1或1,这是不正负号。此外,一些实现此处返回浮动(或T),而不是int,这似乎是一种浪费。

  • 作品为整数,浮点数,双打,unsigned short或​​任何自定义类型从整数0和订购的构造的。

  • 快! 复制符号很慢,特别是如果你需要推广,然后再狭窄。这是网点和优化出色

  • 符合标准的!该bitshift黑客整齐,但只适用于一些有点重presentations,当你有一个无符号类型不起作用。它可以被提供为手动专业化时为宜。

  • 准确!简单比较零可以保持机器内部的高precision重新presentation(例如80位上的x87),并避免premature轮零。

  • 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.

注意事项:


  • 这是一个模板,所以它会采取永远编译。

  • 显然,有些人认为使用新的,玄之又玄,而且速度很慢的标准库函数的甚至不真正落实正负号的是更容易理解。

  • &LT;实例化一个无符号类型时,校验0 部分触发GCC的 -Wtype禁区警告。您可以通过使用某些重载避免这种情况:

  • It's a template so it'll take forever to compile.
  • 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的,SGN)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆