What does "compares less than 0" mean?
Asked Answered
B

6

6

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.

Basutoland answered 26/11, 2017 at 16:38 Comment(12)
Unfamiliar grammar and shorthand notation can be confusing for non-native speaker. But this frying your brain may be a bit exaggerated... and downvote attracting.Monamonachal
@NicolBolas “Compares less than zero” is absolutely not ‘plain English’. This sense of compare usually requires a semantically polyvalent construction (expressed either through a plural subject, a plural DO, or a singular DO followed by a mandatory PC). It may be used in this monovalent way jargonically amongst (some?) programmers, but that does not make it plain English. “X compares less than zero” will not make any sense to most regular English-speakers, any more than “My new car compares 55” would.Drawers
@q-l-p: "No downvotes there!" Of course not. Those sites are meant to examine the use of language. This site is meant to be used by programmers. And "value compares <relation>" is used many times in the C++ standard, which leads me to believe that it is a perfectly cromulent phrase.Seth
@JanusBahsJacquet: The "without object" definitions for "compares" seems to contradict you. "His recital certainly compares with the one he gave last year." That's the form of use being employed here.Seth
@NicolBolas No, it is not. That is the perfectly normal, polyvalent use (albeit, granted, in a structure I neglected to specify in my first comment: singular subject with mandatory PC). There are two semantic verbal complements, of which one is expressed as a PP. The case quoted here is, to begin with, not a “without object” use: there is an object, namely less than. There is a singular subject and a singular DO—and nothing more. That is not a normal structure for compare. A parallel version of your example would be “His recital certainly compares the one he gave last year”.Drawers
@JanusBahsJacquet: The "parallel version" would really be "His recital certainly compares negatively with one he gave last year." The "less than" part explains how it compares.Seth
@Nicol Still no. First off, that would be a highly unusual use of less than. And more importantly, you normally compare X to/with Y, regardless of whether there are any adverbials (like negatively or, if we accept it, less than). In the recital example, you’ve reinstated the with which is not there in the C++ example—that’s what makes it so odd. “His recital compares negatively the one he gave last year” still doesn’t make sense. And “an object that compares less than with zero if a < b” also doesn’t make sense. They are completely different and incomparable constructions.Drawers
@JanusBahsJacquet: "And more importantly, you normally compare X to/with Y, regardless of whether there are any adverbials (like negatively or, if we accept it, less than)." I would say that "less than" works as both the adverbial and the "to/with". The phrase "less than" already presupposes the existence of a following object; there's no need to add "to/with" to it. While you might "normally" need "to/with", in this case you do not.Seth
@Nicol That is precisely what makes it jargon: such a presupposition cannot be extrapolated from a phrase like less than in standard English. The usage may well be established C++ jargon—but it is jargon, not standard/plain English. If I understand your answer correctly, a more standard-English rewording would be something along the lines of “an object which, if compared to 0, returns/evaluates to a value less than zero if a < b”, correct?Drawers
Let us continue this discussion in chat.Seth
You should remove irrelevant parts of the question. If your concern was whether "object compares less than 0" means that result of evaluating object < 0 would be true then the answer would be yes. And the question in this form is rather pointless.Columba
@VTT The question is about the meaning of the expression "compares less than zero" (which is often written "compares <0"). I gave examples of how this expression is used. What parts of the question do you think are irrelevant?Basutoland
S
4

What I am interested in, more exactly, is an equivalent expression of "compares <0". Does "compares <0" mean "evaluates to a negative number"?

First, we need to understand the difference between what you quoted and actual wording for the standard. What you quoted was just an explanation for what would actually get put into the standard.

The standard wording in P0515 for the language feature operator<=> is that it returns one of 5 possible types. Those types are defined by the library wording in P0768.

Those types are not integers. Or even enumerations. They are class types. Which means they have exactly and only the operations that the library defines for them. And the library wording is very specific about them:

The comparison category types’ relational and equality friend functions are specified with an anonymous parameter of unspecified type. This type shall be selected by the implementation such that these parameters can accept literal 0 as a corresponding argument. [Example: nullptr_t satisfies this requirement. — end example] In this context, the behaviour of a program that supplies an argument other than a literal 0 is undefined.

Therefore, Herb's text is translated directly into standard wording: it compares less than 0. No more, no less. Not "is a negative number"; it's a value type where the only thing you can do with it is comparing it to zero.

It's important to note how Herb's descriptive text "compares less than 0" translates to the actual standard text. The standard text in P0515 makes it clear that the result of 1 <=> 2 is strong_order::less. And the standard text in P0768 tells us that strong_order::less < 0 is true.

