[comp.lang.c] min and max

karl@haddock.ISC.COM (Karl Heuer) (01/14/88)

In article <518@cresswell.quintus.UUCP> ok@quintus.UUCP (Richard A. O'Keefe) writes:
>In article <2197@haddock.ISC.COM>, karl@haddock.ISC.COM (Karl Heuer) writes:
>> Actually, I'd give a higher wish-priority to min and max operators.  They
>> could have used "><" and "<>", respectively; the syntax would drive BASIC
>> and PASCAL users crazy!  :-)
>
>Some years ago the C compiler I used *did* have min (spelled "/\")
>and max (spelled "\/") operators.  These symbols are ever so much better
>because they are standard mathematical notation.

Okay, as long as you're careful not to put "/\" at the end of a line.
(ANSI Backslash continuation occurs before token scanning!)

>["lo \/ a /\ hi" is likely to generate less code than the macros would.]

Right.  One thing I like about that idiom is that, assuming lo <= hi, the
relative precedence/associativity of \/ and /\ is irrelevant.  In addition to
the usual min() and max() functions, I keep a med() function in my library:
med(lo, x, hi) returns lo \/ x /\ hi if lo <= hi; undefined if lo > hi.  (I
could easily remove the lo <= hi restriction and have it always return the
median of the three numbers, but I've never needed that functionality and I
wanted to keep the implementation simple.)

Of course, the assignment operators /\= and \/= would be great.  Think of all
the times you've ever written "if (x > xmax) xmax = x;".

>Do I think it would be a good idea to add the to the standard?  No.

Despite my posted enthusiasm for these and the exponentiation operator, I must
agree.  There isn't enough prior art.  I would like to see it become a common
extension, though.

What's the appropriate precedence for these operators?  (What was it on those
compilers that had this extension?)

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint
(Btw, I believe "><" and "<>" are used in SAS for min and max operators.)

john@viper.Lynx.MN.Org (John Stanley) (01/19/88)

In article <2254@haddock.ISC.COM> karl@haddock.ima.isc.com (Karl Heuer) writes:
 >
 >Okay, as long as you're careful not to put "/\" at the end of a line.
 >(ANSI Backslash continuation occurs before token scanning!)

  For this reason, and that \/ is going to get converted to / in too
many cases, I doubt this notation will ever be added.  Note that I
said "notation"..  I would -love- to have the functionality and I
still think it can be done.  How about #< (numerical lesser) or
#> (numerical greater) for min and max?  Last time I looked '#' wasn't
being used for any C operator....


 >Of course, the assignment operators /\= and \/= would be great.  Think of all
 >the times you've ever written "if (x > xmax) xmax = x;".

  Far, far too many times...  Yes, it would be VERY nice to have a
min/max operator for things like this.  It's such an integral part of
things we do all the time there really should be some easier way to
express it within the language.
 

 >What's the appropriate precedence for these operators?  (What was it on those
 >compilers that had this extension?)

  The logical place for a min/max operator would be after <<  >>
and just before the logical <, >, <=, >= operators.


--- 
John Stanley (john@viper.UUCP)
Software Consultant - DynaSoft Systems
UUCP: ...{amdahl,ihnp4,rutgers}!meccts!viper!john

ok@quintus.UUCP (Richard A. O'Keefe) (01/20/88)

In article <515@viper.Lynx.MN.Org>, john@viper.Lynx.MN.Org (John Stanley) writes:
> In article <2254@haddock.ISC.COM> karl@haddock.ima.isc.com (Karl Heuer) writes:
>  >What's the appropriate precedence for these operators?  (What was it on those
>  >compilers that had this extension?)
> 
>   The logical place for a min/max operator would be after <<  >>
> and just before the logical <, >, <=, >= operators.
> 
That's exactly where the compiler I used that had them put them.
And since /\ is like "and" and \/ is like "or", /\ binds more
tightly than \/.

Note that /\, \/, and - give you one of the more useful 3-valued logics.
{With << tighter-than /\ tighter-than \/ tighter-than <, - is already in
the right place for use as the denial operator in this logic.}
If you treat > 0 as true, < = as false, and = 0 as unknown,
#define THREEVALUED(x) ((!!(X)<<1)-1)

	/\  |   T    U    F	\/  |   T    U    F	-   |
	----|--------------	----+--------------	----+----
	T   |   T    U	  F	T   |   T    T    T	T   |   F
	U   |   U    U    F	U   |   T    U    U     U   |   U
	F   |   F    F    F	F   |   T    U    F     F   |   T

/\ and \/ are the standard mathematical symbols (albeit as single glyphs
rather than digraphs) for minimum and maximum.  On a related subject,
the DIS 8859/1 character set includes the times (x) and divide (-:-)
symbols as single characters.  Will it be legal for an otherwise ANSI
conforming C compiler to allow these as multiplication and division
operators?

karl@haddock.ISC.COM (Karl Heuer) (01/20/88)

In article <515@viper.Lynx.MN.Org> john@viper.UUCP (John Stanley) writes:
>In article <2254@haddock.ISC.COM> karl@haddock.ima.isc.com (Karl Heuer) writes:
> >[notes problems with backslashes in "/\" "\/" syntax]
>
>How about #< (numerical lesser) or #> (numerical greater) for min and max?
>Last time I looked '#' wasn't being used for any C operator....

In ANSI C, it's used for certain preprocessor operators.  It might be wise to
reserve that character.  How about "<|" and "|>" instead?  (I'm assuming that
my earlier suggestion ("><" and "<>") is too confusing.)

> >What's the appropriate precedence for these operators?
>
>The logical place for a min/max operator would be after <<  >>
>and just before the logical <, >, <=, >= operators.

Certainly it should be above the relationals.  It might be a good idea to
avoid creating too many levels, though; perhaps it should have the same
precedence as the shifts.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint

kers@otter.HP.COM (Christopher Dollin) (01/20/88)

What's wrong with "max" and "min" as the max and min operators? Or is it an
inviolate C tradition that whatever-fix operators must be built up of sign 
characters?

I suppose some would say "breaks existing code!". Seems to me that various bits
of existing code will be broken anyway. Why not take the opportunity?


Regards,
Kers                                    | "Why Lisp if you can talk Poperly?"

franka@mmintl.UUCP (01/23/88)

How about ?< for min, and ?> for max?
-- 

Frank Adams                           ihnp4!philabs!pwa-b!mmintl!franka
Ashton-Tate          52 Oakland Ave North         E. Hartford, CT 06108

g-rh@cca.UUCP (01/24/88)

One more time around on this very dead horse (no wonder we don't get
anywhere).  FORTRAN has the notion of routines which are defined as part
of the language.  The advantage is that the compiler can implement these
routines as convenient.  Also one can use the general notion of a function
rather than introducing still more obscure symbols.

Suppose that min(a,b) and max(a,b) are defined with the following properties:
a and b must be of the same type; the result is of the same type as a and b;
and if a and b are the same expression, only one evaluation is done.

Note that these typing rules rule out user supplied substitute functions --
a user written function wouldn't know what types to use.

It seems to me that C has about reached the limits of using special symbols
for operators; if new operators are warranted, the function notation might
be better.

Just a thought.
-- 

In the fields of Hell where the grass grows high
Are the graves of dreams allowed to die.
	Richard Harter, SMDS  Inc.