英文:
Go - Ratios of its elements that are positive, negative, and zero number in a slice
问题
我想在一个切片中显示其元素的正数、负数和零的比例。我需要以float32的形式表示这些比例。这是我的代码:
arr := []int32{-2, -1, 0, 1, 2}
var negativeNumber, positiveNumber, zeroNumber, totalNumber int32
var negativeRatio, positiveRatio, zeroRatio float32
for i := 0; i < len(arr); i++ {
totalNumber += 1
}
for i := 0; i < len(arr); i++ {
if arr[i] < 0 {
negativeNumber += 1
} else if arr[i] == 0 {
zeroNumber += 1
} else if arr[i] > 0 {
positiveNumber += 1
}
}
negativeRatio = float32(negativeNumber) / float32(totalNumber)
zeroRatio = float32(zeroNumber) / float32(totalNumber)
positiveRatio = float32(positiveNumber) / float32(totalNumber)
fmt.Printf("总数: %d\n", totalNumber)
fmt.Printf("正数个数: %d\n", positiveNumber)
fmt.Printf("负数个数: %d\n", negativeNumber)
fmt.Printf("零的个数: %d\n", zeroNumber)
fmt.Printf("正数比例: %f\n", positiveRatio)
fmt.Printf("负数比例: %f\n", negativeRatio)
fmt.Printf("零的比例: %f\n", zeroRatio)
但是,当我打印变量时,得到的正数、负数和零的个数是正确的,但比例是错误的。以下是输出结果:
总数: 5
正数个数: 2
负数个数: 2
零的个数: 1
正数比例: 0.000000
负数比例: 0.000000
零的比例: 0.000000
我做错了什么?
英文:
I wanna show the ratios of its elements that are positive, negative, and zero in a slice. I need the ratios in float32. This is my code:
arr := []int32{-2, -1, 0, 1, 2}
var negativeNumber, positiveNumber, zeroNumber, totalNumber int32
var negativeRatio, positiveRatio, zeroRatio float32
for i := 0; i < len(arr); i++ {
totalNumber += 1
}
for i := 0; i < len(arr); i++ {
if arr[i] < 0 {
negativeNumber += 1
} else if arr[i] == 0 {
zeroNumber += 1
} else if arr[i] > 0 {
positiveNumber += 1
}
}
negativeRatio = float32(negativeNumber / totalNumber)
zeroRatio = float32(zeroNumber / totalNumber)
positiveRatio = float32(positiveNumber / totalNumber)
fmt.Printf("total number: %d\n", totalNumber)
fmt.Printf("positive number: %d\n", positiveNumber)
fmt.Printf("negative number: %d\n", negativeNumber)
fmt.Printf("zero number: %d\n", zeroNumber)
fmt.Printf("positive ratio: %f\n", positiveRatio)
fmt.Printf("negative ratio: %f\n", negativeRatio)
fmt.Printf("zero ratio: %f\n", zeroRatio)
But, when I print the variables, I get the positive, negative, and zero numbers right but wrong ratio. Here is the output:
total number: 5
positive number: 2
negative number: 2
zero number: 1
positive ratio: 0.000000
negative ratio: 0.000000
zero ratio: 0.000000
What do I do wrong?
答案1
得分: 1
你正在使用整数除法而不是浮点数除法,这会丢弃余数并仅返回除法结果的整数部分。
由于negativeNumber
和totalNumber
都是int32
类型,negativeNumber / totalNumber
执行整数除法,它执行除法并返回结果的向下取整值(即0)。然后,通过使用float32(negativeNumber / totalNumber)
将其转换为float32
类型,你得到的结果是0.0
,这是预期的。
为了使用浮点数除法,操作数中必须有一个是浮点数类型。你可以使用以下方法:float32(negativeNumber) / totalNumber
英文:
You are using integer division instead of floating point division, which discards the remainder and returns the integer part of the division result only.
Since negativeNumber
and totalNumber
are both of type int32
, negativeNumber / totalNumber
performs integer division, which performs the division and returns the floor of the result (which is 0). When you then cast it to a float32
with float32(negativeNumber / totalNumber)
, you get 0.0
, which is expected.
In order to use floating point division, one of the operands must be a floating point type. To do this, you can use: float32(negativeNumber) / totalNumber
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论