Skip to content

Commit f9d114d

Browse files
griesemerRobert Griesemer
authored and
Robert Griesemer
committed
spec: document min and max built-ins
For #59488. Change-Id: I50f65216bf02b42c1e0619702833f4a6dbed8925 Reviewed-on: https://go-review.googlesource.com/c/go/+/498136 Reviewed-by: Rob Pike <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> TryBot-Bypass: Robert Griesemer <[email protected]>
1 parent 01b5cce commit f9d114d

File tree

1 file changed

+67
-2
lines changed

1 file changed

+67
-2
lines changed

doc/go_spec.html

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!--{
22
"Title": "The Go Programming Language Specification",
3-
"Subtitle": "Version of May 24, 2023",
3+
"Subtitle": "Version of May 25, 2023",
44
"Path": "/ref/spec"
55
}-->
66

@@ -643,6 +643,7 @@ <h2 id="Constants">Constants</h2>
643643
a <a href="#Constant_expressions">constant expression</a>,
644644
a <a href="#Conversions">conversion</a> with a result that is a constant, or
645645
the result value of some built-in functions such as
646+
<code>min</code> or <code>max</code> applied to constant arguments,
646647
<code>unsafe.Sizeof</code> applied to <a href="#Package_unsafe">certain values</a>,
647648
<code>cap</code> or <code>len</code> applied to
648649
<a href="#Length_and_capacity">some expressions</a>,
@@ -2319,7 +2320,7 @@ <h3 id="Predeclared_identifiers">Predeclared identifiers</h3>
23192320

23202321
Functions:
23212322
append cap clear close complex copy delete imag len
2322-
make new panic print println real recover
2323+
make max min new panic print println real recover
23232324
</pre>
23242325

23252326
<h3 id="Exported_identifiers">Exported identifiers</h3>
@@ -7531,6 +7532,70 @@ <h3 id="Making_slices_maps_and_channels">Making slices, maps and channels</h3>
75317532
</p>
75327533

75337534

7535+
<h3 id="Min_and_max">Min and max</h3>
7536+
7537+
<p>
7538+
The built-in functions <code>min</code> and <code>max</code> compute the
7539+
smallest&mdash;or largest, respectively&mdash;value of a fixed number of
7540+
arguments of <a href="#Comparison_operators">ordered types</a>.
7541+
There must be at least one argument.
7542+
</p>
7543+
7544+
<p>
7545+
The same type rules as for <a href="#Operators">operators</a> apply:
7546+
for <a href="#Comparison_operators">ordered</a> arguments <code>x</code> and
7547+
<code>y</code>, <code>min(x, y)</code> is valid if <code>x + y</code> is valid,
7548+
and the type of <code>min(x, y)</code> is the type of <code>x + y</code>
7549+
(and similarly for <code>max</code>).
7550+
If all arguments are constant, the result is constant.
7551+
</p>
7552+
7553+
<pre>
7554+
var x, y int
7555+
m := min(x) // m == x
7556+
m := min(x, y) // m is the smaller of x and y
7557+
m := max(x, y, 10) // m is the larger of x and y but at least 10
7558+
c := max(1, 2.0, 10) // c == 10.0 (floating-point kind)
7559+
f := max(0, float32(x)) // type of f is float32
7560+
var s []string
7561+
_ = min(s...) // invalid: slice arguments are not permitted
7562+
t := max("", "foo", "bar") // t == "foo" (string kind)
7563+
</pre>
7564+
7565+
<p>
7566+
For numeric arguments, <code>min</code> and <code>max</code> are
7567+
commutative and associative:
7568+
</p>
7569+
7570+
<pre>
7571+
min(x, y) == min(y, x)
7572+
min(x, y, z) == min(min(x, y), z) == min(x, min(y, z))
7573+
</pre>
7574+
7575+
<p>
7576+
For floating-point arguments negative zero, NaN, and infinity the following rules apply:
7577+
</p>
7578+
7579+
<pre>
7580+
x y min(x, y) max(x, y)
7581+
7582+
-0.0 0.0 -0.0 0.0 // negative zero is smaller than (non-negative) zero
7583+
-Inf y -Inf y // negative infinity is smaller than any other number
7584+
+Inf y y +Inf // positive infinity is larger than any other number
7585+
NaN y NaN NaN // if any argument is a NaN, the result is a NaN
7586+
</pre>
7587+
7588+
<p>
7589+
For string arguments the result for <code>min</code> is the first argument
7590+
with the smallest (or for <code>max</code>, largest) value,
7591+
compared lexically byte-wise:
7592+
</p>
7593+
7594+
<pre>
7595+
min(x, y) == if x <= y then x else y
7596+
min(x, y, z) == min(min(x, y), z)
7597+
</pre>
7598+
75347599
<h3 id="Allocation">Allocation</h3>
75357600

75367601
<p>

0 commit comments

Comments
 (0)