Go – 切片中元素的正数、负数和零的比例

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

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 &lt; len(arr); i++ {
	totalNumber += 1
}

for i := 0; i &lt; len(arr); i++ {
	if arr[i] &lt; 0 {
		negativeNumber += 1
	} else if arr[i] == 0 {
		zeroNumber += 1
	} else if arr[i] &gt; 0 {
		positiveNumber += 1
	}
}

negativeRatio = float32(negativeNumber / totalNumber)
zeroRatio = float32(zeroNumber / totalNumber)
positiveRatio = float32(positiveNumber / totalNumber)

fmt.Printf(&quot;total number: %d\n&quot;, totalNumber)

fmt.Printf(&quot;positive number: %d\n&quot;, positiveNumber)
fmt.Printf(&quot;negative number: %d\n&quot;, negativeNumber)
fmt.Printf(&quot;zero number: %d\n&quot;, zeroNumber)

fmt.Printf(&quot;positive ratio: %f\n&quot;, positiveRatio)
fmt.Printf(&quot;negative ratio: %f\n&quot;, negativeRatio)
fmt.Printf(&quot;zero ratio: %f\n&quot;, 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

你正在使用整数除法而不是浮点数除法,这会丢弃余数并仅返回除法结果的整数部分。

由于negativeNumbertotalNumber都是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

huangapple
  • 本文由 发表于 2023年1月3日 19:17:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/74992626.html
匿名

发表评论

匿名网友

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

确定