From 72970871016de9aadee5aa8eafcfa15fa59813ca Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Thu, 4 Mar 2010 12:35:16 -0800 Subject: [PATCH] Spec for complex numbers R=rsc, ken2, gri, iant CC=cw, golang-dev https://golang.org/cl/227041 --- doc/go_spec.html | 188 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 153 insertions(+), 35 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index bcde026723f..d95d6492592 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -4,6 +4,8 @@ Todo [ ] clarify: two equal lowercase identifiers from different packages denote different objects [ ] need language about function/method calls and parameter passing rules +[ ] last paragraph of #Assignments (constant promotion) should be elsewhere + and mention assignment to empty interface. [ ] need to say something about "scope" of selectors? [ ] clarify what a field name is in struct declarations (struct{T} vs struct {T T} vs struct {t T}) @@ -301,6 +303,8 @@ exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
 0.
+72.40
+072.40  // == 72.40
 2.71828
 1.e+0
 6.67428e-11
@@ -309,6 +313,31 @@ exponent  = ( "e" | "E" ) [ "+" | "-" ] decimals .
 .12345E+5
 
+

Imaginary literals

+

+An imaginary literal is a decimal representation of the imaginary part of a +complex constant. +It consists of a +floating-point literal +or decimal integer followed +by the lower-case letter i. +

+
+imaginary_lit = (decimals | float_lit) "i" .
+
+ +
+0i
+011i  // == 11i
+0.i
+2.71828i
+1.e+0i
+6.67428e-11i
+1E6i
+.25i
+.12345E+5i
+
+

Character literals

@@ -465,8 +494,10 @@ literal.

Constants

-

There are boolean constants, integer constants, floating-point constants, -and string constants. Integer and floating-point constants are +

There are boolean constants, integer constants, +floating-point constants, complex constants, +and string constants. Integer, floating-point, +and complex constants are collectively called numeric constants.

@@ -474,18 +505,27 @@ collectively called numeric constants. A constant value is represented by an integer, floating-point, +imaginary, character, or string literal, an identifier denoting a constant, a constant expression, or the result value of some built-in functions such as unsafe.Sizeof and cap or len applied to an array, -or len applied to a string constant. +len applied to a string constant, +real and imag applied to a complex constant +and cmplx applied to numeric constants. The boolean truth values are represented by the predeclared constants true and false. The predeclared identifier iota denotes an integer constant.

+

+In general, complex constants are a form of +constant expression +and are discussed in that section. +

+

Numeric constants represent values of arbitrary precision and do not overflow.

@@ -590,18 +630,21 @@ The predeclared architecture-independent numeric types are:

-uint8    the set of all unsigned  8-bit integers (0 to 255)
-uint16   the set of all unsigned 16-bit integers (0 to 65535)
-uint32   the set of all unsigned 32-bit integers (0 to 4294967295)
-uint64   the set of all unsigned 64-bit integers (0 to 18446744073709551615)
+uint8       the set of all unsigned  8-bit integers (0 to 255)
+uint16      the set of all unsigned 16-bit integers (0 to 65535)
+uint32      the set of all unsigned 32-bit integers (0 to 4294967295)
+uint64      the set of all unsigned 64-bit integers (0 to 18446744073709551615)
 
-int8     the set of all signed  8-bit integers (-128 to 127)
-int16    the set of all signed 16-bit integers (-32768 to 32767)
-int32    the set of all signed 32-bit integers (-2147483648 to 2147483647)
-int64    the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
+int8        the set of all signed  8-bit integers (-128 to 127)
+int16       the set of all signed 16-bit integers (-32768 to 32767)
+int32       the set of all signed 32-bit integers (-2147483648 to 2147483647)
+int64       the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
 
-float32  the set of all IEEE-754 32-bit floating-point numbers
-float64  the set of all IEEE-754 64-bit floating-point numbers
+float32     the set of all IEEE-754 32-bit floating-point numbers
+float64     the set of all IEEE-754 64-bit floating-point numbers
+
+complex64   the set of all complex numbers with float32 real and imaginary parts
+complex128  the set of all complex numbers with float64 real and imaginary parts
 
 byte     familiar alias for uint8
 
@@ -619,6 +662,7 @@ There is also a set of predeclared numeric types with implementation-specific si uint either 32 or 64 bits int either 32 or 64 bits float either 32 or 64 bits +complex real and imaginary parts have type float uintptr an unsigned integer large enough to store the uninterpreted bits of a pointer value @@ -1502,7 +1546,8 @@ Zero value: nil Functions: - cap close closed copy len make new panic panicln print println + cap close closed cmplx copy imag len make + new panic panicln print println real @@ -1938,7 +1983,7 @@ Operands denote the elementary values in an expression.
 Operand    = Literal | QualifiedIdent | MethodExpr | "(" Expression ")" .
 Literal    = BasicLit | CompositeLit | FunctionLit .
-BasicLit   = int_lit | float_lit | char_lit | string_lit .
+BasicLit   = int_lit | float_lit | imaginary_lit | char_lit | string_lit .
 
@@ -2771,16 +2816,16 @@ x == y+1 && <-chan_ptr > 0

Arithmetic operators apply to numeric values and yield a result of the same type as the first operand. The four standard arithmetic operators (+, --, *, /) apply to integer and -floating-point types; + also applies +-, *, /) apply to integer, +floating-point, and complex types; + also applies to strings. All other arithmetic operators apply to integers only.

