Pack two fractional values into a single integer while preserving a total order

24 Views Asked by At

Say I have points (x, y) and I define a total order on such point by comparing the x-values of two points and if they are equal, the y-values of two points are compared.

The x, y coordinates are fractional values. Assume we know the max value (e.g., all values smaller than 1000) and that there are only two digits after the decimal point.

How can I convert the point (the two fractional x, y coordinates) to a single integer (e.g., smaller than two billions/the int64 max value), such that the total order is preserved (the total order of comparing two pairs of coordinates.

Example: A list of (x, y) cordinates such as: [ (26.43, 73.11) , (326.57, 734.26), ...] is converted to a list of (say positive) integer values and the integer value representing (26.43, 73.11) is still smaller than the int value representing (326.57, 734.26).

I know an inefficient/insufficient way would be to use a Gödel's encoding in primes for this.

1

There are 1 best solutions below

1
On BEST ANSWER

If you multiply $x$ and $y$ by $100$ you get them as ints in the range $[0,10^5-100]$. You can then take $(100x,100y)$ to $10^5(100x)+y$ and do just what you want. Going back to the original variables, you would have $N(x,y)=10^7x+100y$