locked
Simple usage example for BigInteger? RRS feed

  • Question

  • One of the new additions to the .NET 4.0 framework is System.Numerics.BigInteger .

    Please can someone give a simple usage example? (The docs show only examples for C#.)
    Sunday, January 17, 2010 5:55 PM

Answers

  • using namespace System;
    using namespace System::Numerics;
    
    int main(array<System::String ^> ^args)
    {
       BigInteger number = BigInteger::Add (Int64::MaxValue, Int32::MaxValue);
       Console::WriteLine(L"Result is {0}", number.ToString());
       return 0;
    }
    
    • Marked as answer by Georg Cantor Tuesday, October 18, 2011 2:06 PM
    Sunday, January 17, 2010 10:57 PM
  • The purpose of the forum is to help you to solve your problems, not to solve them for you.

    Here is a C# sample using BigInteger:

    BigInteger bigIntFromDouble = new BigInteger(179032.6541);
    Console.WriteLine(bigIntFromDouble);
    BigInteger bigIntFromInt64 = new BigInteger(934157136952);
    Console.WriteLine(bigIntFromInt64);

    If you know anything about C++/CLI I am sure that you can figure out how to translate it. The compiler is your friend.
    David Wilkinson | Visual C++ MVP
    • Marked as answer by Wesley Yao Friday, January 22, 2010 5:01 AM
    Sunday, January 17, 2010 11:16 PM
  • Now I know that there must be some other error, because I get:

    Error    1    error C2871: 'System' : a namespace with this name does not exist   
    Error    2    error C2653: 'System' : is not a class or namespace name   
    Error    3    error C2871: 'Numerics' : a namespace with this name does not exist  
    If you are getting this error, then you do not have a CLR project.

    Go to

    Project Properties->Configuration Properties->General->Common Language Runtime Support

    and select Common Language Runtime Support(/clr)

    David Wilkinson | Visual C++ MVP
    • Marked as answer by Wesley Yao Friday, January 22, 2010 5:01 AM
    Monday, January 18, 2010 1:01 AM
  • David indicated correctly that you need to ensure that the /clr compiler switch is chosen, and how to achieve that by setting the Common Language Runtime Support option. Alternatively, you can start with a brand new project, but be sure to pick the "CLR Console Application" project template under "Visual C++", "CLR". Next, you need to reference the System.Numerics class. Right-click on the project name in Solution Explorer, choose "References..." and press the "Add New Reference..." button. Under the .NET tab, choose System.Numerics.

    Also, add

    using namespace System;

    as the first line of code after #include "stdafx.h". I forgot that line in the code I pasted previously.



    • Marked as answer by Wesley Yao Friday, January 22, 2010 5:01 AM
    Monday, January 18, 2010 2:27 AM

All replies

  • If you are going to use C++/CLI you are going to have to learn how to translate from C# to C++/CLI.

    What have you tried?

    David Wilkinson | Visual C++ MVP
    • Edited by davewilk Monday, January 18, 2010 1:25 AM (typo)
    Sunday, January 17, 2010 7:47 PM
  • David> If you are going to use C++/CLI you are going to have to learn how to translate from C# to C++/CLI.

    This is not an helpful answer.

    David> What have toy tried?

    This is not an helpful answer.

    Sunday, January 17, 2010 10:29 PM
  • It may not be the answer you want, but it is in the long run the one you will need.  C++/CLI is not getting future development and .NET examples tend to be in C# and sometimes VB.

    If you are going to do significant C+/CLI development you will need to be able to deal with the documentation examples being in C#.
    Sunday, January 17, 2010 10:52 PM
  • using namespace System;
    using namespace System::Numerics;
    
    int main(array<System::String ^> ^args)
    {
       BigInteger number = BigInteger::Add (Int64::MaxValue, Int32::MaxValue);
       Console::WriteLine(L"Result is {0}", number.ToString());
       return 0;
    }
    
    • Marked as answer by Georg Cantor Tuesday, October 18, 2011 2:06 PM
    Sunday, January 17, 2010 10:57 PM
  • Simon> It may not be the answer you want, but it is in the long run the one you will need. 

    How do you know what my 'long run' is going to be? How do you know what I will need in the future?
    This is not a helpful answer.

    Simon> C++/CLI is not getting future development and .NET examples tend to be in C# and sometimes VB.

    This is not a helpful answer.

    Simon> If you are going to do significant C+/CLI development you will need to be able to deal with the documentation examples being in C#.

    This is not a helpful answer.
    Sunday, January 17, 2010 10:59 PM
  • The purpose of the forum is to help you to solve your problems, not to solve them for you.

    Here is a C# sample using BigInteger:

    BigInteger bigIntFromDouble = new BigInteger(179032.6541);
    Console.WriteLine(bigIntFromDouble);
    BigInteger bigIntFromInt64 = new BigInteger(934157136952);
    Console.WriteLine(bigIntFromInt64);

    If you know anything about C++/CLI I am sure that you can figure out how to translate it. The compiler is your friend.
    David Wilkinson | Visual C++ MVP
    • Marked as answer by Wesley Yao Friday, January 22, 2010 5:01 AM
    Sunday, January 17, 2010 11:16 PM
  • Thank you Brian! Simple as it is, it's all I needed.

    Now I know that there must be some other error, because I get:

    Error    1    error C2871: 'System' : a namespace with this name does not exist   
    Error    2    error C2653: 'System' : is not a class or namespace name   
    Error    3    error C2871: 'Numerics' : a namespace with this name does not exist  

    But I will not ask another question here.
    Thanks again Brian.
    Monday, January 18, 2010 12:12 AM
  • davewilk> The purpose of the forum is to help you to solve your problems, not to solve them for you.

    I did not ask you to solve my problems. I asked for a simple example.
    Your answers are quite strange to me.

    Monday, January 18, 2010 12:15 AM
  • Now I know that there must be some other error, because I get:

    Error    1    error C2871: 'System' : a namespace with this name does not exist   
    Error    2    error C2653: 'System' : is not a class or namespace name   
    Error    3    error C2871: 'Numerics' : a namespace with this name does not exist  
    If you are getting this error, then you do not have a CLR project.

    Go to

    Project Properties->Configuration Properties->General->Common Language Runtime Support

    and select Common Language Runtime Support(/clr)

    David Wilkinson | Visual C++ MVP
    • Marked as answer by Wesley Yao Friday, January 22, 2010 5:01 AM
    Monday, January 18, 2010 1:01 AM
  • davewilk> The purpose of the forum is to help you to solve your problems, not to solve them for you.

    I did not ask you to solve my problems. I asked for a simple example.
    Your answers are quite strange to me.


    I am just trying to help you find the answers for yourself. This is the skill that you need.

    No amount of getting answers to specific questions will substitute for it.


    David Wilkinson | Visual C++ MVP
    Monday, January 18, 2010 1:06 AM
  • David indicated correctly that you need to ensure that the /clr compiler switch is chosen, and how to achieve that by setting the Common Language Runtime Support option. Alternatively, you can start with a brand new project, but be sure to pick the "CLR Console Application" project template under "Visual C++", "CLR". Next, you need to reference the System.Numerics class. Right-click on the project name in Solution Explorer, choose "References..." and press the "Add New Reference..." button. Under the .NET tab, choose System.Numerics.

    Also, add

    using namespace System;

    as the first line of code after #include "stdafx.h". I forgot that line in the code I pasted previously.



    • Marked as answer by Wesley Yao Friday, January 22, 2010 5:01 AM
    Monday, January 18, 2010 2:27 AM