473,856 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ equivalent to Java's BigInteger

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

http://java.sun.com/javase/6/docs/ap...igInteger.html

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

Thanks!
Jan 5 '08 #1
6 13584
mearvk wrote:
Does C++ or C have something roughly equivalent to this:

http://java.sun.com/javase/6/docs/ap...igInteger.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
Jan 5 '08 #2
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/ap...igInteger.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.ht ml>
<quote>
Java 2 SDK 1.3

* Performance enhancements in BigInteger
Class java.math.BigIn teger 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 "misdirecti on", 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:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 5 '08 #3
On Jan 5, 12:55*pm, James Kanze <james.ka...@gm ail.comwrote:
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/ap...igInteger.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.ht ml>
<quote>
Java 2 SDK 1.3
* * ** Performance enhancements in BigInteger
* * * *Class java.math.BigIn teger has been reimplemented in pureJava
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 "misdirecti on", 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.ka. ..@gmail.com
Conseils en informatique orientée objet/
* * * * * * * * * *Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34- Hide 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.

Jan 5 '08 #4
In article <977f29eb-ba55-4ba2-914a-66cd951c8400
@i72g2000hsd.go oglegroups.com> , me****@gmail.co m says...
Does C++ or C have something roughly equivalent to this:

http://java.sun.com/javase/6/docs/ap...igInteger.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.
Jan 6 '08 #5
mearvk wrote:
Does C++ or C have something roughly equivalent to this:

http://java.sun.com/javase/6/docs/ap...igInteger.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.
Jan 7 '08 #6
On Jan 7, 10:50*am, Phil Endecott <spam_from_usen et_0...@chezphi l.org>
wrote:
mearvk wrote:
Does C++ or C have something roughly equivalent to this:
http://java.sun.com/javase/6/docs/ap...igInteger.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.
I ended up using LIP. It was only 3 files and only required a little
coding on my part.

Thanks to everyone!
Jan 10 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
12990
by: Jerry | last post by:
Hi, I am writing a Java Chatroom application that will implement encryption of messages using the RSA algorithm using the BigInteger class. It uses socket connections to exchange messages. I have managed to generated the Private and Public keys and exchanged public keys and modulus between the clients and server. Please could someone advise me how encrypt a short message, pass the encrypted message across a socket connection and then...
73
8115
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities as a standard portable part of the core language, the LISP package, and the java.lang package, respectively. Both have big integers, although only LISP has rationals as far as I can tell. Because CL supports keyword arguments, it has a wider range...
0
1737
by: Krzysztof Adamski | last post by:
Hi, Is it possible in jython to map an PyLong object to other class than BigInteger (i.e. java.lang.Long) ? Thanks for you help, Krzysztof Adamski
4
6410
by: adrin | last post by:
I have to create a c++ class similar to BigInteger from java... but i have no idea, how can i implement big numbers at all(mabye store every digit in list element? :)) how is it properly done? thanks for help
458
21610
by: wellstone9912 | last post by:
Java programmers seem to always be whining about how confusing and overly complex C++ appears to them. I would like to introduce an explanation for this. Is it possible that Java programmers simply aren't smart enough to understand C++? This is not merely a whimsical hypothesis. Given my experience with Java programmers --- the code they write and the conversations they have --- Occam's Razor points to this explanation. For example,...
19
6476
by: shana07 | last post by:
I don't understand about this program is doing. Could someone please tell me what it is about. Because I have gcd function in one program that calls this function. Thanks a lot switch (opcode) { case GCD: out = b1.gcd(b2).toString(); break; /** * Returns a BigInteger whose value is the greatest common divisor of * <tt>abs(this)</tt> and <tt>abs(val)</tt>. Returns 0 if * <tt>this==0 &amp;&amp;...
2
2851
by: gemacjr1201 | last post by:
I am creating a GUI for class and need help with my program. My program takes 2 big integers and adds them together. First how do I declare my 2 Big Integers and then add them together? I have read the API docs , but still need help. Thanks! import java.math.BigInteger; public class Integer { private BigInteger IntegerONE; // both entered as strings from GUI private BigInteger IntegerTWO; public Integer() {
2
2787
by: jehugaleahsa | last post by:
Hello: I am currently working on a some simple (not really) classes called BigInteger and BigRational. They are pretty much identical to Java's BigInteger and BigDecimal classes, respectfully. BigRational is really simple and it just maintains a dividend and denominator and then prints out the decimal form when done with the calculation.
11
10539
by: imranisb | last post by:
Hi, Im developing RSA cryptography based application in VC#.Net 2.0 and need to use "BigInteger.probablePrime()". I know that VC#.Net 2.0 doesn't have the BigInteger class but found that one buddy created BigInteger class but it is missing some overloading function like BigInteger.probablePrime(). The code of the BigInteger class developed in the C#.Net can be found here: http://www.codeproject.com/KB/cs/biginteger.aspx
0
9760
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11052
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10695
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10777
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7930
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5759
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5956
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4573
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4172
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.