To carry out a projection operation, the best paper out there that I know of discussing the advantages and disadvantages is:
Stewart, G. W. On the Numerical Analysis of Oblique Projectors, SIMAX (2011), Vol. 32, Issue 1, p. 309-348.
For orthogonal projectors, naïvely constructing and applying the projection matrix is fine because the condition number of that operation is the norm of the projection matrix (which, for an orthogonal projector, is 1).
The most numerically stable way of constructing such a projector that I know of is to take $n$ and perform an SVD of it. The left singular vectors corresponding to zero singular values (I suppose, technically, there are no singular values corresponding to those vectors, since there will only be one singular value, and it will be nonzero...) will be the basis for the orthogonal complement of the span of $n$; let the matrix of these singular values be $U$. Then the projector you're looking for is $P = UU^{H}$, and you can calculate the vector you want one of two ways:
- Let $w = Pv$, and then $f(v) = w / \|w\|$, unless $w = 0$, in which case $f(v) = 0$.
- Calculate $x = U^{H}v$, then $w = Ux$, and $f(v) = w / \|w\|$, unless $w = 0$, in which case $f(v) = 0$.
Based on Stewart's comments in the paper on oblique projectors, method 1 or method 2 should give you roughly the same amount of accuracy.
You can also calculate your projector using QR instead of SVD, but SVD is more numerically stable.
EDIT: The below method also works:
3) Calculate $x = U^{H}v$, then set $w = x / \|x\|$ (unless $x = 0$, then $w = 0$). Finally, $f(v) = Uw$.
Essentially, the argument is that $\|w\|$ and $\|f(v)\|$ are the same because $U$ is a partial isometry. $f(v)$ is guaranteed to be in the span of the columns of $U$ (to within numerical error), and division by $\|x\|$ could amplify error in this subspace, as opposed to methods 1 and 2, where division by $\|w\|$ could amplify error in all of $\mathbb{R}^{n}$, including the normal vector, $n$.