locked
How to use BigInteger in C#

    Question

  • The following c# sample program on VS2005 gives the following error:

    No overload for method 'BigInteger' takes '1' arguments

    Could this be because of vjslib.dll? I have J# installed. Anyway that is a dll, is there any lib to be used? Or any reference to be done? My .Net is 2.0, should I go tp .net 4.0? Would that help?



    using System;
    using System.IO;
    using Microsoft.CSharp;

    namespace Test
    {
        class BigInteger
        {
            static void Main(string[] args)
            {
                BigInteger bigIntFromDouble = new BigInteger(179032.6541);
                Console.WriteLine(bigIntFromDouble);
                BigInteger bigIntFromInt64 = new BigInteger(934157136952);
                Console.WriteLine(bigIntFromInt64);
            }
        }
    }

    Wednesday, November 25, 2009 1:30 AM

Answers

  • Hi ananda vardhana,

    I suggest you use VS2010, which use .net framework 4.0. One of the simplest new additions to the .NET 4.0 framework is an integer with arbitrary length, System.Numerics.BigInteger. Here’s a simple usage example, comparing it to a double:

     

    BigInteger b = BigInteger.Pow(2, 64);

    Console.WriteLine("BigInteger: {0}", b.ToString("N"));

    Console.WriteLine("Double:     {0}", Math.Pow(2, 64).ToString("N"));

    Console.WriteLine();

    b = BigInteger.Pow(2, 128);

    Console.WriteLine("BigInteger: {0}", b.ToString("N"));

    Console.WriteLine("Double:     {0}", ((double)b).ToString("N"));

    For more information:
    http://msdn.microsoft.com/en-us/library/system.numerics.biginteger(VS.100).aspx

    Have a nice day.

    Best regards,

    Guang-Ming Bian - MSFT
    MSDN Subscriber Support
    in Forum
    If you have any feedback on our support, please contact
    msdnmg@microsoft.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    Wednesday, November 25, 2009 9:41 AM
  • The sample compiles and runs without problem:

    using System;
    using System.Numerics;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                BigInteger b = BigInteger.Pow(2, 64);
                Console.WriteLine("BigInteger: {0}", b.ToString("N"));
                Console.WriteLine("Double:     {0}", Math.Pow(2, 64).ToString("N"));
                Console.WriteLine();
                b = BigInteger.Pow(2, 128);
                Console.WriteLine("BigInteger: {0}", b.ToString("N"));
                Console.WriteLine("Double:     {0}", ((double)b).ToString("N"));
                Console.ReadLine();
            }
        }
    }

    Output:

    BigInteger: 18,446,744,073,709,551,616.00
    Double:     18,446,744,073,709,600,000.00

    BigInteger: 340,282,366,920,938,463,463,374,607,431,768,211,456.00
    Double:     340,282,366,920,938,000,000,000,000,000,000,000,000.00

    This is equivalent code in VS2005:

    using System;
    using java.math;
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          BigInteger b = new BigInteger(Convert.ToString(UInt64.MaxValue));
          b = b.add(new BigInteger(Convert.ToString(1)));
          Console.WriteLine("BigInteger: {0}", b);
          Console.WriteLine("Double:     {0}", Math.Pow(2, 64).ToString("N"));
          Console.WriteLine();
          b = b.multiply(b);
          Console.WriteLine("BigInteger: {0}", b);
          Console.WriteLine("Double:     {0}", Math.Pow(2, 128).ToString("N"));
          Console.ReadLine();
        }
      }
    }
    


    Output:

    BigInteger: 18446744073709551616
    Double:     18,446,744,073,709,600,000.00

    BigInteger: 340282366920938463463374607431768211456
    Double:     340,282,366,920,938,000,000,000,000,000,000,000,000.00

    Thursday, November 26, 2009 9:41 AM

All replies

  • Hi,

    Try this


    BigInteger b1 = BigInteger.Parse("23098230498230983409822324234234897657821687324872335523234634463445");
    Please mark the response as answers if it solves your question or vote as helpful if you find it helpful. http://thoughtorientedarchitecture.blogspot.com/
    • Proposed as answer by Haripraghash Wednesday, November 25, 2009 2:42 AM
    Wednesday, November 25, 2009 2:42 AM
  • Per my post in your companion thread:  BIG Integer Library suggestion.  In order to use methods and properties of a namespace, you must reference the namespace in your project.  To use the components of the namespace without qualification, you must place a using statement as the very first statement in your .cs file.

    Wednesday, November 25, 2009 6:51 AM
  • Hi ananda vardhana,

    I suggest you use VS2010, which use .net framework 4.0. One of the simplest new additions to the .NET 4.0 framework is an integer with arbitrary length, System.Numerics.BigInteger. Here’s a simple usage example, comparing it to a double:

     

    BigInteger b = BigInteger.Pow(2, 64);

    Console.WriteLine("BigInteger: {0}", b.ToString("N"));

    Console.WriteLine("Double:     {0}", Math.Pow(2, 64).ToString("N"));

    Console.WriteLine();

    b = BigInteger.Pow(2, 128);

    Console.WriteLine("BigInteger: {0}", b.ToString("N"));

    Console.WriteLine("Double:     {0}", ((double)b).ToString("N"));

    For more information:
    http://msdn.microsoft.com/en-us/library/system.numerics.biginteger(VS.100).aspx

    Have a nice day.

    Best regards,

    Guang-Ming Bian - MSFT
    MSDN Subscriber Support
    in Forum
    If you have any feedback on our support, please contact
    msdnmg@microsoft.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    Wednesday, November 25, 2009 9:41 AM
  • You didn't define a constructor in your BigInteger class. The default constructor takes no argument.

    If the BigInteger you want to use is defined somewhere else, you should either fully qualify its name when you use it, or rename your class.

    Wednesday, November 25, 2009 10:10 AM
  • Hi Guang-Ming,

    I installed 2010, Beta 2. Now am having .Net 4.0.21006. I need to add System.core.dll assembly before I can include System.Numeric right? So I treid to add it. Mine is a 64 bit machine. I tried it from both Framework64 and Framework. Both times it puts put a big long warning box saying:

    "System.Core.dll or one of its dependencies, require a later version of the .Net Framewrok than the one specified in the project......." it did not make sense ...

    What does it mean? I did not want to ignore it. Mine  is a Windows Server 2003, SP2,. 64 bit system

     Directory of C:\WINDOWS\Microsoft.NET\Framework\v4.0.21006

    10/07/2009  02:44 AM         1,257,800 System.Core.dll
                   1 File(s)      1,257,800 bytes

     Directory of C:\WINDOWS\Microsoft.NET\Framework64\v4.0.21006

    10/07/2009  02:44 AM         1,257,800 System.Core.dll
                   1 File(s)      1,257,800 bytes

    All others who responded to my email. Many thanks. Will get back to you all once I get my first hurdle crossed.

    thanks
    ananda

    Wednesday, November 25, 2009 10:46 PM
  • Hi ananda,

    I tried your sample, it works fine, I can't reproduce the the problem. I found even I delete the system.Core.dll reference from the project, it still works. So here I suggest you delete the reference and try again.

    I also install vs2010 beta 2 version, with .net framework 4.0.21006. It works fine in my local.



    Have a nice day.

    Best regards,

    Guang-Ming Bian - MSFT
    MSDN Subscriber Support
    in Forum
    If you have any feedback on our support, please contact
    msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    Thursday, November 26, 2009 8:12 AM

  • I tried your sample, it works fine, I can't reproduce the the problem.
    The sample cannot compile. BigInteger's constructor is not defined. Its default constructor takes no parameters. You have changed something in order for it to compile.
    Thursday, November 26, 2009 8:56 AM
  • The sample compiles and runs without problem:

    using System;
    using System.Numerics;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                BigInteger b = BigInteger.Pow(2, 64);
                Console.WriteLine("BigInteger: {0}", b.ToString("N"));
                Console.WriteLine("Double:     {0}", Math.Pow(2, 64).ToString("N"));
                Console.WriteLine();
                b = BigInteger.Pow(2, 128);
                Console.WriteLine("BigInteger: {0}", b.ToString("N"));
                Console.WriteLine("Double:     {0}", ((double)b).ToString("N"));
                Console.ReadLine();
            }
        }
    }

    Output:

    BigInteger: 18,446,744,073,709,551,616.00
    Double:     18,446,744,073,709,600,000.00

    BigInteger: 340,282,366,920,938,463,463,374,607,431,768,211,456.00
    Double:     340,282,366,920,938,000,000,000,000,000,000,000,000.00

    This is equivalent code in VS2005:

    using System;
    using java.math;
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          BigInteger b = new BigInteger(Convert.ToString(UInt64.MaxValue));
          b = b.add(new BigInteger(Convert.ToString(1)));
          Console.WriteLine("BigInteger: {0}", b);
          Console.WriteLine("Double:     {0}", Math.Pow(2, 64).ToString("N"));
          Console.WriteLine();
          b = b.multiply(b);
          Console.WriteLine("BigInteger: {0}", b);
          Console.WriteLine("Double:     {0}", Math.Pow(2, 128).ToString("N"));
          Console.ReadLine();
        }
      }
    }
    


    Output:

    BigInteger: 18446744073709551616
    Double:     18,446,744,073,709,600,000.00

    BigInteger: 340282366920938463463374607431768211456
    Double:     340,282,366,920,938,000,000,000,000,000,000,000,000.00

    Thursday, November 26, 2009 9:41 AM
  • Of course, it compiles. You changed it.
    Thursday, November 26, 2009 3:33 PM
  • Of course, it compiles. You changed it.

    What are you talking about?  I copied Guang-Ming Bian's code and pasted it into the main method of a console application.  I made no changes to the code.
    Thursday, November 26, 2009 4:04 PM
  • Let's see.

    ananda vardhana:
    "The following c# sample program on VS2005 gives the following error:
    No overload for method 'BigInteger' takes '1' arguments"

    Guang-Ming Bian:
    "Hi ananda,
    I tried your sample, it works fine, I can't reproduce the the problem."

    Me:
    "The sample cannot compile. BigInteger's constructor is not defined."

    You:
    "The sample compiles and runs without problem"

    I thought you were replying to me. The sample Guang-Ming Bian was talking about was not the one you tried.
    Thursday, November 26, 2009 4:38 PM
  • Hi John,

    If you can kindly go to your soultion explorer and click on the the reference and tell me what all you are referencing in VS2010 and VS2005 that would be very helpful for me. The reason is java.math gives me the error "Missing directive" in VS2005 and Systems.Numerics also gives the same error. On VS2010 when I tried to include the reference System.core.dll I ran into other issue (Please refer to me earlier message) In gist System.Numeric also complains of missing directive. Right now I am installing VS2010 on a 32 bit system. However my preferred syste, is the 64 bit system.

    Hi Guang-Ming,

    Thanks for trying out my setup. However I still have all the problems of yesterday. Let me wait to for John's reply and see.

    Hi Louis,

    >>BigInteger's constructor is not defined. Its default constructor takes no parameters.

    Could you please give some code example for the above sentance, then there will be no doubts and will help me also.

    Thanks to all for spending time to help me on a Thanksgiving day!! How appropriate!!

    regards
    ananda

    Thursday, November 26, 2009 5:50 PM
  • The VS2010 references are:

        System
        System.Numerics

    The VS2005 references are:

        System
        vjslib

    Since vjslib is a 32 bit library, you should target x86 to run the code on a 64 bit OS.
    Thursday, November 26, 2009 6:22 PM
  • Hi Everyone,

    Finally on a 32 bit system with VS2010 it worked fine. 64 bit system VS2010 still has problem. I think this might be a VS 2010 Beat 2 issue. I will call MSDN support and get solution from them. I have not tried the VS2005 with the vjslib yet.

    I think I have got all the help I needed, this case may be closed.

    Thanks again to all and have a nice thanksgivig holiday.

    regards
    ananda
    Thursday, November 26, 2009 7:43 PM
  • >>BigInteger's constructor is not defined. Its default constructor takes no parameters.

    Could you please give some code example for the above sentance, then there will be no doubts and will help me also.

    In your first post, you had your Main inside a class named BigInteger.

    Since you didn't fully qualify BigInteger in your code, the compiler assumed it was the enclosing one you were referencing and it looked for a constructor with one parameter in that class. It couldn't find one, so it couldn't compile.

    Friday, November 27, 2009 9:54 AM