Skip to main content
2 of 4
added 120 characters in body
bigyihsuan
  • 10.8k
  • 6
  • 8

Implement the <=> three-way comparison operator on numbers

Tags:

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 return 0.
  • If a > b, then return 1.

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 return 0.
  • If a > b, then return 1.

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 , so shortest answer wins!
bigyihsuan
  • 10.8k
  • 6
  • 8