All things are not created equal

Comparing values quantitatively

Jack Holland
Understanding computer science
5 min readDec 17, 2013

--

This is an ongoing series. Please check out the collection for the rest of the articles.

The topic today is one we all intuitively understand already. The goal is to formalize and quantify our intuitions so that we can explain them to a computer. This is a theme we’ll return to a lot: taking our intuitive notions and solidifying them into formal instructions and theories. The main goal is to formalize our intuitions into computable, programmable blocks: instructions, numbers, rules, etc. But there’s a wonderful side effect that comes along with this; by quantifying our intuitions, we gain a better understanding of the concepts behind them. Don’t view quantification as a tedious necessity! It’s an insightful process that hones vague notions into concrete knowledge.

Without further ado, the topic is comparing values — testing whether two things are equal or if one thing is smaller or larger than the other. It’s fairly straightfoward syntax-wise. In fact, you’ve already seen one instruction:

  • x == y: returns true if x equals y; otherwise returns false

Said another way, == tests if two things are equal. It returns true (as in, responds “yes”) if the two things are equal and returns false (as in, responds “no”) if they aren’t. You’re probably used to a single equals sign, =, to mean equality. By convention, = means something a bit different, so == is used for equality. This instruction works for numbers, like 2 and -20.5, and the Booleans: true and false. For Booleans, true equals true and false equals false — and that’s it. false does not equal true, nor does true equal false. As discussed before, this embodies the more general rule that x == y is the same thing as y == x. This is intuitively obvious in the case of numbers and Booleans but in the future we’ll encounter data types for which this does not hold true. If you’re curious, we say that if x == y is the same as y == x then this instruction, ==, is commutative with respect to x and y. If you’re not big on memorizing terminology, that’s fine — I try to talk about concepts as explicitly as possible, minimizing jargon whenever I can.

The opposite of == is !=. While == means “equals to”, != means “not equal to”. Every time using == returns true, != returns false. So 3 == 3 returns true but 3 != 3 returns false. Accordingly, 3 == 4 returns false but 3 != 4 returns true. Booleans work as you would expect: true != false returns true. To tie this into previous posts, x != y is the same as not(x == y). In fact, != is really just a more concise way of combining not and ==. To see why this is true, consider an example. 5 != 5 is false because 5 does equal 5. On the flip side, 5 == 5 is true for the same reason: 5 equals 5. So negating 5 == 5 with not makes it false. This applies to not just 5 but every number; 5 != 5 must return the same value as not(5 == 5).

Although it’s a bit hypocritical given I just promised to avoid jargon, I’m going to reintroduce some terminology I mentioned in a previous post because it highlights an important idea. Namely, x != y and not(x == y) are logically equivalent. This means that these two expressions always return the same value if x and y are replaced with the same numbers in both expressions. In other words, whether you set x to 5 and y to 10 or x to -100 and y to 0, x != y must return the same value as not(x == y). You can swap one expression for the other and you’ll get the same answer.

So == and != cover a fair bit of logical ground; if you want to test if two things are equal or two things are unequal, these two instructions do the job. But what if you want to test if one thing is smaller or bigger than the other thing? There’s just no way to do this with == or !=. Knowing that x != y is true doesn’t tell you which thing is smaller than the other. For that, we’ll need some more instructions:

  • x < y: returns true if x is less than y; otherwise, returns false
  • x > y: returns true if x is more than y; otherwise, returns false

As you can see, < and > have the same meanings that they usually do. If you have trouble remembering which is which, look at it this way: the smaller number gets the smaller side of the wedge; the larger number gets the larger side of the wedge. So 3 < 4, -6 < -4, and 100 < 101 all return true. As you can probably infer, 4 < 3 returns false as does 4 < 4. That last example leads to an interesting corner case: < and > are not strictly opposites. In other words, just because x < y is false does not mean x > y is true. Why? Because it could be that instead, x == y is true. Thus, there are three possibilities when it comes to the relationship between x and y: x is less than y, x is equal to y, or x is more than y. Depending on your logic background, this observation may be very obvious or completely novel. Either way, it’s useful to think about comparisons so explicitly. For the sake of convenience, there are two more related instructions:

  • x <= y: returns true if x is less than or equal to y; otherwise, returns false
  • x >= y: returns true if x is more than or equal to y; otherwise, returns false

Logically speaking, these are just shorthand for (respectively):

  • (x < y) or (x == y)
  • (x > y) or (x == y)

Do you see why this is true? The first new instruction, x <= y, returns true if one of two conditions is true:

  1. x is less than y (x < y)
  2. x is equal to y (x == y)

If either of these conditions is true (they can’t both be true, so it doesn’t matter whether you use or or xor), then x <= y also returns true. This same logic applies to x >= y but with > instead of <.

Therefore, the opposite of < is >= and the opposite of > is <=. When one is true, the other must be false. If this isn’t clear, try some examples. Take 5 and 6. Clearly, 5 < 6 is true. Accordingly, 5 >= 6 is false since neither 5 == 6 nor 5 > 6 is true. I hope we can agree that this rule holds no matter what numbers we replace x and y with. Either x < y or x >= y, exclusively. They can’t both be true and they can’t both be false.

There is, as always, much more to discuss. But this is a solid start and there are more important topics to cover before we tackle more advanced concepts concerning comparisons and order. When it comes to basic operations, the six instructions we’ve covered (==, !=, <, <=, >, >=) are enough to get the job done.

I’ve ignored a pretty big topic so far in these posts but I think we’re at the point at which it must be introduced. Actually, I introduced it metaphorically when I discussed the warlord’s crown. The concept is called an array. It’s basically a list of things, but there are some technical considerations that make it a bit tricker to explain. Stay tuned for a gentle introduction to one of the most fundamental structures in computer science!

--

--