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 editContent deleted Content addedVisualWikitext
Revision as of 19:02, 14 May 2014 editSmithph (talk | contribs)68 edits The archaic projection of booleans into integers is at best confusing and irrelevant.← Previous edit Latest revision as of 19:35, 28 July 2023 edit undoPsychpsyo (talk | contribs)14 editsm Made redirect more specificTag: Redirect target changed 
(10 intermediate revisions by 7 users not shown)
Line 1: Line 1:
#REDIRECT ]
The '''spaceship operator''', written <code><=></code>, is a binary ] that originated in the ] ]. Other languages, such as ] and ], also support the spaceship operator. Unlike traditional equality operators, which will return true or 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>.


{{Redirect category shell|1=
As a formula:
{{R to section}}
:<math>
{{R with history}}
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>

In Perl, the <code>&lt;=&gt;</code> operator only performs numeric comparisons. For string-based comparison, the analogous <code>cmp</code> operator is used instead.

In other languages (like PHP and C) the <code>&lt;=&gt;</code> operator is not needed for numbers because one can simply do <code>b - a</code>. Rather than returning exactly 1 (or -1), this will return a value greater (or less) than 0. But for those languages this doesn't matter and works fine.

The spaceship operator is primarily used for comparisons in ].

The spaceship operator takes its name because it resembles ]'s fighter from ] . The term is now commonly used and the operator is referred by the name within the Perl ].

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

== Example ==
<source lang="perl">
$a = 5 <=> 7; # $a is set to -1
$a = 7 <=> 5; # $a is set to 1
$a = 6 <=> 6; # $a is set to 0
</source>

== See also ==
* ]
* ]

==External links==
{{wikibooks|Perl Programming}}
*
*

]
]

Latest revision as of 19:35, 28 July 2023

Redirect to:

This page is a redirect. The following categories are used to track and monitor this redirect:
  • With history: This is a redirect from a page containing substantive page history. This page is kept as a redirect to preserve its former content and attributions. Please do not remove the tag that generates this text (unless the need to recreate content on this page has been demonstrated), nor delete this page.
    • This template should not be used for redirects having some edit history but no meaningful content in their previous versions, nor for redirects created as a result of a page merge (use {{R from merge}} instead), nor for redirects from a title that forms a historic part of Misplaced Pages (use {{R with old history}} instead).
When appropriate, protection levels are automatically sensed, described and categorized.