Re: Why no min / max



On Nov 26, 10:54 am, Eric Sosman <esos...@xxxxxxxxxxxxxxxxxxxx> wrote:
On 11/26/2010 10:12 AM, Noob wrote:



As far as I can tell, the standard library does not provide
functions (or macros) to compute the minimum and maximum of
two values, be they integral values, or floating point values;
leaving it up to each programmer in the world to roll their own.
Is this correct?

     No; you've overlooked the fmax(), fmin(), fmaxf(), fminf(),
fmaxl(), and fminl() functions.

What is the rationale for this omission in C89? in C99?

     Mu.

Is it because it is "simple" to implement as a macro?
Something along the lines of #define MIN(a,b) (((a)<(b))?(a):(b))

     With the usual caveats about macros and side-effects, this is
a reasonable implementation for many cases.  Note that it is *not*
reasonable for a system supporting floating-point NaN's: MIN(NaN,42)
could yield 42, while MIN(42,NaN) could yield NaN.  I think most
people would find a non-commutative MIN surprising ...


Simple just do MIN(MIN(x,y),MIN(y,x)).

Solved.

Tom
.


Quantcast