在Go语言中,将int16类型的数字与float64类型的数字相加。

huangapple go评论71阅读模式
英文:

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,所以会在编译时出现错误。根据规范

如果常量值无法表示为相应类型的值,则会出现错误。

在这种情况下,你有三个选项:

  1. 显式地将scores转换为float64类型:

     res = float64(score1) * 0.25 + float64(score2) * 0.25 +
         float64(score3) * 0.25 + float64(score4) * 0.25
    
  2. score变量中使用float64类型。

     var score1 float64 = 500
     var score2 float64 = 400
     // ...
    
  3. 由于你的算法计算平均值,你可以简单地执行以下操作:

     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:

  1. Explicitly convert scores to float64:

     res = float64(score1) * 0.25 + float64(score2) * 0.25 +
         float64(score3) * 0.25 + float64(score4) * 0.25
    
  2. Use float64 type for your score variables.

     var score1 float64 = 500
     var score2 float64 = 400
     // ...
    
  3. 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())
}

huangapple
  • 本文由 发表于 2015年5月27日 17:35:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/30478544.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定