In
computer science
Computer science is the study of computation, information, and automation. Computer science spans Theoretical computer science, theoretical disciplines (such as algorithms, theory of computation, and information theory) to Applied science, ...
, a three-way comparison takes two values A and B belonging to a type with a
total order
In mathematics, a total order or linear order is a partial order in which any two elements are comparable. That is, a total order is a binary relation \leq on some set X, which satisfies the following for all a, b and c in X:
# a \leq a ( re ...
and determines whether A < B, A = B, or A > B in a single operation, in accordance with the mathematical
law of trichotomy.
It can be implemented in terms of a
function (such as
strcmp
in
C), a
method
Method (, methodos, from μετά/meta "in pursuit or quest of" + ὁδός/hodos "a method, system; a way or manner" of doing, saying, etc.), literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In re ...
(such as
compareTo
in
Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
), or an
operator (such as the spaceship operator
<=>
in
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language".
Perl was developed ...
,
PHP and
C++).
Machine-level computation
Many processors have
instruction set
In computer science, an instruction set architecture (ISA) is an abstract model that generally defines how software controls the CPU in a computer or a family of computers. A device or program that executes instructions described by that ISA, s ...
s that support such an operation on primitive types. Some machines have signed
integer
An integer is the number zero (0), a positive natural number (1, 2, 3, ...), or the negation of a positive natural number (−1, −2, −3, ...). The negations or additive inverses of the positive natural numbers are referred to as negative in ...
s based on a sign-and-magnitude or
ones' complement
The ones' complement of a binary number is the value obtained by inverting (flipping) all the bits in the Binary number, binary representation of the number. The name "ones' complement" refers to the fact that such an inverted value, if added t ...
representation (see
signed number representations
In computing, signed number representations are required to encode negative numbers in binary number systems.
In mathematics, negative numbers in any base are represented by prefixing them with a minus sign ("−"). However, in RAM or CPU reg ...
), both of which allow a differentiated positive and negative
zero
0 (zero) is a number representing an empty quantity. Adding (or subtracting) 0 to any number leaves that number unchanged; in mathematical terminology, 0 is the additive identity of the integers, rational numbers, real numbers, and compl ...
. This does not violate trichotomy as long as a consistent total order is adopted: either −0 = +0 or −0 < +0 is valid. Common
floating point types, however, have an exception to trichotomy: there is a special value "NaN" (
Not a Number) such that ''x'' < NaN, ''x'' > NaN, and ''x'' = NaN are all false for all floating-point values ''x'' (including NaN itself).
High-level languages
Abilities
In
C, the functions
strcmp
and
memcmp
perform a three-way comparison between strings and memory buffers, respectively. They return a negative number 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
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language".
Perl was developed ...
(for numeric comparisons only, the
cmp
operator is used for string lexical comparisons),
PHP (since version 7),
Ruby
Ruby is a pinkish-red-to-blood-red-colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapph ...
, and Apache
Groovy
''Groovy'' (or, less commonly, ''groovie'' or ''groovey'') is a slang colloquialism popular during the 1960s and 1970s. It is roughly synonymous with words such as "excellent", "fashionable", or "amazing", depending on context.
History
The word ...
, the "spaceship operator"
<=>
returns the values −1, 0, or 1 depending on whether A < B, A = B, or A > B, respectively. The Python 2.x
cmp
(removed in 3.x),
OCaml
OCaml ( , formerly Objective Caml) is a General-purpose programming language, general-purpose, High-level programming language, high-level, Comparison of multi-paradigm programming languages, multi-paradigm programming language which extends the ...
compare
, and
Kotlin compareTo
functions compute the same thing. In the
Haskell
Haskell () is a general-purpose, statically typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research, and industrial applications, Haskell pioneered several programming language ...
standard
library
A library is a collection of Book, books, and possibly other Document, materials and Media (communication), media, that is accessible for use by its members and members of allied institutions. Libraries provide physical (hard copies) or electron ...
, the three-way comparison function
compare
is defined for all types in the
Ord
class
Class, Classes, or The Class may refer to:
Common uses not otherwise categorized
* Class (biology), a taxonomic rank
* Class (knowledge representation), a collection of individuals or objects
* Class (philosophy), an analytical concept used d ...
; it returns type
Ordering
, whose values are
LT
(less than),
EQ
(equal), and
GT
(greater than):
data Ordering = LT , EQ , GT
Many
object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impl ...
languages have a three-way comparison
function, which performs a three-way comparison between the
object and another given object. For example, in
Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
, any class that implements the
Comparable
interface has
compareTo
method which either returns a negative integer, zero, or a positive integer, or throws a
NullPointerException
(if one or both objects are
null
). Similarly, in the
.NET
The .NET platform (pronounced as "''dot net"'') is a free and open-source, managed code, managed computer software framework for Microsoft Windows, Windows, Linux, and macOS operating systems. The project is mainly developed by Microsoft emplo ...
framework, any class that implements the
IComparable
interface has such
CompareTo
method. In
C++, any class that can be three-way compared can be a parameter to
instances ofbr>
std::compare_three_way
std::strong_order
std::weak_order
o
std::partial_order
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. In principle, a compiler might deduce that these two expressions could be replaced by only one comparison followed by multiple tests of the result, but mention of this optimization is not to be found in texts on the subject.
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 usually tracked and can be used to determine order after subtraction, but this information is usually unavailable 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.
Spaceship operator
The three-way comparison operator or "spaceship operator" for numbers is denoted as
<=>
in
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language".
Perl was developed ...
,
Ruby
Ruby is a pinkish-red-to-blood-red-colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapph ...
, Apache
Groovy
''Groovy'' (or, less commonly, ''groovie'' or ''groovey'') is a slang colloquialism popular during the 1960s and 1970s. It is roughly synonymous with words such as "excellent", "fashionable", or "amazing", depending on context.
History
The word ...
,
PHP, Eclipse
Ceylon
Sri Lanka, officially the Democratic Socialist Republic of Sri Lanka, also known historically as Ceylon, is an island country in South Asia. It lies in the Indian Ocean, southwest of the Bay of Bengal, separated from the Indian subcontinent, ...
, and
C++, and is called the ''spaceship operator''.
In
C++, the
C++20 revision adds the spaceship operator
<=>
, which returns a value that encodes whether the 2 values are equal, less, greater, or unordered and can return different types depending on the strictness of the comparison.
The name's origin is due to it reminding
Randal L. Schwartz
Randal L. Schwartz (born November 22, 1961), also known as merlyn, is an American author, system administrator and programming consultant. He has written several books on the Perl programming language, and plays a promotional role within the Per ...
of the spaceship in an
HP BASIC ''Star Trek'' game. Another coder has suggested that it was so named because it looked similar to Darth Vader's
TIE fighter in the ''
Star Wars
''Star Wars'' is an American epic film, epic space opera media franchise created by George Lucas, which began with the Star Wars (film), eponymous 1977 film and Cultural impact of Star Wars, quickly became a worldwide popular culture, pop cu ...
'' saga.
Example in PHP:
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1
Example in C++:
1 <=> 1; // evaluates to std::strong_ordering::equal
1 <=> 2; // evaluates to std::strong_ordering::less
2 <=> 1; // evaluates to std::strong_ordering::greater
Composite data types
Three-way comparisons have the property of being easy to compose and build
lexicographic
Lexicography is the study of lexicons and the art of compiling dictionaries. It is divided into two separate academic disciplines:
* Practical lexicography is the art or craft of compiling, writing and editing dictionaries.
* Theoretical lex ...
comparisons of non-primitive data types, unlike two-way comparisons.
Here is a composition example in Perl.
sub compare($$)
Note that
cmp
, in Perl, is for strings, since
<=>
is for numbers. Two-way equivalents tend to be less compact but not necessarily less legible. The above takes advantage of
short-circuit evaluation of the
, ,
operator, and the fact that 0 is considered false in Perl. As a result, if the first comparison is equal (thus evaluates to 0), it will "fall through" to the second comparison, and so on, until it finds one that is non-zero, or until it reaches the end.
In some languages, including
Python,
Ruby
Ruby is a pinkish-red-to-blood-red-colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapph ...
,
Haskell
Haskell () is a general-purpose, statically typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research, and industrial applications, Haskell pioneered several programming language ...
, etc., comparison of lists is done lexicographically, which means that it is possible to build a chain of comparisons like the above example by putting the values into lists in the order desired; for example, in Ruby:
.unit, a.rank, a.name<=> .unit, b.rank, b.name/syntaxhighlight>In C++:std::tie(a.unit, a.rank, a.name) <=> std::tie(b.unit, b.rank, b.name)
SQL Join Operator
In some SQL Dialects, <=>
is used as a null-safe join operator which will match operands if both are null.[https://dev.mysql.com/doc/refman/8.4/en/comparison-operators.html#operator_equal-to]
See also
* Sign function
In mathematics, the sign function or signum function (from '' signum'', Latin for "sign") is a function that has the value , or according to whether the sign of a given real number is positive or negative, or the given number is itself zer ...
* Three-valued logic
In logic, a three-valued logic (also trinary logic, trivalent, ternary, or trilean, sometimes abbreviated 3VL) is any of several many-valued logic systems in which there are three truth values indicating ''true'', ''false'', and some third value ...
References
{{DEFAULTSORT:Three-Way Comparison
Conditional constructs
Operators (programming)