But it also tells us that all other comparisons are the functional equivalent of the descriptive phrase "compares less than 0".

For example, if -1 "compares less than 0", then that would also imply that it does not compare equal to zero. And that it does not compare greater than 0. It also implies that 0 does not compare less than -1. And so on.

P0768 tells us that the relationship between strong_order::less and the literal 0 fits all of the implications of the words "compares less than 0".

Seth answered 26/11, 2017 at 19:41 Comment(13)
I don't understand the sentence, "Which means they have exactly and only the operations that the library defines for them."Basutoland
@q-l-p: ... I don't know how to be more clear. Class types don't have comparison operators defined by default; they have to be defined for them. Since these types are defined by the standard library, then the standard library would be the one defining them.Seth
Oh, wait, I get it now. The sentence means "Which means they have only the operations defined by the library", right?Basutoland
I ask what does "compares less than 0" mean. And you say compares less than zero means "it compares less than 0. No more, no less." Can you give an analogy of what happens? For example Ana and Bob return from a shop... with a toaster (or something) that.............does what?Basutoland
@q-l-p: What does it mean to say that an integer compares less than zero? Your thought might be that it is negative, but you're not thinking fundamentally enough. "Object compares less than zero" means object < 0 == true. But it also means object > 0 == false. And (object == 0) == false.Seth
Ah! You're using "compare" in an unaccusative sense, parallel with "compares unfavourably" ! No wonder neither I nor @Basutoland nor Janus Bahs Jacquet understood. The problem is that in ordinary English "less than zero" is not something that could occur as an adverbial complement like that. I agree that this is a useful expression, but I do not agree that it is plain English. It is jargon.Phaedra
@ColinFine Can you translate what he said in plain English in such a way that he would approve? I will then accept his answer here, and your answer on the English language site. This is interesting stuff!!Basutoland
@q-l-p: I would say "when compared with 0, is reported as negative".Phaedra
@ColinFine: That would suggest that the object is in some way "negative". Which it is not.Seth
@ColinFine You are using... normal logic. Nothing gets compared here. In C++ when X compares <0, that's like saying "Its true that (x<0)"Basutoland
@ColinFine Most jargon has some root in plain English. The overall grammatical structure is plain English. The jargon part is the choice of adverbial as you says, and the special implication of the verb "compare" referring to the result of a particular computation.Monamonachal
@ColinFine Actually, "is negative" is a jargon in math and only applies to certain mathematical objects (e.g. numbers) with certain properties. We cannot call it negative here because that computer object we are talking about is not analogous to those math concepts.Monamonachal
I have edited my question to make it, hopefully, clearer. Would you please address the additional questions in your answer?Basutoland
V
6

"compares <0" in plain English is "compares less than zero".

This is a common shorthand, I believe.

So to apply this onto the entire sentence gives:

The expression a <=> b returns an object that compares less than zero if a is less than b, compares greater than zero if a is greater than b, and compares equal to zero if a and b are equal/equivalent.

Which is quite a mouthful. I can see why the authors would choose to use symbols.

Verne answered 26/11, 2017 at 16:40 Comment(0)
S
4

What I am interested in, more exactly, is an equivalent expression of "compares <0". Does "compares <0" mean "evaluates to a negative number"?

First, we need to understand the difference between what you quoted and actual wording for the standard. What you quoted was just an explanation for what would actually get put into the standard.

The standard wording in P0515 for the language feature operator<=> is that it returns one of 5 possible types. Those types are defined by the library wording in P0768.

Those types are not integers. Or even enumerations. They are class types. Which means they have exactly and only the operations that the library defines for them. And the library wording is very specific about them:

The comparison category types’ relational and equality friend functions are specified with an anonymous parameter of unspecified type. This type shall be selected by the implementation such that these parameters can accept literal 0 as a corresponding argument. [Example: nullptr_t satisfies this requirement. — end example] In this context, the behaviour of a program that supplies an argument other than a literal 0 is undefined.

Therefore, Herb's text is translated directly into standard wording: it compares less than 0. No more, no less. Not "is a negative number"; it's a value type where the only thing you can do with it is comparing it to zero.

It's important to note how Herb's descriptive text "compares less than 0" translates to the actual standard text. The standard text in P0515 makes it clear that the result of 1 <=> 2 is strong_order::less. And the standard text in P0768 tells us that strong_order::less < 0 is true.

But it also tells us that all other comparisons are the functional equivalent of the descriptive phrase "compares less than 0".

