"Least trivial" function preserving rationality

Is there a "non-trivial" function $f(x,y)$ such that

$$f(x,y) \in \mathbb{Q} \iff x,y\in \mathbb{Q}?$$

An example of a "trivial" function would be

$$f(x,y) = \begin{cases} 0 & x,y\in \mathbb{Q}\\ \pi & \text{otherwise} \end{cases}$$ or any other $f$ which effectively uses a cases function.

The motivation is just my curiosity. Obviously, operations which preserve one direction of the $\iff$ are plentiful and well-studied. I was wondering how onerous the condition of the additional direction is on the choice of $f$. This question on mathoverflow seems related.


Solution 1:

Perhaps a slightly less trivial example would be the function $f$ that interleaves the decimal digits of $x$ and $y$: that is, if \begin{align} x&=0.x_1x_2x_3\dots \\ y&=0.y_1y_2y_3\dots \end{align} are decimal expansions of $x$ and $y$, then $$ f(x,y)=0.x_1y_1x_2y_2x_3y_3\dots $$

You can make this definition unambiguous by deciding to always (or never) take finite decimal expansions when they are available.

Then $f(x,y)$ has a repeating decimal expansion if and only if both $x$ and $y$ do, and so it satisfies your condition.

Solution 2:

This is an interesting question.

The answer would depend on how the functions are defined. My inclination is to say that the answer is "no", because irrationals can be made to cancel out to give a rational.

However, if limiting processes can be used, perhaps a function that uses the fact that rationals can not be well-approximated by rationals (error in approximation by $a/b$ at best $\Omega(1/b)$) and irrationals can (error in approximation by $a/b$ at worst $O(1/b^2)$) could be constructed.