2

I have two a cyclic integer, modulo 16, so they assume values between 0 and 15.

I need to compare two numbers to determine if n_1 is greater than n_0

n_1 > n_0

Obviously, this is not exactly defined, so I define n_1 to be greater than n_0 if it is less than 8 "numbers" ahead, otherwise, it is lesser than n_0 (if not equal).

I.e. if:

n_0 = 0
if n_1 is between 1 and 8 (both inclusive)
then n_1 is greater than n_0.

n_0 = 5
if n_1 is between 6 and 15 (both inclusive)
then n_1 is greater than n_0.

n_0 = 12
if n_1 is between 13 and 15 (both inclusive)
    or between 0 and 4 (both inclusive)
then n_1 is greater than n_0.

How do I express this comparison programatically?

I am sure I am confusing the terminology above, so please feel free to correct my wording. :)

Bjarke Freund-Hansen
  • 28,728
  • 25
  • 92
  • 135
  • *Obviously, this is not exactly defined* - actually, it's prety well defined mathematically, you're inventing a wheel here (and one that doesn't match the current definition). – SomeWittyUsername Dec 15 '17 at 13:06
  • @SomeWittyUsername: I am entirely sure that I am walking on ground that I don't understand mathematically. Please enlighten me, I would be more than happy to learn how to express the problem mathematically correct? – Bjarke Freund-Hansen Dec 15 '17 at 13:12
  • @SomeWittyUsername: Researching a bit, from this questions it seems that I am right in stating that it is not well defined: https://math.stackexchange.com/questions/1211454/in-mod3-is-3-greater-than-or-less-than-1 – Bjarke Freund-Hansen Dec 15 '17 at 13:24
  • you're right, I stand corrected – SomeWittyUsername Dec 15 '17 at 13:34
  • Looks like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) question. – President James K. Polk Dec 15 '17 at 14:06
  • @JamesKPolk: Not really :) I posted no attempted solution, only the problem. And the answers already provides the solution, which I have implemented and it is working perfectly. :) – Bjarke Freund-Hansen Dec 18 '17 at 07:38

4 Answers4

1

I was thinking of a clock with 16 hours. The idea is basically to move n0 to the 0 position and move n1 the same amount of "ticks". Now you can simply check if n1 is greater or smaller dependent on if it is before 8 or after 8 o'clock.

public int compare (int n0, int n1){
    int ticksToZero = 16 - n0;
    if(n0 == n1)
        return 0;
    else if((n1 + ticksToZero) % 16 <= 8)
        return -1; //n0 is smaller than n1
    else
        return 1; //n0 is larger than n1
}
SaiBot
  • 3,595
  • 1
  • 13
  • 19
1

You may test it by finding the difference of n1 and n0 and test it whether it is between 1 and 8.

#include <iostream>
using namespace std;

bool Test(int n0, int n1) {
    int n = (n1 - n0 + 16) % 16;
    return n && n <= 8;
}

int main() {
    cout << Test(0, 0) << endl;
    cout << Test(0, 1) << endl;
    cout << Test(0, 8) << endl;
    cout << Test(0, 9) << endl;
    cout << Test(0, 15) << endl;
    cout << endl;

    cout << Test(5, 0) << endl;
    cout << Test(5, 4) << endl;
    cout << Test(5, 5) << endl;
    cout << Test(5, 6) << endl;
    cout << Test(5, 13) << endl;
    cout << Test(5, 15) << endl;
    cout << endl;

    cout << Test(12, 0) << endl;
    cout << Test(12, 3) << endl;
    cout << Test(12, 4) << endl;
    cout << Test(12, 5) << endl;
    cout << Test(12, 12) << endl;
    cout << Test(12, 15) << endl;

    return 0;
}
abdullah
  • 662
  • 5
  • 7
1

You can do it without an explicit addition of 16 using this expression:

(b - a) >= (a <= b ? 8 : -8);

The idea is that the difference must be above 8 or -8 depending on the result of comparing a to b.

The result of applying this formula to numbers 0..15, inclusive, is as follows (asterisks represent points where the number from the horizontal line is less than the number from the vertical line; hex digits are used to represent numbers above 9; demo)

  0 1 2 3 4 5 6 7 8 9 A B C D E F
0                 * * * * * * * * 
1 *                 * * * * * * * 
2 * *                 * * * * * * 
3 * * *                 * * * * * 
4 * * * *                 * * * * 
5 * * * * *                 * * * 
6 * * * * * *                 * * 
7 * * * * * * *                 * 
8 * * * * * * * *                 
9   * * * * * * * *               
A     * * * * * * * *             
B       * * * * * * * *           
C         * * * * * * * *         
D           * * * * * * * *       
E             * * * * * * * *     
F               * * * * * * * *   
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

I started with the simple part of the condition, and then mirrored it.

    function smaller(n_0, n_1) {
      n = 16;   
      n_0 = n_0 % n;
      n_1 = n_1 % n;
    
      if(n_0 == n_1)
          return 0;
      else
          return (n_0 < n_1 && n_1 <= n_0 + 8) || (n_1 < n_0 &&  n_0 >= n_1 + 8);
    }
    console.log(0);
    console.log(smaller(0,1));
    console.log(smaller(0,8));
    console.log(smaller(0,9));
    console.log(5);
    console.log(smaller(5,6));
    console.log(smaller(5,15));
    console.log(smaller(5,16));
    console.log(12);
    console.log(smaller(12,13));
    console.log(smaller(12,14));
    console.log(smaller(12,15))
    console.log(smaller(12,0));
    console.log(smaller(12,1))
    console.log(smaller(12,2))
    console.log(smaller(12,3))
    console.log(smaller(12,4));
    console.log(smaller(12,5));
    console.log(smaller(12,6));
    console.log(smaller(12,7));
    console.log(smaller(12,8));
    console.log(smaller(12,9));
    console.log(smaller(12,10));
    console.log(smaller(12,11));
Adder
  • 5,708
  • 1
  • 28
  • 56