-+    sum                    integers, floats, strings
--    difference             integers, floats
-*    product                integers, floats
-/    quotient               integers, floats
++    sum                    integers, floats, complex values, strings
+-    difference             integers, floats, complex values
+*    product                integers, floats, complex values
+/    quotient               integers, floats, complex values
 %    remainder              integers
 
 &    bitwise and            integers
@@ -2894,9 +2939,10 @@ not occur. For instance, it may not assume that x < x + 1 is alw
 
 

Comparison operators yield a value of type bool. -The operators == and != apply, at least in some cases, +The operators == and != apply to operands of all types except arrays and structs. -All other comparison operators apply only to numeric and string values. +All other comparison operators apply only to integer, floating-point +and string values.

@@ -3208,15 +3254,19 @@ For example, if x := uint16(0x10F0), then uint32(int8(x)) ==
 The conversion always yields a valid value; there is no indication of overflow.
 

-

Conversions involving floating point types

+

Conversions involving floating point and complex types

  1. When converting a floating-point number to an integer, the fraction is discarded (truncation towards zero).
  2. -When converting a number to a floating-point type, the result value is rounded -to the precision specified by the floating point type. +A value of complex type may be converted to a different complex type, +but there is no conversion between complex and any other type. +
  3. +When converting a number to a floating-point or complex type, +the result value is rounded +to the precision specified by the destination type. For instance, the value of a variable x of type float32 may be stored using additional precision beyond that of an IEEE-754 32-bit number, but float32(x) represents the result of rounding x's value to @@ -3226,8 +3276,9 @@ of precision, but float32(x + 0.1) does not.

-In all conversions involving floating-point values, if the result type cannot -represent the value the conversion succeeds but the result value is +In all conversions involving floating-point or complex values, +if the result type cannot represent the value the conversion +succeeds but the result value is implementation-dependent.

@@ -3310,16 +3361,39 @@ respectively. Except for shift operations, if the operands of a binary operation are an untyped integer constant and an untyped floating-point constant, the integer constant is converted to an untyped floating-point constant (relevant for / and %). +Similarly, +untyped integer or floating-point constants may be used as operands +wherever it is legal to use an operand of complex type; +the integer or floating point constant is converted to a +complex constant with a zero imaginary part.

Applying an operator to untyped constants results in an untyped -constant of the same kind (that is, a boolean, integer, floating-point, or -string constant), except for -comparison operators which result in +constant of the same kind (that is, a boolean, integer, floating-point, +complex, or string constant), except for +comparison operators, which result in a constant of type bool.

+

+Imaginary literals are untyped complex constants (with zero real part) +and may be combined in binary +operations with untyped integer and floating-point constants; the +result is an untyped complex constant. +Complex constants are always constructed from +constant expressions involving imaginary +literals or constants derived from them, or calls of the +built-in function cmplx. +

+ +
+const Σ = 1 - 0.707i
+const Δ = Σ + 2.0e-4 - 1/i
+const Φ = iota * 1i
+const iΓ = cmplx(0, Γ)
+
+

Constant expressions are always evaluated exactly; intermediate values and the constants themselves may require precision significantly larger than supported @@ -3567,9 +3641,10 @@ In assignments, each value must be assignment compatible with the type of the operand to which it is assigned. If an untyped constant is assigned to a variable of interface type, the constant is converted -to type bool, int, float, or string +to type bool, int, float, +complex or string respectively, depending on whether the value is a boolean, integer, floating-point, -or string constant. +complex, or string constant.

@@ -4219,7 +4294,7 @@ for i := 0; i <= 3; i++ {

Built-in functions

-A small number of built-in functions are +Built-in functions are predeclared. They are called like any other function but some of them accept a type instead of an expression as the first argument. @@ -4378,6 +4453,49 @@ n1 := copy(s, &a) // n1 == 6, s == []int{0, 1, 2, 3, 4, 5} n2 := copy(s, s[2:]) // n2 == 4, s == []int{2, 3, 4, 5, 4, 5}

+

Assembling and disassembling complex numbers

+ +

+Three functions assemble and disassemble complex numbers. +The built-in function cmplx constructs a complex +value from a floating-point real and imaginary part, while +real and imag +extract the real and imaginary parts of a complex value. +

+ +
+cmplx(realPart, imaginaryPart floatT) complexT
+real(complexT) floatT
+imag(complexT) floatT
+
+ +

+The type of the arguments and return value correspond. +For cmplx, 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: +complex for float, +complex64 for float32, +complex128 for float64. +The real and imag functions +together form the inverse, so for a complex value z, +z == cmplx(real(z), imag(z)). +

+ +

+If the operands of these functions are all constants, the return +value is a constant. +

+ +
+var a = cmplx(2, -2)  // has type complex
+var b = cmplx(1.0, -1.4)  // has type complex
+x := float32(math.Cos(math.Pi/2))
+var c64 = cmplx(5, -x)  // has type complex64
+var im = imag(b)  // has type float
+var rl = real(c64)  // type float32
+
+

Bootstrapping

@@ -4808,6 +4926,6 @@ The following minimal alignment properties are guaranteed:
  • Implementation does not honor the restriction on goto statements and targets (no intervening declarations).
  • Method expressions are not implemented.
  • -
  • Conversions from strings to []int and []byte are not implemented..
  • +
  • The implementation of complex numbers is incomplete.
  • Gccgo allows only one init() function per source file.