什么是“小于0"?意思是? [英] What does "compares less than 0" mean?

查看:809
本文介绍了什么是“小于0"?意思是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文

我正在阅读持续比较,我注意到动词进行比较的特殊用法:

有一个新的三向比较运算符, a< => b 返回比较< 0 如果 a< b ,比较> 0 如果 a> b ,以及 比较 == 0 如果 a example ,可在GitHub上找到(重点是我):

//执行循环16位比较.
//如果两个数字之间的距离大于32767,
//且数字大于32768,请减去65536
//因此,65535 比较小于0 ,但大于65534
//这样可以正确处理65535-> 0环绕情况

当然,对于经验丰富的程序员来说,含义很清楚.但是,在这些示例中使用动词进行比较的方式在任何标准化的英语形式中都不是标准的.

问题 *

  • 编程行话句子对象比较小于零"如何转换为普通英语?
  • 这是否意味着如果将对象与0进行比较,结果将是小于零"?
  • 为什么说对象小于小于零"而不是对象比较小于零"会出错?

*我在">用法.

解决方案

是的,对象比较小于0"表示object < 0将产生true.同样,compares equal to 0表示object == 0将产生true,compares greater than 0表示object > 0将产生true.

关于他为什么不使用短语小于0"的原因,我想是为了强调这是保证的.例如,从本质上讲,它可以是任意类型,包括一个并不真正代表实际值,而仅支持与0进行比较的类型.

例如,让我们考虑一个类似这样的类型:

class comparison_result {
    enum { LT, GT, EQ } res; 

    friend template <class Integer>
    bool operator<(comparison_result c, Integer) { return c.res == LT; }

    friend template <class Integer>
    bool operator<(Integer, comparison_result c) { return c.res == GT; }

    // and similarly for `>` and `==`
};

[目前,让我们假设friend template<...>的东西都是合法的-无论如何,我认为您已经掌握了基本思想).

这根本不代表任何值.它只是表示如果与0比较,则结果应小于,等于或大于"的结果.因此,并不是 小于0,而是与0相比,它会产生truefalse(而与另一个值相比,它会产生相同的结果).

关于<0是否为true表示>0==0必须为false(反之亦然):对操作符本身的返回类型没有这种限制.该语言甚至不包含指定或强制执行此类要求的方法.规范中没有阻止它们全部返回true的内容.对于所有比较,返回true是可能的,并且似乎允许这样做,但这可能牵强.

为它们全部返回false是完全合理的-例如,与浮点NaN进行的所有比较通常应返回false. NaN的意思是不是数字",并且不是数字的东西不少于大于数字.两者是无可比拟的,因此在每种情况下,答案都是(很正确的)错误的.

Context

While I was reading Consistent comparison, I have noticed a peculiar usage of the verb to compare:

There’s a new three-way comparison operator, <=>. The expression a <=> b returns an object that compares <0 if a < b, compares >0 if a > b, and compares ==0 if a and b are equal/equivalent.

Another example found on the internet (emphasis mine):

It returns a value that compares less than zero on failure. Otherwise, the returned value can be used as the first argument on a later call to get.

One last example, found in a on GitHub (emphasis mine):

// Perform a circular 16 bit compare.
// If the distance between the two numbers is larger than 32767,
// and the numbers are larger than 32768, subtract 65536
// Thus, 65535 compares less than 0, but greater than 65534
// This handles the 65535->0 wrap around case correctly

Of course, for experienced programmers the meaning is clear. But the way the verb to compare is used in these examples is not standard in any standardized forms of English.

Questions*

  • How does the programming jargon sentence "The object compares less than zero" translate into plain English?
  • Does it mean that if the object is compared with0 the result will be "less than zero"?
  • Why would be wrong to say "object is less than zero" instead of "object compares less than zero"?

* I asked for help on English Language Learners and English Language & Usage.

解决方案

Yes, an "object compares less than 0" means that object < 0 will yield true. Likewise, compares equal to 0 means object == 0 will yield true, and compares greater than 0 means object > 0 will yield true.

As to why he doesn't use the phrase "is less than 0", I'd guess it's to emphasize that this is all that's guaranteed. For example, this could be essentially any arbitrary type, including one that doesn't really represent an actual value, but instead only supports comparison with 0.

Just, for example, let's consider a type something like this:

class comparison_result {
    enum { LT, GT, EQ } res; 

    friend template <class Integer>
    bool operator<(comparison_result c, Integer) { return c.res == LT; }

    friend template <class Integer>
    bool operator<(Integer, comparison_result c) { return c.res == GT; }

    // and similarly for `>` and `==`
};

[For the moment, let's assume the friend template<...> stuff is all legit--I think you get the basic idea, anyway).

This doesn't really represent a value at all. It just represents the result of "if compared to 0, should the result be less than, equal to, or greater than". As such, it's not that it is less than 0, only that it produces true or false when compared to 0 (but produces the same results when compared to another value).

As to whether <0 being true means that >0 and ==0 must be false (and vice versa): there is no such restriction on the return type for the operator itself. The language doesn't even include a way to specify or enforce such a requirement. There's nothing in the spec to prevent them from all returning true. Returning true for all the comparisons is possible and seems to be allowed, but it's probably pretty far-fetched.

Returning false for all of them is entirely reasonable though--just, for example, any and all comparisons with floating point NaNs should normally return false. NaN means "Not a Number", and something that's not a number isn't less than, equal to or greater than a number. The two are incomparable, so in every case, the answer is (quite rightly) false.

这篇关于什么是“小于0"?意思是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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