Array.Sum() 导致溢出
Array.Sum() results in an overflow
我有一个像这样的 int 数组
int[] arr = {256741038,623958417,467905213,714532089,938071625};
然后我创建了一个 int64 变量
Int64 sum = arr.Sum();
但这导致了溢出
Run-time exception (line 19): Arithmetic operation resulted in an
overflow.
不使用循环总结如何解决这个问题? (数组类型必须是int)
您必须将其转换为 long 以免溢出
var result = arr.Select(x => (long)x).Sum();
Range = -2,147,483,648
to 2,147,483,647
一些背景,这是Sum
的源代码
public static int Sum(this IEnumerable<int> source)
{
if (source == null) throw Error.ArgumentNull("source");
int sum = 0;
checked
{
foreach (int v in source)
sum += v;
}
return sum;
}
意思是,不管你喜欢与否,有人在使用 for 循环,另外 checked
的用法是它抛出的原因:)
The checked keyword is used to explicitly enable overflow checking for
integral-type arithmetic operations and conversions.
问题在于,虽然单个值适合 int
,但这些数字结果的总和大于 int
可以容纳的值。
因此,您需要将值转换为 long
(或另一种需要那么大数字的数据类型,但由于您使用的是 Int64...):
long sum = arr.Sum(v => (long)v);
对整数数组调用求和时,输出类型为整数,且输入数字的总和大于整数最大值。正如@TheGeneral 所说的 sum 方法检查溢出并抛出异常。
可能是你期望 sum 方法 return 结果一样长,但它的 return 类型是一个整数。
int a = 256741038;
int b = 623958417;
int c = 467905213;
int d = 714532089;
int e = 938071625;
long sumInt = a + b + c + d + e;
//-1293758914 //overflow happened withoud exception
long sumLong = (long)a + b + c + d + e;//clr now the result may be more than int.maxvalue
//3001208382
求和如下所示
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
int[] arr = {999999999, 999999999, 999999999,999999999, 999999999};
long sum = 0;
for (int i = 0; i < arr.Length; i += 1)
{
sum += arr[i];
}
Console.WriteLine(sum);
}
}
更短的方式如下图
using System;
namespace Test
{
public partial class TestPage : System.Web.UI.Page
{
public int[] p = { 999999999, 999999999, 999999999, 999999999, 999999999 };
protected void Page_Load(object sender, EventArgs e)
{
long s = Sum;
}
public long Sum
{
get { long s = 0; Array.ForEach(p, delegate (int i) { s += i; }); return s; }
}
}
}
希望对您有所帮助。
我有一个像这样的 int 数组
int[] arr = {256741038,623958417,467905213,714532089,938071625};
然后我创建了一个 int64 变量
Int64 sum = arr.Sum();
但这导致了溢出
Run-time exception (line 19): Arithmetic operation resulted in an overflow.
不使用循环总结如何解决这个问题? (数组类型必须是int)
您必须将其转换为 long 以免溢出
var result = arr.Select(x => (long)x).Sum();
Range =
-2,147,483,648
to2,147,483,647
一些背景,这是Sum
public static int Sum(this IEnumerable<int> source)
{
if (source == null) throw Error.ArgumentNull("source");
int sum = 0;
checked
{
foreach (int v in source)
sum += v;
}
return sum;
}
意思是,不管你喜欢与否,有人在使用 for 循环,另外 checked
的用法是它抛出的原因:)
The checked keyword is used to explicitly enable overflow checking for integral-type arithmetic operations and conversions.
问题在于,虽然单个值适合 int
,但这些数字结果的总和大于 int
可以容纳的值。
因此,您需要将值转换为 long
(或另一种需要那么大数字的数据类型,但由于您使用的是 Int64...):
long sum = arr.Sum(v => (long)v);
对整数数组调用求和时,输出类型为整数,且输入数字的总和大于整数最大值。正如@TheGeneral 所说的 sum 方法检查溢出并抛出异常。
可能是你期望 sum 方法 return 结果一样长,但它的 return 类型是一个整数。
int a = 256741038;
int b = 623958417;
int c = 467905213;
int d = 714532089;
int e = 938071625;
long sumInt = a + b + c + d + e;
//-1293758914 //overflow happened withoud exception
long sumLong = (long)a + b + c + d + e;//clr now the result may be more than int.maxvalue
//3001208382
求和如下所示
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
int[] arr = {999999999, 999999999, 999999999,999999999, 999999999};
long sum = 0;
for (int i = 0; i < arr.Length; i += 1)
{
sum += arr[i];
}
Console.WriteLine(sum);
}
}
更短的方式如下图
using System;
namespace Test
{
public partial class TestPage : System.Web.UI.Page
{
public int[] p = { 999999999, 999999999, 999999999, 999999999, 999999999 };
protected void Page_Load(object sender, EventArgs e)
{
long s = Sum;
}
public long Sum
{
get { long s = 0; Array.ForEach(p, delegate (int i) { s += i; }); return s; }
}
}
}
希望对您有所帮助。