Maximum and Minimum without comparisons

Discussions related to mathematics, numerical methods, graph plotting etc.
Richard Russell
Posts: 272
Joined: Tue 18 Jun 2024, 09:32

Maximum and Minimum without comparisons

Post by Richard Russell »

This is probably well known to everybody except me, but I've only recently encountered these functions to find the maximum and minimum of two numeric values without any comparison operators (less-than, greater-than etc.) being needed at all:

Code: Select all

      DEF FNmax(a,b)=(a+b+ABS(a-b))/2
      DEF FNmin(a,b)=(a+b-ABS(a-b))/2
Admittedly they are a little slower than the naïve approach using comparisons, but may nevertheless be of interest.