Is there any way to find arithmetic mean “better” than sum()/N?

前端 未结 6 1744
情书的邮戳
情书的邮戳 2020-12-23 17:48

Suppose we have N numbers(integers, floats, whatever you want) and want to find their arithmetic mean. Simplest method is to sum all values and divide by number of values:

6条回答
  •  抹茶落季
    2020-12-23 18:11

    If you use float you might avoid big integers:

    def simple_mean(array[N]):
        sum = 0.0 # <---
        for i = 1 to N
           sum += array[i]
        return sum / N
    

提交回复
热议问题