mirror of
https://github.com/golang/go
synced 2024-09-15 22:20:06 +00:00
spec: document numeric operations behavior for generic types
Includes a few minor cosmetic changes. Change-Id: I6c307d958b47d83671142688630ea7835168439f Reviewed-on: https://go-review.googlesource.com/c/go/+/384622 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
parent
8ba3ad92eb
commit
3b8c716e0f
|
@ -1,6 +1,6 @@
|
||||||
<!--{
|
<!--{
|
||||||
"Title": "The Go Programming Language Specification - Go 1.18 Draft (incomplete)",
|
"Title": "The Go Programming Language Specification - Go 1.18 Draft (incomplete)",
|
||||||
"Subtitle": "Version of Feb 9, 2022",
|
"Subtitle": "Version of Feb 10, 2022",
|
||||||
"Path": "/ref/spec"
|
"Path": "/ref/spec"
|
||||||
}-->
|
}-->
|
||||||
|
|
||||||
|
@ -4752,7 +4752,7 @@ Arithmetic operators apply to numeric values and yield a result of the same
|
||||||
type as the first operand. The four standard arithmetic operators (<code>+</code>,
|
type as the first operand. The four standard arithmetic operators (<code>+</code>,
|
||||||
<code>-</code>, <code>*</code>, <code>/</code>) apply to
|
<code>-</code>, <code>*</code>, <code>/</code>) apply to
|
||||||
<a href="#Numeric_types">integer</a>, <a href="#Numeric_types">floating-point</a>, and
|
<a href="#Numeric_types">integer</a>, <a href="#Numeric_types">floating-point</a>, and
|
||||||
<a href="#Numeric_types">complex</a> types; <code>+</code> also applies to <a href="#String_types">strings</.
|
<a href="#Numeric_types">complex</a> types; <code>+</code> also applies to <a href="#String_types">strings</a>.
|
||||||
The bitwise logical and shift operators apply to integers only.
|
The bitwise logical and shift operators apply to integers only.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
@ -4772,6 +4772,37 @@ The bitwise logical and shift operators apply to integers only.
|
||||||
>> right shift integer >> integer >= 0
|
>> right shift integer >> integer >= 0
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
Excluding shifts, if the operand type is a <a href="#Type_parameters">type parameter</a>,
|
||||||
|
it must have <a href="#Structure_of_interfaces">specific types</a>, and the operator must
|
||||||
|
apply to each specific type.
|
||||||
|
The operands are represented as values of the type argument that the type parameter
|
||||||
|
is <a href="#Instantiations">instantiated</a> with, and the operation is computed
|
||||||
|
with the precision of that type argument. For example, given the function:
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
func dotProduct[F ~float32|~float64](v1, v2 []F) F {
|
||||||
|
var s F
|
||||||
|
for i, x := range v1 {
|
||||||
|
y := v2[i]
|
||||||
|
s += x * y
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
the the product <code>x * y</code> and the addition <code>s += x * y</code>
|
||||||
|
are computed with <code>float32</code> or <code>float64</code> precision,
|
||||||
|
respectively, depending on the type argument for <code>F</code>.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
For shifts, the <a href="#Core_types">core type</a> of both operands must be
|
||||||
|
an integer.
|
||||||
|
</p>
|
||||||
|
|
||||||
<h4 id="Integer_operators">Integer operators</h4>
|
<h4 id="Integer_operators">Integer operators</h4>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
|
@ -4857,10 +4888,10 @@ follows:
|
||||||
<h4 id="Integer_overflow">Integer overflow</h4>
|
<h4 id="Integer_overflow">Integer overflow</h4>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
For unsigned integer values, the operations <code>+</code>,
|
For <a href="#Numeric_types">unsigned integer</a> values, the operations <code>+</code>,
|
||||||
<code>-</code>, <code>*</code>, and <code><<</code> are
|
<code>-</code>, <code>*</code>, and <code><<</code> are
|
||||||
computed modulo 2<sup><i>n</i></sup>, where <i>n</i> is the bit width of
|
computed modulo 2<sup><i>n</i></sup>, where <i>n</i> is the bit width of
|
||||||
the <a href="#Numeric_types">unsigned integer</a>'s type.
|
the unsigned integer's type.
|
||||||
Loosely speaking, these unsigned integer operations
|
Loosely speaking, these unsigned integer operations
|
||||||
discard high bits upon overflow, and programs may rely on "wrap around".
|
discard high bits upon overflow, and programs may rely on "wrap around".
|
||||||
</p>
|
</p>
|
||||||
|
@ -4875,7 +4906,6 @@ A compiler may not optimize code under the assumption that overflow does
|
||||||
not occur. For instance, it may not assume that <code>x < x + 1</code> is always true.
|
not occur. For instance, it may not assume that <code>x < x + 1</code> is always true.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
|
||||||
<h4 id="Floating_point_operators">Floating-point operators</h4>
|
<h4 id="Floating_point_operators">Floating-point operators</h4>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
|
@ -4931,7 +4961,6 @@ s += " and good bye"
|
||||||
String addition creates a new string by concatenating the operands.
|
String addition creates a new string by concatenating the operands.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
|
||||||
<h3 id="Comparison_operators">Comparison operators</h3>
|
<h3 id="Comparison_operators">Comparison operators</h3>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
|
@ -5220,7 +5249,7 @@ string(65.0) // illegal: 65.0 is not an integer constant
|
||||||
<p>
|
<p>
|
||||||
Converting a constant to a type parameter yields a <i>non-constant</i> value of that type,
|
Converting a constant to a type parameter yields a <i>non-constant</i> value of that type,
|
||||||
with the value represented as a value of the type argument that the type parameter
|
with the value represented as a value of the type argument that the type parameter
|
||||||
is instantiated with.
|
is <a href="#Instantiations">instantiated</a> with.
|
||||||
For example, given the function:
|
For example, given the function:
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue