Implement the <=>
three-way comparison operator on numbers
Tags: code-golfnumbercomparison
In C++, there exists a a <=> b
three-way comparison operator that, for numerical types, does the following:
- If
a < b
, then return-1
. - If
a = b
, then return0
. - If
a > b
, then return1
.
The Challenge
Implement the shortest full program, function, method, or custom operator such that, when given two numbers of the same type a
and b
,
- If
a < b
, then return-1
. - If
a = b
, then return0
. - If
a > b
, then return1
.
Bonuses
If your language supports at least 2 number types (int, float, byte, long, etc.), and your solution is generic across k
numerical types, you can subtract (k-1)
% from your score for each numeric type your solution supports.
So if your solution is 100 bytes, and you support 5 numeric types, your score is 100 - 100*(5-1)% = 100 - 4 = 96
.
Notes
- If you're writing in x86 assembly, this question may be of use to you.
- This is code-golf, so shortest answer wins!