Three-way comparisonIn computer science, a three-way comparison takes two values A and B belonging to a type with a total order 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 Machine-level computationMany processors have instruction sets that support such an operation on primitive types. Some machines have signed integers based on a sign-and-magnitude or ones' complement representation (see signed number representations), both of which allow a differentiated positive and negative zero. 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 languagesAbilitiesIn C, the functions In Perl (for numeric comparisons only, the data Ordering = LT | EQ | GT
Many object-oriented programming languages have a three-way comparison function, which performs a three-way comparison between the object and another given object. For example, in Java, any class that implements the Since Java version 1.5, the same can be computed using the 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 operatorThe three-way comparison operator or "spaceship operator" for numbers is denoted as In C++, the C++20 revision adds the spaceship operator The name's origin is due to it reminding Randal L. Schwartz of the spaceship in an HP BASIC Star Trek game.[4] Another coder has suggested that it was so named because it looked similar to Darth Vader's TIE fighter in the Star Wars saga.[5] 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 typesThree-way comparisons have the property of being easy to compose and build lexicographic comparisons of non-primitive data types, unlike two-way comparisons. Here is a composition example in Perl. sub compare($$) {
my ($a, $b) = @_;
return $a->{unit} cmp $b->{unit}
|| $a->{rank} <=> $b->{rank}
|| $a->{name} cmp $b->{name};
}
Note that In some languages, including Python, Ruby, Haskell, 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: [a.unit, a.rank, a.name] <=> [b.unit, b.rank, b.name]
In C++: std::tie(a.unit, a.rank, a.name) <=> std::tie(b.unit, b.rank, b.name)
SQL Join OperatorIn some SQL Dialects, See alsoReferences
|
Portal di Ensiklopedia Dunia