diff options
author | Robert Griesemer <gri@golang.org> | 2022-02-09 15:40:20 -0800 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2022-02-10 21:40:12 +0000 |
commit | 3b8c716e0fb897f867cabd2aeeb77b2b8ccd2241 (patch) | |
tree | dfb808b1d905956e74b8f4614c18dd15a62c929a /doc | |
parent | 8ba3ad92ebd38a0d41c96dda7ccb5d650236d3c6 (diff) | |
download | go-git-3b8c716e0fb897f867cabd2aeeb77b2b8ccd2241.tar.gz |
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>
Diffstat (limited to 'doc')
-rw-r--r-- | doc/go_spec.html | 43 |
1 files changed, 36 insertions, 7 deletions
diff --git a/doc/go_spec.html b/doc/go_spec.html index c0ed27730f..751d7fea01 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,6 +1,6 @@ <!--{ "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" }--> @@ -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>, <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">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. </p> @@ -4772,6 +4772,37 @@ The bitwise logical and shift operators apply to integers only. >> right shift integer >> integer >= 0 </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> <p> @@ -4857,10 +4888,10 @@ follows: <h4 id="Integer_overflow">Integer overflow</h4> <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 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 discard high bits upon overflow, and programs may rely on "wrap around". </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. </p> - <h4 id="Floating_point_operators">Floating-point operators</h4> <p> @@ -4931,7 +4961,6 @@ s += " and good bye" String addition creates a new string by concatenating the operands. </p> - <h3 id="Comparison_operators">Comparison operators</h3> <p> @@ -5220,7 +5249,7 @@ string(65.0) // illegal: 65.0 is not an integer constant <p> 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 -is instantiated with. +is <a href="#Instantiations">instantiated</a> with. For example, given the function: </p> |