max, min
max, min Function
Syntax:
max &rest reals+ → max-real
max, minmin &rest reals+ → min-real
Arguments and Values:
real—a real.
max-real, min-real—a real.
Description:
max returns the real that is greatest (closest to positive infinity). min returns the real that is least (closest to negative infinity).
For max, the implementation has the choice of returning the largest argument as is or applying the rules of floating-point contagion, taking all the arguments into consideration for contagion purposes. Also, if one or more of the arguments are =, then any one of them may be chosen as the value to return. For example, if the reals are a mixture of rationals and floats, and the largest argument is a rational, then the implementation is free to produce either that rational or its float approximation; if the largest argument is a float of a smaller format than the largest format of any float argument, then the implementation is free to return the argument in its given format or expanded to the larger format. Similar remarks apply to min (replacing “largest argument” by “smallest argument”).
Examples:
(max 3) → 3
(min 3) → 3
(max 6 12) → 12
(min 6 12) → 6
(max -6 -12) → -6
(min -6 -12) → -12
(max 1 3 2 -7) → 3
(min 1 3 2 -7) → -7
(max -2 3 0 7) → 7
(min -2 3 0 7) → -2
(max 5.0 2) → 5.0
(min 5.0 2)
→ 2
<i><sup>or</sup>→</i> 2.0
(max 3.0 7 1)
→ 7
<i><sup>or</sup>→</i> 7.0
(min 3.0 7 1)
→ 1
<i><sup>or</sup>→</i> 1.0
(max 1.0s0 7.0d0) → 7.0d0
(min 1.0s0 7.0d0)
→ 1.0s0
<i><sup>or</sup>→</i> 1.0d0
(max 3 1 1.0s0 1.0d0)
→ 3
<i><sup>or</sup>→</i> 3.0d0
(min 3 1 1.0s0 1.0d0)
→ 1
<i><sup>or</sup>→</i> 1.0s0
<i><sup>or</sup>→</i> 1.0d0
Exceptional Situations:
Should signal an error of type type-error if any number is not a real.
Expanded Reference: max, min
Basic usage
max returns the largest of its arguments. min returns the smallest. At least one argument is required.
(max 3)
=> 3
(min 3)
=> 3
(max 6 12)
=> 12
(min 6 12)
=> 6
Multiple arguments
Both functions accept any number of real arguments.
(max 1 3 2 -7)
=> 3
(min 1 3 2 -7)
=> -7
(max -2 3 0 7)
=> 7
(min -2 3 0 7)
=> -2
Negative numbers
max and min work correctly with negative numbers.
(max -6 -12)
=> -6
(min -6 -12)
=> -12
(max -1 -2 -3 -4)
=> -1
(min -1 -2 -3 -4)
=> -4
Floating-point contagion
When arguments include floats, the result may or may not be converted to a float, depending on the implementation.
(max 5.0 2)
=> 5.0
(max 1.0s0 7.0d0)
=> 7.0d0
Practical use: clamping a value to a range
max and min can be combined to clamp a value within a given range.
(defun clamp (value low high)
(max low (min value high)))
(clamp 5 0 10)
=> 5
(clamp -3 0 10)
=> 0
(clamp 15 0 10)
=> 10
Finding extremes in a list
Use apply or reduce to find the maximum or minimum of a list.
(apply #'max '(4 7 2 9 1))
=> 9
(reduce #'min '(4 7 2 9 1))
=> 1