From: Robert Griesemer Date: Wed, 29 Jul 2015 22:42:04 +0000 (-0700) Subject: spec: clarify semantics of built-in functions 'complex', 'real', and 'imag' X-Git-Tag: go1.5rc1~8 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=98aa82287f7a06b2c12884b062c87bc3c18b66ca;p=gostls13.git spec: clarify semantics of built-in functions 'complex', 'real', and 'imag' For #11669, #11540, #11945, #11946, #11947. Change-Id: Ifb0053c498cee9f3473c396f9338d82bd856c110 Reviewed-on: https://go-review.googlesource.com/12860 Reviewed-by: Russ Cox Reviewed-by: Rob Pike --- diff --git a/doc/go_spec.html b/doc/go_spec.html index d186e5948d..22f9701a75 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,6 +1,6 @@ @@ -5688,11 +5688,28 @@ The type of the arguments and return value correspond. For complex, the two arguments must be of the same floating-point type and the return type is the complex type with the corresponding floating-point constituents: -complex64 for float32, -complex128 for float64. -The real and imag functions -together form the inverse, so for a complex value z, -z == complex(real(z), imag(z)). +complex64 for float32 arguments, and +complex128 for float64 arguments. +If one of the arguments evaluates to an untyped constant, it is first +converted to the type of the other argument. +If both arguments evaluate to untyped constants, they must be non-complex +numbers or their imaginary parts must be zero, and the return value of +the function is an untyped complex constant. +

+ +

+For real and imag, the argument must be +of complex type, and the return type is the corresponding floating-point +type: float32 for a complex64 argument, and +float64 for a complex128 argument. +If the argument evaluates to an untyped constant, it must be a number, +and the return value of the function is an untyped floating-point constant. +

+ +

+The real and imag functions together form the inverse of +complex, so for a value z of a complex type Z, +z == Z(complex(real(z), imag(z))).

@@ -5702,11 +5719,15 @@ value is a constant.

 var a = complex(2, -2)             // complex128
-var b = complex(1.0, -1.4)         // complex128
+const b = complex(1.0, -1.4)       // untyped complex constant 1 - 1.4i
 x := float32(math.Cos(math.Pi/2))  // float32
 var c64 = complex(5, -x)           // complex64
-var im = imag(b)                   // float64
+const s uint = complex(1, 0)       // untyped complex constant 1 + 0i can be converted to uint
+_ = complex(1, 2<<s)               // illegal: 2 has floating-point type, cannot shift
 var rl = real(c64)                 // float32
+var im = imag(a)                   // float64
+const c = imag(b)                  // untyped constant -1.4
+_ = imag(3 << s)                   // illegal: 3 has complex type, cannot shift
 

Handling panics