C++ equivalent to Java's BigInteger



 DEVELOP > c-Plus-Plus > C++ equivalent to Java's BigInteger

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "mearvk"
Date: 05 Jan 2008 03:14:06 AM
Object: C++ equivalent to Java's BigInteger
Does C++ or C have something roughly equivalent to this:
http://java.sun.com/javase/6/docs/api/java/math/BigInteger.html
What I need is a way to quickly convert between decimal and binary and
from char*/string to a numeric representation.
Thanks!
.

User: "Jerry Coffin"

Title: Re: C++ equivalent to Java's BigInteger 05 Jan 2008 07:55:22 PM
In article <977f29eb-ba55-4ba2-914a-66cd951c8400
@i72g2000hsd.googlegroups.com>,
says...

Does C++ or C have something roughly equivalent to this:

http://java.sun.com/javase/6/docs/api/java/math/BigInteger.html

What I need is a way to quickly convert between decimal and binary and
from char*/string to a numeric representation.

A couple of possibilities include Arjen Lenstra's LIP package:
http://www.win.tue.nl/~klenstra/
and Victor Shoup's NTL packaage:
http://www.shoup.net/ntl/
LIP is quite small, and relatively easy to use, but the interface is
pure C (not C++) so (for example) reading a couple numbers, adding them
together, then writing out the result looks something like:
// warning: untested code, written from memory...
#include "lip.h"
int main() {
verylong x=0, y=0, z=0;
zread(&x);
zread(&y);
zadd(x, y, &z);
zwrite(z);
return 0;
}
A C++ interface using operator overloading could obviously improve
readability a bit (at the expense, perhaps, of slower code from creating
some temporaries).
Doing a bit of looking, it looks like I've made a couple of minor
modifications to the code. First of all, I commented out a prototype for
calloc at line 1116 of lip.c (it probably includes a standard header
that already prototypes it?). Second, I added:
#pragma comment(lib, "ws2_32.lib")
at line 495 of lip.h. offhand, I don't remember what network functions
it's using, and that's probably why I added this -- it saves having to
explicitly link against a library that isn't obviously related to
anything you're doing.
NTL is a much larger library (to put it mildly) but it's pretty easy to
use. It does include a C++ interface that does operator overloading, so
your code looks a bit more natural. Code equivalent to the above would
look something like:
// warning: untested code, written from memory...
#include <iostream>
#include "NTL/zz.h"
int main() {
NTL::ZZ x, y, z;
std::cin >> x >> y;
z = x + y;
std::cout << z;
return 0;
}
There are, of course, quite a few more out there as well, but I've found
both of these to work quite nicely under Windows.
--
Later,
Jerry.
The universe is a figment of its own imagination.
.

User: ""

Title: Re: C++ equivalent to Java's BigInteger 05 Jan 2008 03:37:17 AM
mearvk wrote:

Does C++ or C have something roughly equivalent to this:

http://java.sun.com/javase/6/docs/api/java/math/BigInteger.html

What I need is a way to quickly convert between decimal and binary and
from char*/string to a numeric representation.

Such facilities are not part of the C++ standard, but they are available as
third-party libraries. You could check whether the gnu mp bignum library
offers what you need.
Best
Kai-Uwe Bux
.
User: "Alf P. Steinbach"

Title: Re: C++ equivalent to Java's BigInteger 05 Jan 2008 04:41:08 AM
* jkherciueh@gmx.net:

mearvk wrote:

Does C++ or C have something roughly equivalent to this:

http://java.sun.com/javase/6/docs/api/java/math/BigInteger.html

What I need is a way to quickly convert between decimal and binary and
from char*/string to a numeric representation.


Such facilities are not part of the C++ standard, but they are available as
third-party libraries. You could check whether the gnu mp bignum library
offers what you need.

<url: http://java.sun.com/j2se/1.5.0/docs/guide/math/enhancements.html>
<quote>
Java 2 SDK 1.3
* Performance enhancements in BigInteger
Class java.math.BigInteger has been reimplemented in pure Java
programming-language code. Previously, BigInteger's implementation was
based on an underlying C library. On a high-quality VM, the new
implementation performs all operations faster than the old
implementation. The speed-up can be as much as 5x or more, depending on
the operation being performed and the length of the operands.
</quote>
Depending on how much of the above is misdirection, it could be truly
amazing.
If the 5x speedup is due only to avoiding using some inefficient way to
call native code and perhaps inefficient conversion between Java and
native data, then it's not at all amazing and not at all something
positive for Java: in that case, it just says that calling native codf
from Java is extremely inefficient.
But if the speedup is due only to e.g. optimization made possible by
just-in-time compilation, then there's a lesson here.
Anyways, to the OP, note that the original Java BigInt was just a Java
wrapper for the a C library, presumably GMP.
So the real question for a language like Java is, always, does it have
something roughly equivalent to the functionality available in C++?
Cheers, & hth.
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.
User: "James Kanze"

Title: Re: C++ equivalent to Java's BigInteger 05 Jan 2008 11:55:04 AM
On Jan 5, 11:41 am, "Alf P. Steinbach" <al...@start.no> wrote:

* jkherci...@gmx.net:

mearvk wrote:

Does C++ or C have something roughly equivalent to this:
http://java.sun.com/javase/6/docs/api/java/math/BigInteger.html
What I need is a way to quickly convert between decimal and binary and
from char*/string to a numeric representation.

Such facilities are not part of the C++ standard, but they
are available as third-party libraries. You could check
whether the gnu mp bignum library offers what you need.

<url:http://java.sun.com/j2se/1.5.0/docs/guide/math/enhancements.html>
<quote>
Java 2 SDK 1.3

* Performance enhancements in BigInteger
Class java.math.BigInteger has been reimplemented in pure Java
programming-language code. Previously, BigInteger's implementation was
based on an underlying C library. On a high-quality VM, the new
implementation performs all operations faster than the old
implementation. The speed-up can be as much as 5x or more, depending on
the operation being performed and the length of the operands.
</quote>
Depending on how much of the above is misdirection, it could
be truly amazing.

I presume that by "misdirection", you mean misinformation.

If the 5x speedup is due only to avoiding using some
inefficient way to call native code and perhaps inefficient
conversion between Java and native data, then it's not at all
amazing and not at all something positive for Java: in that
case, it just says that calling native codf from Java is
extremely inefficient.
But if the speedup is due only to e.g. optimization made
possible by just-in-time compilation, then there's a lesson
here.

I'd like to see the exact benchmarks first. I can imagine
certain cases where JIT could create such a speed up---e.g. if
all of the test cases involved constants, or in some very
specific cases of chained operations. A cross module optimizer
using profiling output could, at least potentially, also do some
of these optimizations. They probably don't apply universally,
however, and a lot of other cases (perhaps more typical) won't
show a similar speed up.
What is sure, of course, is that there is no cross-module
optimization between Java and C routines called via JNI, where
as all of the JIT compilers do cross-module optimization
(relatively easy, given the environment in which they operate).
And it shouldn't be very difficult at all to design a benchmark
which benefits particularly from such cross-module optimization.

Anyways, to the OP, note that the original Java BigInt was
just a Java wrapper for the a C library, presumably GMP.
So the real question for a language like Java is, always, does
it have something roughly equivalent to the functionality
available in C++?

The real difference here, of course, is that the library is
standard in Java, so 1) all Java programmers will know where to
look for it, if they need it, and 2) all Java programmers will
use the same library. Being standard is an advantage. (Whether
it's an important advantage is another question. In the case of
string, I think it definitely is, but in this case, I'm not so
sure.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.
User: "mearvk"

Title: Re: C++ equivalent to Java's BigInteger 05 Jan 2008 05:06:58 PM
On Jan 5, 12:55=A0pm, James Kanze <james.ka...@gmail.com> wrote:

On Jan 5, 11:41 am, "Alf P. Steinbach" <al...@start.no> wrote:





* jkherci...@gmx.net:

mearvk wrote:

Does C++ or C have something roughly equivalent to this:
http://java.sun.com/javase/6/docs/api/java/math/BigInteger.html
What I need is a way to quickly convert between decimal and binary an=

d

from char*/string to a numeric representation.

Such facilities are not part of the C++ standard, but they
are available as third-party libraries. You could check
whether the gnu mp bignum library offers what you need.

<url:http://java.sun.com/j2se/1.5.0/docs/guide/math/enhancements.html>
<quote>
Java 2 SDK 1.3


=A0 =A0 =A0* Performance enhancements in BigInteger
=A0 =A0 =A0 =A0Class java.math.BigInteger has been reimplemented in pure=

Java

programming-language code. Previously, BigInteger's implementation was
based on an underlying C library. On a high-quality VM, the new
implementation performs all operations faster than the old
implementation. The speed-up can be as much as 5x or more, depending on
the operation being performed and the length of the operands.
</quote>
Depending on how much of the above is misdirection, it could
be truly amazing.


I presume that by "misdirection", you mean misinformation.

If the 5x speedup is due only to avoiding using some
inefficient way to call native code and perhaps inefficient
conversion between Java and native data, then it's not at all
amazing and not at all something positive for Java: in that
case, it just says that calling native codf from Java is
extremely inefficient.
But if the speedup is due only to e.g. optimization made
possible by just-in-time compilation, then there's a lesson
here.


I'd like to see the exact benchmarks first. =A0I can imagine
certain cases where JIT could create such a speed up---e.g. if
all of the test cases involved constants, or in some very
specific cases of chained operations. =A0A cross module optimizer
using profiling output could, at least potentially, also do some
of these optimizations. =A0They probably don't apply universally,
however, and a lot of other cases (perhaps more typical) won't
show a similar speed up.

What is sure, of course, is that there is no cross-module
optimization between Java and C routines called via JNI, where
as all of the JIT compilers do cross-module optimization
(relatively easy, given the environment in which they operate).
And it shouldn't be very difficult at all to design a benchmark
which benefits particularly from such cross-module optimization.

Anyways, to the OP, note that the original Java BigInt was
just a Java wrapper for the a C library, presumably GMP.
So the real question for a language like Java is, always, does
it have something roughly equivalent to the functionality
available in C++?


The real difference here, of course, is that the library is
standard in Java, so 1) all Java programmers will know where to
look for it, if they need it, and 2) all Java programmers will
use the same library. =A0Being standard is an advantage. =A0(Whether
it's an important advantage is another question. =A0In the case of
string, I think it definitely is, but in this case, I'm not so
sure.)

--
James Kanze (GABI Software) =A0 =A0 =A0 =A0 =A0 =A0 email:james.ka...@gmai=

l.com

Conseils en informatique orient=E9e objet/
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Beratung in objektorientierter Date=

nverarbeitung

9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34- H=

ide quoted text -


- Show quoted text -

GMP looked very promising but it looks like it's intended primarily
for Linux. It is beyond my understanding to get it to compile on a
Windows environment, which for my client, is where it needs to run.
Thanks.
.




User: "Phil Endecott"

Title: Re: C++ equivalent to Java's BigInteger 07 Jan 2008 09:50:41 AM
mearvk wrote:

Does C++ or C have something roughly equivalent to this:

http://java.sun.com/javase/6/docs/api/java/math/BigInteger.html

What I need is a way to quickly convert between decimal and binary and
from char*/string to a numeric representation.

Try google("boost bigint"). IIRC there were proposals for a GSoC
project to do it; I don't think a "finished product" was ever delivered,
but you may be able to find something useable for your purposes, perhaps
in the Boost "sandbox" or "vault".
Phil.
.
User: "mearvk"

Title: Re: C++ equivalent to Java's BigInteger 10 Jan 2008 04:39:13 AM
On Jan 7, 10:50=A0am, Phil Endecott <spam_from_usenet_0...@chezphil.org>
wrote:

mearvk wrote:

Does C++ or C have something roughly equivalent to this:


http://java.sun.com/javase/6/docs/api/java/math/BigInteger.html


What I need is a way to quickly convert between decimal and binary and
from char*/string to a numeric representation.


Try google("boost bigint"). =A0IIRC there were proposals for a GSoC
project to do it; I don't think a "finished product" was ever delivered,
but you may be able to find something useable for your purposes, perhaps
in the Boost "sandbox" or "vault".

Phil.

I ended up using LIP. It was only 3 files and only required a little
coding on my part.
Thanks to everyone!
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER