Average function without overflow exception

后端 未结 18 1782
一生所求
一生所求 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:36

    If you know in advance that all your numbers are going to be 'big' (in the sense of 'much nearer long.MaxValue than zero), you can calculate the average of their distance from long.MaxValue, then the average of the numbers is long.MaxValue less that.

    However, this approach will fail if (m)any of the numbers are far from long.MaxValue, so it's horses for courses...

提交回复
热议问题