简体   繁体   English

类型不匹配的float64和int

[英]Mismatched types float64 and int

I'm trying to create random grades and adding them to a test_scores array. 我正在尝试创建随机成绩并将其添加到test_scores数组中。 Then calculate the average. 然后计算平均值。

This program: 该程序:

package main

import (
    "fmt"
    "math/rand"
)

func main() {
    i := 0
    var test_scores [5]float64
    for i < len(test_scores) {
        test_scores[i] = rand.Float64()
        i++
    }
    fmt.Println(test_scores)

    var total float64 = 0
    i = 0
    for i < len(test_scores) {
        total += test_scores[i]
        i++
    }
    fmt.Println(total)
    fmt.Println(total / len(test_scores))
}

produces: 产生:

main.go:24: invalid operation: total / 5 (mismatched types float64 and int)

This one works fine: 这个很好用:

package main

import (
    "fmt"
    "math/rand"
)

func main() {
    i := 0
    var test_scores [5]float64
    for i < len(test_scores) {
        test_scores[i] = rand.Float64()
        i++
    }
    fmt.Println(test_scores)

    var total float64 = 0
    i = 0
    for i < len(test_scores) {
        total += test_scores[i]
        i++
    }
    fmt.Println(total)
    fmt.Println(total / 5)
}

The only difference being that in the final line, I'm using a fixed 5 and on the non-working one, I'm using the len(test_scores) call. 唯一的区别是,在最后一行中,我使用固定5 ,在不工作的情况下,我使用len(test_scores)调用。

Len returns an integer as well, so what's up? Len也会返回整数,所以怎么了?

float64 and int are different types, but conversions are allowed under specific circumstances. float64int是不同的类型,但是在特定情况下允许进行转换。 ( http://golang.org/ref/spec#Conversions ) http://golang.org/ref/spec#Conversions

Your literal 5 in the code is an untyped constant ( http://golang.org/ref/spec#Constants ), and the proper type is determined by the expression during compilation. 代码中的文字5是无类型的常量( http://golang.org/ref/spec#Constants ),正确的类型由编译期间的表达式确定。

Simply use float64(len(test_scores)) 只需使用float64(len(test_scores))

When you write 5 directly in the source-code that's called a constant. 当您直接在称为常量的源代码中编写5 Same goes for writing true . true The only difference is that the former is an untyped constant and the latter a typed constant. 唯一的区别是,前者是无类型常量,而后者是有类型常量。

The difference lies in that there's no ambiguity about what type true should have – it'll always be bool but in the case of 5 that's not so obvious and depends on the context. 区别在于,对于true应该具有哪种类型没有任何歧义-始终是bool但在5的情况下并不是很明显,并且取决于上下文。

The Go compiler will figure out what type to give the constant on compilation. Go编译器会找出在编译时提供常量的类型。 The details of this are described in Go's language specification . 有关详细信息,请参见Go的语言规范

Edit: 编辑:

I realized that there's a mistake in my answer: true is in fact also untyped according to the spec because it may be utilized anywhere where a type deriving from bool is expected. 我意识到我的答案有一个错误:实际上,根据规范也未输入true ,因为它可以在期望从bool派生的类型的任何地方使用。 That means: 这意味着:

type MyBool bool

func DoNothing(b MyBool) {}

DoNothing(true) // true is coerced to MyBool

The answer is still valid, though. 不过,答案仍然有效。 The distinction between typed and untyped constants holds. 类型常量和非类型常量之间的区别成立。

This lines 这条线

fmt.Printf("%T\n", total)
fmt.Printf("%T\n", 5)
fmt.Printf("%T\n", 5.0)
fmt.Printf("%T\n", len(test_scores))

prints 版画

float64
int
float64
int

Maybe compiler perceives 5 as 5.0.. Anyway you should use conversion to float64. 也许编译器将5表示为5.0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM