Jump to content

Comparison (computer programming)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 180.191.146.67 (talk) at 06:53, 20 August 2012. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.


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.

See also