struct someStruct {
//...
var rating: Double
mutating func rate(rating: Double) {
}
}
I know how to calculate the average if I record every rating in an array. But my idea is to modify the rating
each time I call the method rate()
with a new rating
, just like
如果我记录数组中的每个评级,我知道如何计算平均值。但我的想法是每次用新评级调用方法rate()时修改评级,就像
self.rating = (self.rating * numberOfRatings + rating)/(numberOfRatings + 1)
The problem is that I have no idea how to manage numberOfRatings
. How can this be realized specifically?
问题是我不知道如何管理numberOfRatings。具体如何实现?
2 个解决方案
#1
Just make numberOfRatings
a property of your structure:
只需将numberOfRatings作为结构的属性:
struct someStruct {
//...
var rating: Double = 0
var numberOfRatings: Double = 0
mutating func rate(rating: Double) {
self.rating = ((self.rating * numberOfRatings) + rating) / (numberOfRatings + 1)
numberOfRatings++
}
}
Then you'd use it like this:
然后你会像这样使用它:
var a = someStruct()
a.rate(5)
a.rate(6)
a.rate(8)
println(a.rating) // prints "6.33333"
@ABakerSmith provides an alternate answer that has the advantage of maintaining the total
which avoids introducing error into the calculation. We can take that a step further and turn rating
into a computed property that only gets calculated when you need it:
@ABakerSmith提供了一个备用答案,其优点是可以保持总数,从而避免在计算中引入错误。我们可以更进一步,将评级转换为计算属性,只在需要时计算:
struct someStruct {
var total: Double = 0
var numberOfRatings: Double = 0
var rating: Double {
return total/numberOfRatings
}
mutating func rate(rating: Double) {
total += rating
numberOfRatings++
}
}
This would be more efficient if you call rate
more often than you access the rating
property. If the opposite is true, then computing the rating
within rate
would be more efficient.
如果您比访问rating属性更频繁地调用rate,这将更有效。如果情况相反,则计算费率内的评级会更有效。
#2
@vacawama gives a good method. Here's an alternative you could try too: firstly, have a look at this. The there are two methods stated at the end for calculating a running average. The simplest one is to add the nth number to the existing total and divide by n. Here's an example:
@vacawama提供了一个很好的方法。这里有一个你可以尝试的替代方案:首先,看看这个。最后有两种方法用于计算运行平均值。最简单的一个是将第n个数字加到现有总数中并除以n。这是一个例子:
struct SomeStruct {
var rating: Double = 0
var numberOfRatings: Int = 0
var total: Double = 0
mutating func rate(num: Double) {
total += num
rating = total / Double(++numberOfRatings)
}
}
Then use this like so:
然后像这样使用它:
myStruct.rate(5)
myStruct.rate(6)
myStruct.rate(8)
myStruct.rating // 6.33333...
#1
Just make numberOfRatings
a property of your structure:
只需将numberOfRatings作为结构的属性:
struct someStruct {
//...
var rating: Double = 0
var numberOfRatings: Double = 0
mutating func rate(rating: Double) {
self.rating = ((self.rating * numberOfRatings) + rating) / (numberOfRatings + 1)
numberOfRatings++
}
}
Then you'd use it like this:
然后你会像这样使用它:
var a = someStruct()
a.rate(5)
a.rate(6)
a.rate(8)
println(a.rating) // prints "6.33333"
@ABakerSmith provides an alternate answer that has the advantage of maintaining the total
which avoids introducing error into the calculation. We can take that a step further and turn rating
into a computed property that only gets calculated when you need it:
@ABakerSmith提供了一个备用答案,其优点是可以保持总数,从而避免在计算中引入错误。我们可以更进一步,将评级转换为计算属性,只在需要时计算:
struct someStruct {
var total: Double = 0
var numberOfRatings: Double = 0
var rating: Double {
return total/numberOfRatings
}
mutating func rate(rating: Double) {
total += rating
numberOfRatings++
}
}
This would be more efficient if you call rate
more often than you access the rating
property. If the opposite is true, then computing the rating
within rate
would be more efficient.
如果您比访问rating属性更频繁地调用rate,这将更有效。如果情况相反,则计算费率内的评级会更有效。
#2
@vacawama gives a good method. Here's an alternative you could try too: firstly, have a look at this. The there are two methods stated at the end for calculating a running average. The simplest one is to add the nth number to the existing total and divide by n. Here's an example:
@vacawama提供了一个很好的方法。这里有一个你可以尝试的替代方案:首先,看看这个。最后有两种方法用于计算运行平均值。最简单的一个是将第n个数字加到现有总数中并除以n。这是一个例子:
struct SomeStruct {
var rating: Double = 0
var numberOfRatings: Int = 0
var total: Double = 0
mutating func rate(num: Double) {
total += num
rating = total / Double(++numberOfRatings)
}
}
Then use this like so:
然后像这样使用它:
myStruct.rate(5)
myStruct.rate(6)
myStruct.rate(8)
myStruct.rating // 6.33333...