In 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.
In C, the functions strcmp and memcmp perform a three-way comparison between strings and memory buffers, respectively.
The Python 2.x cmp(removed in 3.x), OCaml compare, and Kotlin compareTo functions compute the same thing.
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.
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.
[3] 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.