Home computickets What other fundamentally different ways to implement MIN and MAX functions are...

What other fundamentally different ways to implement MIN and MAX functions are you known?




To prepare a video tracking on MIN and MAX functions, I would like to learn from the community about fundamentally different ways to implement these functions that I do not know about. Fundamentally different are those who differ in their logical meaning, and not by implementation. I will write what I know, and in the answers I ask you to write what I could miss. Let us consider only the iconic numbers, with unsigned general principles the same, but the formulas are a little others.

first : normal comparison:

max = a & gt; b? A: B;
MIN = A & LT; B? A: B;

Second : First, determine the function DOZ (A, B), which is A-B, if A & GT; = B, and zero otherwise. This feature can be implemented in many ways:

doz = a & gt; = b? A-B: 0;


doz = (a-b) & amp ;-( a & gt; = b)


d = a-b;
DOZ = (D & amp; (~ (d ^ ((a ^ b) & amp; (d ^ a))) & gt; & gt; 31));

Next, just write:

max = b + doz (a, b);
MIN = A - DOZ (A, B);

or, combining ideas:

max = ((a ^ b) & amp ;-( a & gt; = b)) ^ b;
MIN = ((A ^ b) & amp ;-( a & lt; = b)) ^ b;

Third : if we know exactly that in intermediate calculations there will be no overflow or a lean, then we consider DOZ so:

doz = (a-b) & gt; ~ ((a-b) & gt; & gt; 31);

At the same time, MIN and MAX consider it through DOZ as it was higher.

fourth : also assume that the difference and amount do not overflow the variables:

max = (a + b) / 2 + abs (A-B) / 2;
MIN = (A + B) / 2 - ABS (A-B) / 2;

At the same time, the ABS feature we sell the most in different ways (several options can be drawn here ), from this The idea does not change.

It’s all that I know (I hope I did not allow typos). Are there any other ways that differ from these fundamentally, then there is no permutation of operations or replace one actions on similar (otherwise you can launch a dozen formulas), namely the idea itself? So I ask you to share.

Answer 1, Authority 100%

All methods except the first – extremely unsuccessful ideas.

The fact is that the min and max function are defined on any Linearly ordered sets – and can be applied to them. In other words, in order for the min and max functions, one comparison operator is enough.

All alternative methods use additional operators – and therefore cannot be applied on those sets where these operations are not defined.

Such tasks occur more often than you seem to:

  1. strings. The strings define a lexicographic order – but the lines can not be folded and subtracted. Even if you determine addition as a concatenation – it does not have those properties in relation to lexicographic order.

  2. real numbers. The implementation of MIN / MAX based on the comparison does not lose accuracy. Implementation through addition and subtraction lead to loss of accuracy. Bit operations are not defined.

  3. structures. For structures, you can define the operations “minby / maxby” – finding a minimum or a maximum of some field. At the same time, to fold or deduct the structure of the entire structure may not work (for example, if in another field – a string or array).

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions