我正在阅读持续比较,我注意到动词进行比较的特殊用法:
在互联网上发现的另一个示例(重点是我的):
最后一个 example ,可在GitHub上找到(重点是我):
当然,对于经验丰富的程序员来说,含义很清楚.但是,在这些示例中使用动词进行比较的方式在任何标准化的英语形式中都不是标准的.
问题- 编程行话句子对象比较小于零"如何转换为普通英语?
- 这是否意味着如果将对象与
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相比,它会产生true
或false
(而与另一个值相比,它会产生相同的结果).
关于<0
是否为true表示>0
和==0
必须为false(反之亦然):对操作符本身的返回类型没有这种限制.该语言甚至不包含指定或强制执行此类要求的方法.规范中没有阻止它们全部返回true
的内容.对于所有比较,返回true
是可能的,并且似乎允许这样做,但这可能牵强.
为它们全部返回false
是完全合理的-例如,与浮点NaN进行的所有比较通常应返回false
. NaN的意思是不是数字",并且不是数字的东西不少于或大于数字.两者是无可比拟的,因此在每种情况下,答案都是(很正确的)错误的.
While I was reading Consistent comparison, I have noticed a peculiar usage of the verb to compare:
Another example found on the internet (emphasis mine):
One last example, found in a on GitHub (emphasis mine):
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 with
0
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"?
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"?意思是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!