Three-way Comparison - High-level Languages

High-level Languages

In C, the `strcmp` and `memcmp` perform three-way comparison between strings and memory buffers, respectively. They return a number with sign -1 when the first argument is lexicographically smaller than the second, zero when the arguments are equal and a positive number otherwise. This convention of returning the "sign of the difference" is extended to arbitrary comparison functions by the standard sorting function qsort, which takes a comparison function as an argument and requires it to abide by it.

In Perl (numeric comparison only), Ruby, and Groovy, the spaceship operator ("<=>") returns the sign function (a.k.a. signum) values of -1, 0, or 1 depending on whether A < B, A = B, or A > B, respectively. In Python 2.x (since removed in 3.x), the cmp function computes the same thing. In OCaml the compare function computes the same thing. In the Haskell standard library the three-way comparison function compare is defined for all types in the Ord class; it returns type Ordering, whose values are LT (less than), EQ (equal), and GT (greater than).

Many object-oriented languages have a three-way comparison method, which performs a three-way comparison between the object and another given object. For example, in Java, any class that implements the Comparable interface has a compareTo method which returns a negative integer, zero, or a positive integer. Similarly, in the .NET Framework, any class that implements the IComparable interface has such a CompareTo method.

Since Java version 1.5, the same can be computed using the Math.signum static method if the difference can be known without computational problems such as arithmetic overflow mentioned below. Many computer languages allow the definition of functions so a compare(A,B) could be devised appropriately, but the question is whether or not its internal definition can employ some sort of three-way syntax or else must fall back on repeated tests.

When implementing a three-way comparison where a three-way comparison operator or method is not already available, it is common to combine two comparisons, such as A = B and A < B, or A < B and A > B. A compiler may be able to optimize these two comparisons into a single three-way comparison at a lower level, but this is not a common optimization.

In some cases, three-way comparison can be simulated by subtracting A and B and examining the sign of the result, exploiting special instructions for examining the sign of a number. However, this requires the type of A and B to have a well-defined difference. Fixed-width signed integers may overflow when they are subtracted, floating-point numbers have the value NaN with undefined sign, and character strings have no difference function corresponding to their total order. At the machine level, overflow is typically tracked and can be used to determine order after subtraction, but this information is not usually available to higher-level languages.

In one case of a three-way conditional provided by the programming language, Fortran's now-deprecated three-way arithmetic IF statement considers the sign of an arithmetic expression and offers three labels to jump to according to the sign of the result:

IF (expression) negative,zero,positive

The common library function strcmp in C and related languages is a three-way lexicographic comparison of strings; however, these languages lack a general three-way comparison of other data types.

Read more about this topic:  Three-way Comparison

Famous quotes containing the word languages:

    The less sophisticated of my forbears avoided foreigners at all costs, for the very good reason that, in their circles, speaking in tongues was commonly a prelude to snake handling. The more tolerant among us regarded foreign languages as a kind of speech impediment that could be overcome by willpower.
    Barbara Ehrenreich (b. 1941)