abs(1-4) = 3 > 0.001, so val += n(4) should run.
It appears I misread, and thought it was else if, so abs(1-2) = 1 > 0.001 should also run val += n(2);

I'm missing the n(2) term.