Average function without overflow exception

后端 未结 18 1780
一生所求
一生所求 2021-02-12 14:58

.NET Framework 3.5.
I\'m trying to calculate the average of some pretty large numbers.
For instance:

using System;
using System.Linq;

class Program
{
           


        
18条回答
  •  眼角桃花
    2021-02-12 15:30

    Here is my version of an extension method that can help with this.

        public static long Average(this IEnumerable longs)
        {
            long mean = 0;
            long count = longs.Count();
            foreach (var val in longs)
            {
                mean += val / count;
            }
            return mean;
        }
    

提交回复
热议问题