# Comparing sum of floating points

I am currently working on a numerical algorithm involving a lot of floating point arithmetic, involving some badly conditioned problem sets.

I am using the relation $$|x - y| / (\max(|x|, |y|, 1)) \leq \epsilon$$. Unfortunately, the corresponding relation $$=\sim$$ is not invariant with respect to additions, i.e. $$x =\sim y$$ does not imply $$x - y =\sim 0$$.

As a counter example I have $$x = 1e+20$$, $$y = 1.0000000000000082e+20$$, and therefore $$x - y = -819200$$ (wrt. floating point arithmetic). I think that this is a pretty typical case of cancellation...

My questions are:

• Is there a suitable relation?
• How should I compare $$x + y =\sim z$$? I am using the statement in an assertion which consequently fails on certain instances. Is it advisable to use ($$x + y =\sim z$$ or $$x =\sim z - y$$) instead?

Edit:

What am I trying to achieve: I am implementing a solver for nonlinear programming problems, and running the implementation on the cutest suite of test problems. In the specific step, I am asserting that

$$c(x) + J(x)d = \sim 0,$$

where $$c(x)$$ is a vector of constraint values, and $$J(x)$$ is the constraint Jacobian. The problems in the test set vary widely in terms of size and type. Therefore, I'm afraid that there is not much specific knowledge.

What is more, the test suite is known to be badly scaled, i.e. containing constraints of the type $$1000*x^2 + 0.001 * y^2$$. Therefore, the Jacobian is likely ill-conditioned.

• (a) You're using relative errors, and only zero is relatively close to zero. (b) You lost scaling invariance in $\max(1,|x|,|y|)$. Comparing floating-point numbers for approximate equality is actually quite tricky, and usually people do it by using domain-specific knowledge to determine what tolerances to use. Could you maybe describe in a bit more mathematical detail what it is precisely that you're trying to achieve? – Kirill Dec 10 '18 at 11:31
• @Kirill: OK, I edited the question accordingly. – hfhc2 Dec 10 '18 at 12:56