For example, if -1 "compares less than 0", then that would also imply that it does not compare equal to zero. And that it does not compare greater than 0. It also implies that 0 does not compare less than -1. And so on.

P0768 tells us that the relationship between strong_order::less and the literal 0 fits all of the implications of the words "compares less than 0".

Seth answered 26/11, 2017 at 19:41 Comment(13)
I don't understand the sentence, "Which means they have exactly and only the operations that the library defines for them."Basutoland
@q-l-p: ... I don't know how to be more clear. Class types don't have comparison operators defined by default; they have to be defined for them. Since these types are defined by the standard library, then the standard library would be the one defining them.Seth
Oh, wait, I get it now. The sentence means "Which means they have only the operations defined by the library", right?Basutoland
I ask what does "compares less than 0" mean. And you say compares less than zero means "it compares less than 0. No more, no less." Can you give an analogy of what happens? For example Ana and Bob return from a shop... with a toaster (or something) that.............does what?Basutoland
@q-l-p: What does it mean to say that an integer compares less than zero? Your thought might be that it is negative, but you're not thinking fundamentally enough. "Object compares less than zero" means object < 0 == true. But it also means object > 0 == false. And (object == 0) == false.Seth
Ah! You're using "compare" in an unaccusative sense, parallel with "compares unfavourably" ! No wonder neither I nor @Basutoland nor Janus Bahs Jacquet understood. The problem is that in ordinary English "less than zero" is not something that could occur as an adverbial complement like that. I agree that this is a useful expression, but I do not agree that it is plain English. It is jargon.Phaedra
@ColinFine Can you translate what he said in plain English in such a way that he would approve? I will then accept his answer here, and your answer on the English language site. This is interesting stuff!!Basutoland
@q-l-p: I would say "when compared with 0, is reported as negative".Phaedra
@ColinFine: That would suggest that the object is in some way "negative". Which it is not.Seth
@ColinFine You are using... normal logic. Nothing gets compared here. In C++ when X compares <0, that's like saying "Its true that (x<0)"Basutoland
@ColinFine Most jargon has some root in plain English. The overall grammatical structure is plain English. The jargon part is the choice of adverbial as you says, and the special implication of the verb "compare" referring to the result of a particular computation.Monamonachal
@ColinFine Actually, "is negative" is a jargon in math and only applies to certain mathematical objects (e.g. numbers) with certain properties. We cannot call it negative here because that computer object we are talking about is not analogous to those math concepts.Monamonachal
I have edited my question to make it, hopefully, clearer. Would you please address the additional questions in your answer?Basutoland
G
2

"acompares less than zero" means that a < 0 is true.

"a compares == 0 means that a == 0 is true.

The other expressions I'm sure make sense now right?

Gilbart answered 26/11, 2017 at 19:57 Comment(0)
C
2

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.

Contracture answered 28/11, 2017 at 6:41 Comment(2)
I have edited my question to make it, hopefully, clearer. Would you please address the additional questions in your answer?Basutoland
@JerryCoffin: "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." That's not true. P0768 and P0515, in the actual standard wording, make it very clear that for any value of the type, which of those conditions will be true and which will be false. partial_ordering::less for example, will have >0 be false. The "compares <0" phrase only appears in descriptive text for these types.Seth
B
1

I think the other answers so far have answered mostly what the result of the operation is, and that should be clear by now. @VTT's answer explains it best, IMO.

However, so far none have answered the English language behind it. "The object compares less than zero." is simply not standard English, at best it is jargon or slang. Which makes it all the more confusing for non-native speakers.

An equivalent would be:
A comparison of the object using <0 (less than zero) always returns true.

That's quite lengthy, so I can understand why a "shortcut" was created:
The object compares less than zero.

Biannulate answered 30/11, 2017 at 8:38 Comment(2)
Are you attempting to answer the question or you're just commenting?Basutoland
I'm answering. Since you haven't accepted @VTT's answer (which explains best what is happening in code) I was assuming you were looking for an English language explanation instead. So... there you go.Biannulate
U
0

It means that the expression will return an object that can be compared to <0 or >0 or ==0.

If a and b are integers, then the expression evaluates to a negative value (probably -1) if a is less than b.

The expression evaluates to 0 if a==b

And the expression will evaluates to a positive value (probably 1) if a is greater than b.

Unmoving answered 26/11, 2017 at 17:2 Comment(1)
"I think it means ..." Is a rather poor foundation for posting an answer.Indeliberate

© 2022 - 2024 — McMap. All rights reserved.