Constants may be typed or untyped. Literal constants, true, false, iota, and certain constant expressions containing only untyped constant operands are untyped.
A constant may be given a type explicitly by a constant declaration or conversion, or implicitly when used in a variable declaration or an assignment or as an operand in an expression. It is an error if the constant value cannot be represented as a value of the respective type.
An untyped constant has a default type which is the type to which the constant is implicitly converted in contexts where a typed value is required, for instance, in a short variable declaration such as i := 0 where there is no explicit type. The default type of an untyped constant is bool, rune, int, float64, complex128 or string respectively, depending on whether it is a boolean, rune, integer, floating-point, complex, or string constant.
Implementation restriction: Although numeric constants have arbitrary precision in the language, a compiler may implement them using an internal representation with limited precision. That said, every implementation must:
Represent integer constants with at least 256 bits.(256ビット以上の整数定数を表します。) Represent floating-point constants, including the parts of a complex constant, with a mantissa of at least ・256 bits and a signed binary exponent of at least 16 bits.(複素定数の一部を含む浮動小数点定数を表します。仮数は256ビット以上で、符号付き2進指数は16ビット以上です。) Give an error if unable to represent an integer constant precisely.(整数定数を正確に表すことができない場合は、エラーを出します。) Give an error if unable to represent a floating-point or complex constant due to overflow.(オーバーフローのために浮動小数点または複素定数を表すことができない場合は、エラーを出します。) Round to the nearest representable constant if unable to represent a floating-point or complex constant due to limits on precision.(精度の制限のために浮動小数点または複素定数を表現できない場合は、最も近い表現可能な定数に丸めます。) These requirements apply both to literal constants and to the result of evaluating constant expressions.
これらの要件は、リテラル定数と定数式の評価結果の両方に適用されます。(by Google Translated.)
Constant expressions may contain only constant operands and are evaluated at compile time.
定数式には定数オペランドのみを含めることができ、コンパイル時に評価されます。
Untyped boolean, numeric, and string constants may be used as operands wherever it is legal to use an operand of boolean, numeric, or string type, respectively.
A constant comparison always yields an untyped boolean constant. If the left operand of a constant shift expression is an untyped constant, the result is an integer constant; otherwise it is a constant of the same type as the left operand, which must be of integer type.
Any other operation on untyped constants results in an untyped constant of the same kind; that is, a boolean, integer, floating-point, complex, or string constant. If the untyped operands of a binary operation (other than a shift) are of different kinds, the result is of the operand’s kind that appears later in this list: integer, rune, floating-point, complex. For example, an untyped integer constant divided by an untyped complex constant yields an untyped complex constant.
型なし定数に対する他の操作は、同じ種類の型なし定数になります。つまり、ブール、整数、浮動小数点、複素数、または文字列定数です。二項演算(シフト以外)の型指定されていないオペランドの種類が異なる場合、結果はこのリストの後半に表示されるオペランドの種類(整数、ルーン、浮動小数点、複素数)になります。たとえば、型なし整数定数を型なし複素定数で割ると、型なし複素定数が生成されます。(by Google Translated.)
package mainimport"fmt"const (// Create a huge number by shifting a 1 bit left 100 places.// In other words, the binary number that is 1 followed by 100 zeroes.// 百桁ビット(→2進数で[100...(百桁)...000]) Big =1<<100// Untyped integer constant// Shift it right again 99 places, so we end up with 1<<1, or 2.// 百桁ビットのBigから99桁消す(→2進数で[10]、10進数だと[2]) Small = Big >>99// Untyped integer constant)funcneedInt(x int) int {return x *10+1}funcneedFloat(x float64) float64 {return x *0.1}funcmain() {// 定数をファンクションで計算し結果を出力 fmt.Println(needInt(Small)) fmt.Println(needFloat(Small)) fmt.Println(needFloat(Big))// 「A Tour of Go」のページで下記を追加しろとあるが、追加するとエラー(Overflow)が発生する。// これは、定数 Big が needInt ファンクションの引数として扱われたタイミングで// Untyped integer constant から int として扱いを変えようとしたため。// int型では百桁ビット(10進数だと[1267650600228229401496703205376])は大きすぎて扱えない。// needFloat(Big) ファンクションのように float型 なら許容できる。// fmt.Println(needInt(Big))}
コメント