Show order equivalence between the rationals and the binary fractions
+2
−0
Write a function $f$ from rational numbers to binary fractions (rational numbers whose denominator is a power of two) which is bijective and preserves order.
That means:
- For every binary fraction $x$ there is exactly one rational $y$ such that $f(y)=x$.
- $x < y \iff f(x) < f(y)$
That's all.
This is code-golf so the goal is to minimize the size of your source code as measured in bytes.
IO
You should not take input as a floating point. This is because floating point numbers only ever represent binary fractions, which messes with the whole idea of the challenge. You can take input in any reasonable format which allow the representation of non-binary fractions, including any of the following
- A native rational type.
- The numerator and denominator of a fully reduced fraction. That is, a pair $p\in\mathbb Z$ and $q\in\mathbb N_{>0}$ such that $\gcd\left(\lvert p\rvert, q\right) = 1$.
- The absolute value of the numerator and denominator along with a boolean representing the sign.
You may output in any of these formats as well types that only represent binary fractions.
1 comment thread