Misplaced Pages

Spaceship operator: Difference between revisions

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
Browse history interactively← Previous editNext edit →Content deleted Content addedVisualWikitext
Revision as of 19:26, 27 November 2008 editAlksentrs (talk | contribs)Extended confirmed users1,314 editsm <code>ify; said 0/1 mean false/true; “−” instead of “-”← Previous edit Revision as of 03:48, 28 November 2008 edit undoAlksentrs (talk | contribs)Extended confirmed users1,314 edits add info about incomparable arguments; add a formulaNext edit →
Line 1: Line 1:
The '''spaceship operator''' is a binary ] that originated in the ] ]. Other languages, such as ] and ] also support the spaceship operator. It is written <code>&lt;=&gt;</code>. Unlike traditional equality operators, which will return 1 (true) or 0 (false) depending on whether the arguments are equal or unequal, the spaceship operator will return 1, 0, or &minus;1 depending on the value of the left argument relative to the right argument. If the left argument is greater than the right argument, the operator returns 1. If the left argument is less than the right argument, the operator returns &minus;1. If the two arguments are equal, the operator returns 0. The '''spaceship operator''' is a binary ] that originated in the ] ]. Other languages, such as ] and ] also support the spaceship operator. It is written <code>&lt;=&gt;</code>. Unlike traditional equality operators, which will return 1 (true) or 0 (false) depending on whether the arguments are equal or unequal, the spaceship operator will return 1, 0, or &minus;1 depending on the value of the left argument relative to the right argument. If the left argument is greater than the right argument, the operator returns 1. If the left argument is less than the right argument, the operator returns &minus;1. If the two arguments are equal, the operator returns 0. If the two arguments cannot be compared (e.g. one of them is ]), the operator returns <code>undef</code>.

As a formula:
:<math>
a\ \texttt{<=>}\ b\ \ \ =
\begin{cases}
-1 & \mbox{if }a < b, \\
0 & \mbox{if }a = b, \\
1 & \mbox{if }a > b, \\
\texttt{undef}
& \mbox{otherwise.}
\end{cases}
</math>


The spaceship operator is primarily used for comparisons in ]. The spaceship operator is primarily used for comparisons in ].

Revision as of 03:48, 28 November 2008

The spaceship operator is a binary relational operator that originated in the Perl programming language. Other languages, such as Ruby and Groovy also support the spaceship operator. It is written <=>. Unlike traditional equality operators, which will return 1 (true) or 0 (false) depending on whether the arguments are equal or unequal, the spaceship operator will return 1, 0, or −1 depending on the value of the left argument relative to the right argument. If the left argument is greater than the right argument, the operator returns 1. If the left argument is less than the right argument, the operator returns −1. If the two arguments are equal, the operator returns 0. If the two arguments cannot be compared (e.g. one of them is NaN), the operator returns undef.

As a formula:

a   <=>   b       = { 1 if  a < b , 0 if  a = b , 1 if  a > b , undef otherwise. {\displaystyle a\ {\texttt {<=>}}\ b\ \ \ ={\begin{cases}-1&{\mbox{if }}a<b,\\0&{\mbox{if }}a=b,\\1&{\mbox{if }}a>b,\\{\texttt {undef}}&{\mbox{otherwise.}}\end{cases}}}

The spaceship operator is primarily used for comparisons in sorting.

The spaceship operator takes its name because it looks like a small flying saucer as ASCII art. The term is now commonly used and the operator is referred by the name within the Perl documentation.

This operator is also used in ASCII-based mathematical notation to represent "less than, equal to or greater than", and is synonymous with the symbols ⋛ and ⋚. It can be used to test if the result of a calculation is actually a number.

Example

$a = 5 <=> 7;  # $a is set to -1
$a = 7 <=> 5;  # $a is set to 1
$a = 6 <=> 6;  # $a is set to 0

External links

Category: