Usage example for BigInteger and C++

  • Thread starter Thread starter GeorgCantor
  • Start date Start date
G

GeorgCantor

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

Please can someone give a simple usage example?

Thank you.
 
GeorgCantor said:
One of the new additions to the .NET 4.0 framework is
System.Numerics.BigInteger.

Please can someone give a simple usage example?

Thank you.

I'm as frustrated as you are. After reading your post and the responses you
got, I thought "how hard could it be?" and now I'm stumped. So, meaning no
disrespect to anybody, I'm forwarding this to the C++/CLI group to see what
happens :-)
 
Hi PvdG42!
I'm as frustrated as you are. After reading your post and the responses
you got, I thought "how hard could it be?" and now I'm stumped. So,
meaning no disrespect to anybody, I'm forwarding this to the C++/CLI
group to see what happens :-)

What examples do you want to see?
It is no big problem to translate C# into C++/CLI...

For example:
BigInteger negativeNumber = -1000000;
BigInteger positiveNumber = 15777216;

string negativeHex = negativeNumber.ToString("X");
string positiveHex = positiveNumber.ToString("X");

BigInteger negativeNumber2, positiveNumber2;
negativeNumber2 = BigInteger.Parse(negativeHex,
NumberStyles.HexNumber);
positiveNumber2 = BigInteger.Parse(positiveHex,
NumberStyles.HexNumber);

Console.WriteLine("Converted {0:N0} to {1} back to {2:N0}.",
negativeNumber, negativeHex, negativeNumber2);

Console.WriteLine("Converted {0:N0} to {1} back to {2:N0}.",
positiveNumber, positiveHex, positiveNumber2);


=>

BigInteger negativeNumber = -1000000;
BigInteger positiveNumber = 15777216;

String^ negativeHex = negativeNumber.ToString("X");
String^ positiveHex = positiveNumber.ToString("X");

BigInteger negativeNumber2, positiveNumber2;
negativeNumber2 = BigInteger::Parse(negativeHex,
NumberStyles.HexNumber);
positiveNumber2 = BigInteger::Parse(positiveHex,
NumberStyles.HexNumber);

Console::WriteLine("Converted {0:N0} to {1} back to {2:N0}.",
negativeNumber, negativeHex, negativeNumber2);

Console::WriteLine("Converted {0:N0} to {1} back to {2:N0}.",
positiveNumber, positiveHex, positiveNumber2);


--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
PvdG42 said:
I'm as frustrated as you are. After reading your post and the responses
you got, I thought "how hard could it be?" and now I'm stumped. So,
meaning no disrespect to anybody, I'm forwarding this to the C++/CLI
group to see what happens :-)

If you are talking about the thread on the MSDN forum, then I confess that I
incurred the wrath of the OP by refusing to answer his question, but simply
pointed out that *if you know C++/CLI* then translating from C# is very easy,
and is a skill one needs to learn. I then asked him what he had tried.

However if you read the whole thread, I think you will find that the real
problem is not that the OP does not know how to use BigInteger, but rather that
he does not know anything about C++/CLI.

If you are talking about the MSDN .NET documentation, it is regrettable that
there are often no examples in C++/CLI, but that is just the way it is.
Microsoft has given up on trying to make C++/CLI a first class citizen in
..NET-land.
 
Jochen Kalmbach said:
Hi PvdG42!


What examples do you want to see?
It is no big problem to translate C# into C++/CLI...

For example:
BigInteger negativeNumber = -1000000;
BigInteger positiveNumber = 15777216;

string negativeHex = negativeNumber.ToString("X");
string positiveHex = positiveNumber.ToString("X");

BigInteger negativeNumber2, positiveNumber2;
negativeNumber2 = BigInteger.Parse(negativeHex,
NumberStyles.HexNumber);
positiveNumber2 = BigInteger.Parse(positiveHex,
NumberStyles.HexNumber);

Console.WriteLine("Converted {0:N0} to {1} back to {2:N0}.",
negativeNumber, negativeHex, negativeNumber2);
Console.WriteLine("Converted {0:N0} to {1} back to {2:N0}.",
positiveNumber, positiveHex, positiveNumber2);

