英文:
Mismatched types float64 and int
问题
我正在尝试创建随机成绩并将它们添加到test_scores
数组中,然后计算平均值。
这个程序:
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))
}
产生的错误信息是:
main.go:24: invalid operation: total / 5 (mismatched types float64 and int)
而这个可以正常工作:
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)
}
唯一的区别在于最后一行,非工作版本中使用了len(test_scores)
,而正常工作的版本中使用了固定的5
。
Len 也返回一个整数,那么问题出在哪里呢?
英文:
I'm trying to create random grades and adding them to a test_scores
array. 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.
Len returns an integer as well, so what's up?
答案1
得分: 5
float64
和int
是不同的类型,但在特定情况下允许进行转换。(http://golang.org/ref/spec#Conversions)
你代码中的字面量5
是一个无类型常量(http://golang.org/ref/spec#Constants),在编译过程中会根据表达式确定其正确的类型。
只需使用float64(len(test_scores))
即可。
英文:
float64
and int
are different types, but conversions are allowed under specific circumstances. (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.
Simply use float64(len(test_scores))
答案2
得分: 3
当你直接在源代码中写入5
时,这被称为常量。对于写入true
也是一样的。唯一的区别在于前者是一个无类型常量,而后者是一个有类型常量。
区别在于对于true
应该具有什么类型没有任何歧义,它总是bool
类型,但对于5
来说,这并不那么明显,它取决于上下文。
Go编译器会在编译时确定常量的类型。关于此的详细信息在Go语言规范中有描述。
编辑:
我意识到我的回答中有一个错误:根据规范,true
实际上也是无类型的,因为它可以在任何需要派生自bool
类型的地方使用。这意味着:
type MyBool bool
func DoNothing(b MyBool) {}
DoNothing(true) // true被强制转换为MyBool
尽管如此,答案仍然有效。有类型常量和无类型常量之间的区别仍然存在。
英文:
When you write 5
directly in the source-code that's called a constant. Same goes for writing 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.
The Go compiler will figure out what type to give the constant on compilation. The details of this are described in Go's language specification.
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. 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.
答案3
得分: -1
这段代码的翻译如下:
这些代码行
fmt.Printf("%T\n", total)
fmt.Printf("%T\n", 5)
fmt.Printf("%T\n", 5.0)
fmt.Printf("%T\n", len(test_scores))
输出结果为
float64
int
float64
int
也许编译器将5视为5.0。无论如何,你应该使用float64进行转换。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论