Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integral constant is too large (CS1021) - How to put 1000 extremely big (70+ digits) integers in an array?

Tags:

c#

I'm using the software SharpDevelop (C#).

I've created a list of integers (array) like this:

int[] name = new int[number-of-elements]{elements-separated-by-commas}

In the {} I would like to put 1000 integers, some with over 70 digits.

But when I do that I get the following error:

Integral constant is too large (CS1021).

So how do I solve this problem?

like image 897
user50746 Avatar asked Dec 19 '22 10:12

user50746


2 Answers

The error does not mean that you have too many integers in your array. It means that one of the integers is larger than the maximum value representable in an int in C#, i.e. above 2,147,483,647.

If you need representation of 70-digit numbers, use BigInteger:

BigInteger[] numbers = new[] {
    BigInteger.Parse("1234567890123456789012345678")
,   BigInteger.Parse("2345678901234567890123456789")
,   ...
};
like image 71
Sergey Kalinichenko Avatar answered Dec 21 '22 23:12

Sergey Kalinichenko


From .Net Framework 4.0 Microsoft introduced System.Numerics.dll which contains a BigInteger structure which can represents an arbitrarily large signed integer. for more information you can refer to http://msdn.microsoft.com/en-us/library/system.numerics.biginteger%28v=vs.100%29.aspx

        BigInteger[] name =
        {
            BigInteger.Parse("9999999999999999999999999999999999999999999999999999999999999999999999"),
            BigInteger.Parse("9999999999999999999999999999999999999999999999999999999999999999999999")
        };

for older versions of framework you can use IntX library. you can obtain the package either from Nuget with Intall-Package IntX command or https://intx.codeplex.com/

        IntX[] name =
        {
            IntX.Parse("9999999999999999999999999999999999999999999999999999999999999999999999"),
            IntX.Parse("9999999999999999999999999999999999999999999999999999999999999999999999")
        };

other problem is the biggest integer literal you can define in c# is ulong with max value of 18,446,744,073,709,551,615 (larger values leads to compile error), which is obviously not enough in your case, easy solution would be to use BigInteger.Parse or in case of IntX library IntX.Parse.

like image 40
user3473830 Avatar answered Dec 22 '22 00:12

user3473830