erlang:max/2 and erlang:min/2

Richard O'Keefe <>
Thu Sep 9 06:33:56 CEST 2010


I see that max/2 and min/2 are now in Erlang.
You would expect them to satisfy
	[min(X,Y),max(X,Y)] is a permutation of [X,Y]
	max(X,Y) is max(Y,X)
	min(X,Y) is min(Y,X)
amongst other things.

When X and Y are distinct terms that are equal as numbers,
max(X,Y) and min(X,Y) both return X.

sort3([A,B,C]) ->
    [U,V] = sort2([A,B]),
    [W,Z] = sort2([V,C]),
    [X,Y] = sort2([U,W]),
    [X,Y,Z].

sort2([A,B]) ->
    [min(A,B),max(A,B)].

You'd expect this to sort.  But it doesn't:
> sort3:sort3([0,0.0,-0.0]).
[0,0,0]

It really is very confusing for it to make a difference
whether you write max(X, 0) or max(0, X), but in R14A it does.

The *really* big problem here is that max/2 and min/2 are
(otherwise) based on *term* comparison, and there *can't* be
two identical but distinguishable terms.

In order to get a total order on numbers, it seems necessary
to rule that
	-0.0 < 0 < 0.0
and this extends to a general rule that
	if X and Y are numbers that are numerically equal
	but of different types, the floating point one is
	smaller if its sign bit is set, otherwise the
	floating point one is bigger.

The root of the problem is trying to make one ordering
serve as both a numeric ordering and a term ordering.




More information about the erlang-questions mailing list