=>

BigInteger negativeNumber = -1000000;
BigInteger positiveNumber = 15777216;

String^ negativeHex = negativeNumber.ToString("X");
String^ positiveHex = positiveNumber.ToString("X");

BigInteger negativeNumber2, positiveNumber2;
negativeNumber2 = BigInteger::Parse(negativeHex,
NumberStyles.HexNumber);
positiveNumber2 = BigInteger::Parse(positiveHex,
NumberStyles.HexNumber);

Console::WriteLine("Converted {0:N0} to {1} back to {2:N0}.",
negativeNumber, negativeHex, negativeNumber2);
Console::WriteLine("Converted {0:N0} to {1} back to {2:N0}.",
positiveNumber, positiveHex, positiveNumber2);

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

That's exactly the kind of thing I thought would work as a demo, and I
appreciate your code. I also agree that translation from C# to C++/CLI for
demos should be trivial.

So, (in VS 2010 Premium b2 running on Win 7 x64) I created a C++/CLI console
app, added:

using namespace System::Numerics;

then put some demo code you offered in main(),

I get:
"error C2039: 'Numerics': is not a member of 'System'"
"error C2871: 'Numerics': a namespace with this name does not exist"

I'm sure it’s something my old brain has simply forgotten?
 
David Wilkinson said:
If you are talking about the thread on the MSDN forum, then I confess that
I
incurred the wrath of the OP by refusing to answer his question, but
simply
pointed out that *if you know C++/CLI* then translating from C# is very
easy,
and is a skill one needs to learn. I then asked him what he had tried.

However if you read the whole thread, I think you will find that the real
problem is not that the OP does not know how to use BigInteger, but rather
that
he does not know anything about C++/CLI.

If you are talking about the MSDN .NET documentation, it is regrettable
that
there are often no examples in C++/CLI, but that is just the way it is.
Microsoft has given up on trying to make C++/CLI a first class citizen in
.NET-land.

Hi, David :-)
No good deed goes unpunished, eh? I've been there many times. I was not
aware of the extended thread. The thread I forwarded from was started by
"GeorgCantor" in dotnet.framework, I thought more could be learned here, as
it has.
I fully agree that it's a shame the docs don't include more C++/CLI example
code. I also agree that for demos, translation from C# should be easy.
Anyway, thanks for your input!
 
Hi PvdG42!
"error C2039: 'Numerics': is not a member of 'System'"
"error C2871: 'Numerics': a namespace with this name does not exist"

You either need to set the target-framework to .NET 4 or add the
appropriate reference:

- System.Numerics.dll

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
GeorgCantor said:
Haha, very funny. And now please show me what you have found, ok?

Georg:

Perhaps you see a common pattern between this thread and the one on the forums
where you asked this same question.

People are very very willing to help you, but when they sense that you are not
prepared to make the slightest effort yourself, and do do not show anything that
you might have tried, they tend to hold back from spoon-feeding you the answer.

Translating from C# to C++/CLI is usually very easy (even if you don't know C#,
as I don't, really). Even if you just paste the C# code into Visual C++ without
changing anything, you should get some error messages that point you in the
right direction.
 
GeorgCantor said:
One of the new additions to the .NET 4.0 framework is
System.Numerics.BigInteger.
Please can someone give a simple usage example?

Hi Georg,

(1) make a new project choosing the project template "CLR Cosole
Application" (not "Win32 Console Application" as I did :)

(2) Next, 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.
(Thanks to Brian Muth for this hint.)

(3) Finally here a small piece of code:

#include "stdafx.h"
using namespace System;
using namespace System::Numerics;

int main()
{
int n, limit = 44;
BigInteger f = 1;

for (n = 0; n < limit; n++)
{
Console::WriteLine("{0}! = {1}", n, f.ToString());
f = BigInteger::Multiply(f, n + 1);
}
Console::ReadLine();
return 0;
}

Hope this helps.
Cheers, Georg
 
Back
Top