49017

How to calculate the sum of an int array whose result exceeds Int32.Max value

Question:

For example we have an int array:

var array = new int[]{ 2147483647, 2147483647, 2147483647, 2147483647};

What is the easiest way to calculate the sum of the array entries, <strong>BUT in respect to the example provided above</strong>?

array.Sum()

results in:

<blockquote>

Arithmetic operation resulted in an overflow

</blockquote>

Because the result is not an int anymore..

Answer1:

Because the sum of the values in your array overflows the Int32.MaxValue you are forced to cast your elements to a long

var array = new int[]{ 2147483647, 2147483647, 2147483647, 2147483647}; var total = array.Sum(x => (long)x); Console.WriteLine(total);

And you can see that the total variable is of type Int64

Console.WriteLine(total.GetType());

Answer2:

Steve's answer fits your question perfectly. However, if you need to store the sum of values that are longer than the typical datatypes you could use BigInteger.

var array = new [] { long.MaxValue, long.MaxValue, long.MaxValue, long.MaxValue }; var result = new BigInteger(); result = array.Aggregate(result, (current, i) => current + i);

This solution would also work for your provided array.

Recommend

  • require.js shim, export myOwnGlobal name
  • Getting a Template/Generic java.lang.reflect.Method object from org.aspectj.lang.ProceedingJoinPoint
  • C++ subtype degeneration when placed in a container
  • Where can I download an older version of AnythingSlider?
  • How was this records/fanout logic derived for the “no statistics” case in MySQL's Query Planner
  • Browser Automation and Cross Site Scripting
  • Difference between Memory Mapped I/O and Programmed I/O
  • Making Toplevel resize itself to fit the title
  • Perl Moose accessors generated on the fly
  • How to show that Shift Left Logical multiplies by 2^n?
  • Is there a way to know if TAdoQuery in Edit state was changed?
  • Regular expression from font to span (size and colour) and back (VB.NET)
  • AngularFire httpsCallable Object(…) is not a function
  • Why isn't my .net destructor called in this very simple scenario?
  • ArrayList in C#
  • What code changes are required to migrate C++ from VS2003 to VS2005?
  • How to update data in Redis and MySQL at the same time?
  • Why TableAdapter doesn't recognize @parameter
  • JAXB 2 in an Oracle 10g Webapp
  • SSIS package hangs while running
  • C++ stl pop doesn't return [closed]
  • How can I determine if process is 32 or 64Bit from a handle?
  • How to remove last utf8 char of a python string
  • Unable to set a breakpoint on main while debugging a program compiled with Rust 1.10 with GDB
  • Upper limits for fibonnacci
  • Multiple Left Join LINQ-to-entities
  • Suqueries in select clause with JPA
  • jQuery: add elements until a particular height is reached
  • Default parameter as generic type
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • When should I choose bucket sort over other sorting algorithms?
  • Convert array of 8 bytes to signed long in C++
  • php design question - will a Helper help here?
  • Unanticipated behavior
  • AngularJs get employee from factory
  • Understanding cpu registers
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices