In my real analysis book, when they show that the rational numbers does not have the least upper bound property, they show that the set $\{r \in \mathbb Q : r>0 \text{ and } r^2 < 2\}$ is non-empty and bounded above, but does not have a least upper bound. I understand the argument fully, but I'm having trouble understanding where they came up with some of it. Their argument goes as follows
Define $B = \{r \in Q : r>0 \text{ and } r^2 < 2\}$. Suppose $p \in B$. Define the rational number $q$ by
$$q = p + \frac{2-p^2}{p+2} = \frac{2p+2}{p+2}$$
Then we have
$$q^2-2 = \frac{2(p^2-2)}{(p+2)^2}$$
I understand that this shows for any $p \in B$ there is a $q \in B$ such that $p<q$. My question is why did they decide to define $q$ this way? Where is the logic for choosing $q$ to be that expression?
Thank you.