英文:
int16 number plus float64 in golang
问题
我想做一件简单的事情:
func (this *ScoreProvider) getScore()(res float64) {
var score1 int16 = 500
var score2 int16 = 400
var score3 int16 = 300
var score4 int16 = 200
res = score1 * 0.25 + score2 * 0.25 + score3 * 0.25 + score4 * 0.25
return
}
但是报错了:
无法将 score1 * 0 + score2 * 0 + score3 * 0 + score4 * 0 (类型为 int16) 分配给类型 float64
我应该如何正确处理这个问题?
英文:
I want to do a simple thing:
func (this *ScoreProvider) getScore()(res float64) {
var score1 int16 = 500
var score2 int16 = 400
var score3 int16 = 300
var score4 int16 = 200
res = score1 * 0.25 + score2 * 0.25 + score3 * 0.25 + score4 * 0.25
return
}
But this reports an error:
can not use score1 * 0 + score2 * 0 + score3 * 0 + score4 * 0 (type int16) as type float64 in assignment
How can I do this right?
答案1
得分: 3
Go语言不提供隐式的数字转换,可以参考这个常见问题解答:为什么Go语言不提供隐式的数字转换?。
这意味着在进行算术运算时,不能混合不同类型的数据。它们必须是相同的类型。根据规范:
你的情况有点不同,因为0.25
是一个无类型常量,但由于它有小数部分,无法转换/解释为int16
,所以会在编译时出现错误。根据规范:
如果常量值无法表示为相应类型的值,则会出现错误。
在这种情况下,你有三个选项:
-
显式地将
scores
转换为float64
类型:res = float64(score1) * 0.25 + float64(score2) * 0.25 + float64(score3) * 0.25 + float64(score4) * 0.25
-
在
score
变量中使用float64
类型。var score1 float64 = 500 var score2 float64 = 400 // ...
-
由于你的算法计算平均值,你可以简单地执行以下操作:
res = float64(score1 + score2 + score3 + score4) / 4
英文:
Go does not provide implicit numeric conversion, see this FAQ: Why does Go not provide implicit numeric conversion?.
This means you can't mix different types when doing arithmetic operations. They have to be of the same type. Spec:
> The operand types must be identical unless the operation involves shifts or untyped constants.
Your case is a little different because 0.25
is an untyped constant but since it has a fraction part it can't be converted/interpreted as int16
so you get a compile time error. From the spec:
> It is an error if the constant value cannot be represented as a value of the respective type.
You have 3 options in this case:
-
Explicitly convert scores to
float64
:res = float64(score1) * 0.25 + float64(score2) * 0.25 + float64(score3) * 0.25 + float64(score4) * 0.25
-
Use
float64
type for yourscore
variables.var score1 float64 = 500 var score2 float64 = 400 // ...
-
Since your algorithm calculates average, you can simply do:
res = float64(score1 + score2 + score3 + score4) / 4
答案2
得分: 1
你的常量(0.25)被截断为整数(0)。
解决的两种方法:
将score1等变量转换为float32类型:
var score1 int16 = 500
var score2 int16 = 400
var score3 int16 = 300
var score4 int16 = 200
res := float32(score1)*0.25 + float32(score2)*0.25 + float32(score3)*0.25 + float32(score4)*0.25
fmt.Println("Score", res)
或者更合理的做法是,将它们一开始就声明为float32类型:
var score1a float32 = 500
var score2a float32 = 400
var score3a float32 = 300
var score4a float32 = 200
res2 := score1a * 0.25 + score2a * 0.25 + score3a * 0.25 + score4a * 0.25
fmt.Println("Result 1", res)
fmt.Println("Result 2", res2)
在Go Playground上查看示例。
英文:
Your constants (0.25) are being truncated to intergers (0).
Two ways to solve:
cast the score1 etc variables to float32:
var score1 int16 = 500
var score2 int16 = 400
var score3 int16 = 300
var score4 int16 = 200
res := float32(score1)*0.25 + float32(score2)*0.25 + float32(score3)*0.25 + float32(score4)*0.25
fmt.Println("Score", res)
or more sensible, instead of declaring them as int16 declare them as float32 to begin with:
var score1a float32 = 500
var score2a float32 = 400
var score3a float32 = 300
var score4a float32 = 200
res2 := score1a * 0.25 + score2a * 0.25 + score3a * 0.25 + score4a * 0.25
fmt.Println("Result 1", res)
fmt.Println("Result 2", res2)
On the Go Playground
答案3
得分: 0
在Go语言中,没有自动类型提升,你需要显式地进行类型转换。
我会将你的代码写成这样:
package main
import "fmt"
func getScore() float64 {
s1, s2, s3, s4 := int16(500), int16(400), int16(300), int16(200)
return (float64(s1) + float64(s2) + float64(s3) + float64(s4)) * 0.25
}
func main() {
fmt.Println(getScore())
}
希望对你有帮助!
英文:
There's no automatic type promotion in go and you need to include explicit conversions instead.
I'd write your code something like this:
package main
import "fmt"
func getScore() float64 {
s1, s2, s3, s4 := int16(500), int16(400), int16(300), int16(200)
return (float64(s1) + float64(s2) + float64(s3) + float64(s4)) * 0.25
}
func main() {
fmt.Println(getScore())
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论