Give an example of a function $h:\mathbb{R}\backslash\mathbb{Q}\rightarrow\ \mathbb{Q}$

The question is

$$h:\mathbb{R}\backslash\mathbb{Q}\rightarrow\ \mathbb{Q}$$ so that the image of $$h$$ is the same as the codomain of $$h$$.

I couldn't really think of a function that maps irrational numbers to rational numbers. Can anyone give me some hints?

• One method would be to think of an invertible function $g: \mathbb{Q} \to \mathbb{R} \setminus \mathbb{Q}$; and then map all the un-mapped-to irrationals to, say, zero. Oct 1, 2022 at 6:24

2 Answers

$$h(x)=r$$ if $$x=\sqrt2+r$$ for some $$r\in\mathbb Q$$, and $$h(x)=0$$ else.

• Clever! :) :) :) Jan 4, 2023 at 22:18

Use the floor of the absolute value, $$x\mapsto⌊|x|⌋\ :\ {\mathbb R}\setminus{\mathbb Q}\,\to\,{\mathbb N}$$ which is surjective, and further use that $${\mathbb Q}$$ is countable.

• The floor function would only produce $\mathbb{N}$ right? The question is actually asking me to find a function whose range is $\mathbb{Q}$. Oct 1, 2022 at 6:41
• @d0nut Yeah for brievity the answer takes for granted that ${\mathbb Q}$ and ${\mathbb N}$ have the same cardinality. Here's a pointer to the relevant Wikipedia section and you'll find counting functions on this site too. Oct 1, 2022 at 6:45