0
$\begingroup$

I am writing a function that can be split into cases

(1)$f(x) \leq f(y)$ and $f(x) \geq f(y)$

or into cases

(2)$f(x) < f(y), f(x) = f(y), f(x) > f(y)$

If I have f(x) = f(y), it is theoretically more efficient to use the cases outlined in (2), but is it actually more efficient in practice?

My concerns are that if $f(x) = f(y)$ theoretically, how often will they also be equal computationally? and if it happens that $f(x) \neq f(y)$ theoretically, how often will they be equal computationally? (As this would cause the algoritm to produce a wrong answer).

Note that if $f(x) = f(y)$ theoretically, but computationally are not equal, the algorithm will still produce a correct final answer - just will reach it slightly slower.

$\endgroup$
3
  • $\begingroup$ It depends on the exact details, but in floating point computations, due to rounding errors it is usually only the case that $f(x) = f(y)$ in special circumstances. $\endgroup$ – Yuval Filmus Dec 16 '17 at 17:33
  • $\begingroup$ So would I be right in saying that $f(x) = f(y)$ computationally and correctly is rare enough that it's probably not worthwhile to treat it as a separate case? $\endgroup$ – KingJ Dec 16 '17 at 17:38
  • $\begingroup$ It depends on the circumstances. $\endgroup$ – Yuval Filmus Dec 16 '17 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.