Is it possible to compute the minimum of three numbers by using two comparisons at the same time?
Posted
by
Milo Hou
on Stack Overflow
See other posts from Stack Overflow
or by Milo Hou
Published on 2013-11-06T20:36:58Z
Indexed on
2013/11/06
21:54 UTC
Read the original article
Hit count: 198
algorithm
|comparison
I've been trying to think up of some way that I could do two comparisons at the same time to find the greatest/least of three numbers. Arithmetic operations on them are considered "free" in this case.
That is to say, the classical way of finding the greater of two, and then comparing it to the third number isn't valid in this case because one comparison depends on the result of the other.
Is it possible to use two comparisons where this isn't the case? I was thinking maybe comparing the differences of the numbers somehow or their products or something, but came up with nothing.
Just to reemphasize, two comparisons are still done, just that neither comparison relies on the result of the other comparison.
EDIT: what about:
boolA = A^2 + B^2 < C^2
boolB = A > B
if boolA then max=C else if boolB then max=A else max=B
© Stack Overflow or respective owner