Is there a bijection between $(0,1)$ and $\mathbb{R}$ that preserves rationality? While reading about cardinality, I've seen a few examples of bijections from the open unit interval $(0,1)$ to $\mathbb{R}$, one example being the function defined by $f(x)=\tan\pi(2x-1)/2$. Another geometric example is found by bending the unit interval into a semicircle with center $P$, and mapping a point to its projection from $P$ onto the real line.
My question is, is there a bijection between the open unit interval $(0,1)$ and $\mathbb{R}$ such that rationals are mapped to rationals and irrationals are mapped to irrationals?
I played around with mappings similar to $x\mapsto 1/x$, but found that this never really had the right range, and using google didn't yield any examples, at least none which I could find. Any examples would be most appreciated, thanks!
 A: Consider the function $f: (0,1) \rightarrow \mathbb{R}$.
$f(n) =\left\{
\begin{array}{ll} 
\dfrac{1}{x} - 2  & \text{if}\ 0 < x \leq \dfrac{1}{2}\\ 
\dfrac{1}{x-1} + 2 & \text{if} \ \dfrac{1}{2} < x < 1
\end{array}
\right.$
We claim that $f$ is a bijective function between the open unit interval $(0,1)$ and $\mathbb{R}$ that takes rationals to rationals and irrationals to irrationals.
First, we notice that $f$ is piece-wise defined in such a way that $dom \ f$ partitions the open unit interval into two sets $S$ and $R$ defined by $S = \left \{ x \in \mathbb{R} \ | \ x \in (0,\dfrac{1}{2}] \right\}$ and $R = \left \{ x \in \mathbb{R} \ | \ x \in (\dfrac{1}{2},1) \right\}$.
Second, we show that $f$ takes rationals only to rationals and irrationals only to irrationals. There are a total of four cases to consider.
$\bf{Case \ \#1}$: Say $x \in S \cap \mathbb{Q}$. Then, by the definition of our function $f$, $\exists a,b \in \mathbb{Z}$ such that $x = \dfrac{a}{b}$ and $f(x) = \dfrac{b}{a} - 2$ and $b \neq 0$ because every rational $x$ can be represented as a ratio of two integers, with the denominator non-zero. Since rationals are closed under division and subtraction, we know that $\dfrac{b}{a} - 2$ is rational.
$\bf{Case \ \#2}$: Say $x \in R \cap \mathbb{Q}$. Then, by the definition of our function $f$, $\exists a,b \in \mathbb{Z}$ such that $x = \dfrac{a}{b}$ and $f(x) = \dfrac{1}{\dfrac{a}{b}-1} + 2$. Since rationals are closed under division, subtraction, and addition, we know that $\dfrac{1}{x-1} + 2$ is rational.
This shows that $\forall x \in (0,1) \cap  \mathbb{Q}, f(x) \in \mathbb{Q} \subseteq \mathbb{R}$. So every rational in the open interval is mapped to a rational.
$\bf{Case \ \#3}$: Say $x \in S \cap (\mathbb{R} - \mathbb{Q}$). Then, by the definition of $f$, $\sim \exists a,b \in \mathbb{Z}$ such that $f(x) = \dfrac{b}{a} - 2$. Since both the subtraction and division of an irrational number by a rational number still produces an irrational number, we know that $\dfrac{b}{a} - 2$ is irrational.
$\bf{Case \ \#4}$: Say $x \in R \cap (\mathbb{R} - \mathbb{Q}$). Then, by the definition of $f$, $\sim \exists a,b \in \mathbb{Z}$ such that $f(x) = \dfrac{1}{\dfrac{a}{b}-1} + 2$. Since the subtraction, division, and addition of an irrational number with a rational number produces an irrational number, we know that $\dfrac{1}{\dfrac{a}{b}-1} + 2$ is irrational.
Therefore, $f$ maps irrationals only to irrationals.
Thirdly, we must show that $f$ is a bijective function.
To prove injectivity, there are two cases:
$\bf{Case \ \#1}$: Let $f(x) = f(y)$, where $x,y \in S \cap \mathbb{Q}$. Then, $\exists a,b,c,d \in \mathbb{Z}$, where $b,d \neq 0$, and $x = \dfrac{a}{b}$ and $y = \dfrac{c}{d}$ such that $\dfrac{b}{a} - 2 = \dfrac{d}{c} -2$. Adding both sides by two, we get $\dfrac{b}{a} = \dfrac{d}{c}$ which implies that $\dfrac{a}{b} = \dfrac{c}{d}$. So $x = y$ and $f$ is injective.
$\bf{Case \ \#2}$: Let $f(x) = f(y)$, where $x,y \in R \cap \mathbb{Q}$. Then, $\exists a,b,c,d \in \mathbb{Z}$, where $b,d \neq 0$, and $x = \dfrac{a}{b}$ and $y = \dfrac{c}{d}$ such that $\dfrac{1}{\dfrac{a}{b}-1} + 2 = \dfrac{1}{\dfrac{c}{d}-1} + 2$. Subtracting both sides by 2 we get $\dfrac{1}{\dfrac{a}{b}-1} = \dfrac{1}{\dfrac{c}{d}-1}$. This implies that $\dfrac{c}{d} - 1 = \dfrac{a}{b} -1$. Adding both sides by 1, we get $y = x$ so $f$ is injective.
To prove surjectivity, there are four cases:
$\bf{Case \ \#1}$:  We show that $\forall y \in \mathbb{Q}$, $y = f(x)$ for some $x \in S \cap \mathbb{Q}$. Let $f(x) = y$; so $y = \dfrac{1}{x} - 2$. Then, $x = \dfrac{1}{y+ 2}$ and we are done.
$\bf{Case \ \#2}$: We show that $\forall y \in \mathbb{Q}$, $y = f(x)$ for some $x \in R \cap \mathbb{Q}$. Let $f(x) = y$; so $y = \dfrac{1}{x-1} + 2$. Then, $x = \dfrac{1}{y-2} + 1$ and we are done.
$\bf{Case \ \#3}$: We show that $\forall y \in (\mathbb{R} - \mathbb{Q})$, $y  = f(x)$ for some $x \in S \cap (\mathbb{R} - \mathbb{Q})$. Let $f(x) = y$; so $y = \dfrac{1}{x} - 2$. Then, $x = \dfrac{1}{y+ 2}$ and we are done.
$\bf{Case \ \#4}$: We show that $\forall y \in (\mathbb{R} - \mathbb{Q})$, $y  = f(x)$ for some $x \in R \cap (\mathbb{R} - \mathbb{Q})$. Let $f(x) = y$; so $y = \dfrac{1}{x-1} + 2$. Then, $x = \dfrac{1}{y-2} + 1$ and we are done.
This completes our proof that $f$ is a bijective function between the open unit interval $(0,1)$ and $\mathbb{R}$ that takes rationals to rationals and irrationals to irrationals.
A: $(1/x)-2$ on $(0,1/2]$ and $2-(1/(x-1/2))$ on $(1/2,1)$.
A: $$
f(x) = \frac{2x - 1}{1 - |2x - 1|}.
$$
A: With the axiom of choice we can find well orderings of $\mathbb{R}$ and of $(0,1)$ such that the first $\omega$ elements are all the rationals of the set, then we can define our map to go from one well ordering to another by preserving the index (that is $a_\alpha\mapsto b_\alpha$, for $\alpha<2^{\aleph_0}$)
As discussed in the comments below by Colin and Jason (and myself), one does not need the axiom of choice for that. Using the Cantor-Schroeder-Bernstein theorem one can have two bijections, one from $(0,1)\setminus\mathbb{Q}$ to $\mathbb{R}\setminus\mathbb{Q}$ and one from $(0,1)\cap\mathbb{Q}$ to $\mathbb{Q}$, and define a bijection as needed without the use of the axiom of choice.
