Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

关于 Go 语言中 const 的问题

本问题来自《Go语言圣经》中 GIF 动画 中的代码,作用是生成一个利萨如图形 GIF。

代码如下:

// Lissajous generates GIF animations of random Lissajous figures.
package main

import (
    "fmt"
    "image"
    "image/color"
    "image/gif"
    "io"
    "math"
    "math/rand"
    "os"
    "reflect"
)

var palette = []color.Color{color.White, color.RGBA{0x6C, 0x9F, 0xB4, 0xff}}

const (
    whiteIndex = 0 // first color in palette
    greenIndex = 1 // second color in palette
)

func main() {
    lissajous(os.Stdout)
}

func lissajous(out io.Writer) {
    const (
        cycles  = 5     // number of complete x oscillator revolutions
        res     = 0.001 // angular resolution
        size    = 100   // image canvas covers [-size..+size]
        nframes = 64    // number of animation frames
        delay   = 8     // delay between frames in 10ms units
    )

    freq := rand.Float64() * 3.0 // relative frequency of y oscillator
    anim := gif.GIF{LoopCount: nframes}
    phase := 0.0 // phase difference
    for i := 0; i < nframes; i++ {
        rect := image.Rect(0, 0, 2*size+1, 2*size+1)
        img := image.NewPaletted(rect, palette)
        for t := 0.0; t < cycles*2*math.Pi; t += res {
            x := math.Sin(t)
            y := math.Sin(t*freq + phase)
            img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5), greenIndex)
        }
        phase += 0.1
        anim.Delay = append(anim.Delay, delay)
        anim.Image = append(anim.Image, img)
    }

    // gif.EncodeAll(out, &anim) // NOTE: ignoring encoding errors

    fmt.Println(reflect.TypeOf(cycles))
}

不输出 gif,执行代码查看 cycles 的类型为 int.

% go run lissajous.go
int

将 cycles 的声明语句改为 var cycles = 5 后出错,这里 cycles 也是 int 型。

% go run lissajous.go
# command-line-arguments
./lissajous.go:44: constant 3.14159 truncated to integer
./lissajous.go:44: invalid operation: t < cycles * 2 * math.Pi (mismatched types float64 and int)

尝试改成 var cycles = 5.0 后,cycles 的类型为 float64,代码能正常运行。

问题

为什么用 const 声明的 intvar 声明的 int 会有差别?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

首先感谢 @mugbya 的回答,这里自己完善下回答。

常量| Go 语言圣经 下有这么一段

Go语言的常量有个不同寻常之处。虽然一个常量可以有任意有一个确定的基础类型,例如int或float64,或者是类似time.Duration这样命名的基础类型,但是许多常量并没有一个明确的基础类型。编译器为这些没有明确的基础类型的数字常量提供比基础类型更高精度的算术运算;你可以认为至少有256bit的运算精度。

通过延迟明确常量的具体类型,无类型的常量不仅可以提供更高的运算精度,而且可以直接用于更多的表达式而不需要显式的类型转换。

例子: math.Pi 为无类型的浮点型常量,可以直接用于需要浮点型或复数的地方

var x float32 = math.Pi     // 3.1415927
var y float64 = math.Pi     // 3.141592653589793
var z complex128 = math.Pi  // (3.141592653589793+0i)

const Pi64 float64 = math.Pi // 明确指明类型
var i float32 = Pi64        // error: cannot use Pi64 (type float64) as type float32 in assignment
var j float64 = Pi64        // 3.141592653589793

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...