﻿

# Comparison (computer programming)

In computer programming, comparison of two data items is effected by the comparison operators typically written as:

> (greater than)
< (less than)
>= (greater than or equal to)
<= (less than or equal to)
= or == (exactly equal to)
!=, <>, ~= or /= (not equal to)

These operators produce the logical value `true` or `false`, depending on the result of the comparison. For example, in the pseudo-code

`if a > 1 then ...`

the statements following `then` are executed only if the value of the variable "a" is greater than 1 (i.e. when the logical value of `a > 1` is `true`).

Some programming languages make a syntactical distinction between the "equals" of assignment (e.g. `a = 1` assigns the value 1 to the variable "a") and the "equals" of comparison (`if a == 1 then ...`). Other languages determine which is meant from context.

"Greater than" and "less than" comparison of non-numeric data is performed according to a sort convention (such as, for text strings, lexicographical order) which may be built in to the programming language and/or configurable by the programmer.

When it is desired to associate a numeric value with the result of a comparison between two data items, say "a" and "b", the usual convention is to assign −1 if a < b, 0 if a = b and 1 if a > b. For example, the C function `strcmp` performs a three-way comparison and returns −1, 0, or 1 according to this convention, and qsort expects the comparison function to return values according to this convention. In sorting algorithms, the efficiency of comparison code is critical since it is one of the major factors contributing to sorting performance.

In floating-point arithmetic, numbers, including many simple fractions, cannot be represented exactly, and it may be necessary to test for equality within a given tolerance. For example, rounding errors may mean that the comparison in

`a = 1/7`
`if a*7 = 1 then ...`

unexpectedly evaluates `false`. Typically this problem is handled by rewriting the comparison as `if abs(a*7 - 1) < tolerance then ...`, where `tolerance` is a suitably tiny number and `abs` is the absolute value function.

Comparison of programmer-defined data types (data types of which the programming language itself has no in-built understanding) may be carried out by custom-written or library functions (such as `strcmp` mentioned above), or, in some languages, by "overloading" a comparison operator – that is, assigning a programmer-defined meaning that depends on the data types being compared.

Sometimes, particularly in object-oriented programming, the comparison raises questions of data types and inheritance, equality and identity. It is often necessary to distinguish between:

• two objects with different datatypes both related to another datatype, e.g. an orange and a lemon, both being citrus fruit
• two different objects of the same type, e.g. two hands
• two objects being equal but distinct, e.g. two \$10 banknotes
• two different references to the same object, e.g. two nicknames for the same person

Sameness and difference can be relative or graduated as well as absolute, particularly in fuzzy logic, artificial intelligence, signal processing, lossy data compression and pattern recognition.

Wikimedia Foundation. 2010.