473,411 Members | 2,186 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

very large integers

bob
If you were designing a class to represent very large integers in C++,
what kind of internal representation would be best?

Oct 8 '06 #1
13 3507
* bo*@coolgroups.com:
If you were designing a class to represent very large integers in C++,
what kind of internal representation would be best?
Depends. But if you're wondering about that you're probably best off
using one of the existing, free large integer libraries.

--
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?
Oct 8 '06 #2
bob wrote:
If you were designing a class to represent very large integers in C++,
what kind of internal representation would be best?
vector<int>?

Ooh, I can use http://www.google.com/codesearch for this one!

Searching for "bigint lang:c++ vector.int", without the quotes, I get this:

http://www.cs.hmc.edu/courses/mostRe...k/cs70ass2.tgz

class BigInt {
...
vector<intchunks_; // Value of the BigInt.

So it looks like they used the vector, like I suggested, as a "counting"
algorithm, treating each int as a symbol, in base 2-billion.

Question: Is this optimal, per vector's guarantees on the speed of
traversal, vs speed pushing back and such? Would a std::list perform better
or worse?

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Oct 8 '06 #3
Phlip wrote:
bob wrote:
>If you were designing a class to represent very large integers in C++,
what kind of internal representation would be best?

vector<int>?
vector<unsignedis far better. Writing semi-numeric algorithms with
signed types produces major headaches. Just ask anyone who's implemented
big integers in Java.

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Oct 8 '06 #4
Phlip wrote:
bob wrote:
>If you were designing a class to represent very large integers in C++,
what kind of internal representation would be best?

vector<int>?
For a generic implementation, I would prefer vector< unsigned long or more
generally vector<Twhere T is the largest unsigned arithmetic type for
which you have fast arithmetic. I would prefer unsigned types if two
conditions are met: (a) they wrap around on overflow and (b) they use
two-complement representation.

If speed is of the utmost importance (and in this business, it usually is) I
would have a look at what types are best supported on the machine level and
supply some assembler routines for the core functions of the class. That
would then determine most of the internal representation.

Most important: I would use a library instead of thinking about this myself.

Ooh, I can use http://www.google.com/codesearch for this one!

Searching for "bigint lang:c++ vector.int", without the quotes, I get
this:

http://www.cs.hmc.edu/courses/mostRe...k/cs70ass2.tgz

class BigInt {
...
vector<intchunks_; // Value of the BigInt.

So it looks like they used the vector, like I suggested, as a "counting"
algorithm, treating each int as a symbol, in base 2-billion.
Assuming ints in range [-2^31, 2^31), I would guess the basis is -2^32 and
not 2^31.

Question: Is this optimal, per vector's guarantees on the speed of
traversal, vs speed pushing back and such? Would a std::list perform
better or worse?
A list would perform much worse memory-wise. Very likely it would also be
much slower on almost any arithmetic algorithm. Memory locality is better
in a vector. Also, many of the fast arithmetic algorithms require random
access iterators over the string of digits.

A little bit on the wicked side: one could also try

class big_int {

unsigned long * digits;
long length;

};

where one uses the sign of length as the sign of the represented number.
Best

Kai-Uwe Bux
Oct 8 '06 #5
Pete Becker wrote:
>vector<int>?
vector<unsignedis far better.
Yet note that Google agreed with me, because I included the search regex
vector.int! And note the result I found was clearly labeled "homework".
Writing semi-numeric algorithms with signed types produces major
headaches.
Agreed - I said as much myself when I pointed out that the underlying
algorithm was counting in "base 2 billion".

I did not think that thru to its logical conclusion, that the sign only
appears once in a BigInt, so it should not repeat in all the elements.
Just ask anyone who's implemented big integers in Java.
Could you limit each element manually to a managably small positive range?
Waste bits to make the code more maintainable?

And why are we agreeing on vector, if pushing and popping might need to be
faster, and random access is irrelevent, right?

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Oct 9 '06 #6
Phlip wrote:
Pete Becker wrote:

>Just ask anyone who's implemented big integers in Java.

Could you limit each element manually to a managably small positive range?
Waste bits to make the code more maintainable?
Yup. Half the bits, minus 1.
>
And why are we agreeing on vector, if pushing and popping might need to be
faster, and random access is irrelevent, right?
I wasn't agreeing, just replacing int with unsigned.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Oct 9 '06 #7
Phlip wrote:
Pete Becker wrote:
>>vector<int>?
>vector<unsignedis far better.
[snip]
And why are we agreeing on vector, if pushing and popping might need to be
faster, and random access is irrelevent, right?
For addition: yes. For multiplication: yes, if you do the quadratic
algorithm from elementary school. However, some of the faster algorithms
need random access for instance to split the numbers into upper and lower
halves.

Moreover, why would you need to push and pop? For every arithmetic operation
you can get a fairly accurate estimate for the length of the result by
looking at the lengths of the operands:

digits( a+b ) <= 1 + max( digits(a) , digits(b) )
digits( a*b ) <= digits(a) + digits(b)
Best

Kai-Uwe Bux
Oct 9 '06 #8
Kai-Uwe Bux wrote:
>And why are we agreeing on vector, if pushing and popping might need to
be
faster, and random access is irrelevent, right?

For addition: yes. For multiplication: yes, if you do the quadratic
algorithm from elementary school. However, some of the faster algorithms
need random access for instance to split the numbers into upper and lower
halves.

Moreover, why would you need to push and pop?
From the back, not the front; for the quadratic equation from elementary
school, right?

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Oct 9 '06 #9
bob

Phlip wrote:
Kai-Uwe Bux wrote:
And why are we agreeing on vector, if pushing and popping might need to
be
faster, and random access is irrelevent, right?
For addition: yes. For multiplication: yes, if you do the quadratic
algorithm from elementary school. However, some of the faster algorithms
need random access for instance to split the numbers into upper and lower
halves.

Moreover, why would you need to push and pop?

From the back, not the front; for the quadratic equation from elementary
school, right?

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Any ideas how the division would work? Just use elementary school
style?

Oct 9 '06 #10
Phlip wrote:
Kai-Uwe Bux wrote:
>>And why are we agreeing on vector, if pushing and popping might need to
be
faster, and random access is irrelevent, right?

For addition: yes. For multiplication: yes, if you do the quadratic
algorithm from elementary school. However, some of the faster algorithms
need random access for instance to split the numbers into upper and lower
halves.

Moreover, why would you need to push and pop?

From the back, not the front; for the quadratic equation from elementary
school, right?
The classical methods for addition and subtraction give you the digits of,
say, the sum one by one in order (from least significant to most
significant).

The multiplication algorithm from elementary school (which has quadratic
complexity) does not yield the digits of the product in any nice particular
order. It gives you a bunch of partial products that you need to shift and
add. However, there is a variation of that algorithm that can be
implemented using just push as the access primitive:

345
x175
------
032825 // ones x ones tens x tens hundreds x hundreds
2120 // tens x ones hundreds x tens
0435 // ones x tens tens x hundreds
15 // hundreds x ones
05 // ones x hundreds
========
060375

Each of the combined partial products can be effectively computed in
two-digit chunks that do not overlap. You still have to add, but addition
can be done just using push.

On the other hand, even if you can formulate these algorithms so that push
is the only primitive they use, it still does not make list a good
container for this purpose: in all cases, you know in advance how many
digits to expect and hence you can reserve enough memory from the get go.
In that case, push and pop in a vector are presumably not slower but faster
than push and pop in a list.
Best

Kai-Uwe Bux
Oct 9 '06 #11
bob wrote:
Any ideas how the division would work? Just use elementary school
style?
How did the homework I cited do it?

Here's a better Google search:

http://www.google.com/codesearch
vector.unsigned lang:"c++" file:bigint

The second hit has a big working example with this comment:

/* the division operator is based on the algorithm D in Knuth The Art of
Computer Programing
Vol 2 (third edition), page 272 (Algorithm D). */

I detest all the if(DEBUG) stuff, though!

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Oct 9 '06 #12
bo*@coolgroups.com wrote:
>
Phlip wrote:
>Kai-Uwe Bux wrote:
>And why are we agreeing on vector, if pushing and popping might need
to be
faster, and random access is irrelevent, right?

For addition: yes. For multiplication: yes, if you do the quadratic
algorithm from elementary school. However, some of the faster
algorithms need random access for instance to split the numbers into
upper and lower halves.

Moreover, why would you need to push and pop?

From the back, not the front; for the quadratic equation from elementary
school, right?

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!

Any ideas how the division would work? Just use elementary school
style?
D.E. Knuth describes an intersting division algorithm using Newtons method.
It reduces division to multiplication so that division is in the same
run-time complexity class as the underlying multiplication algorithm. For
details see TAOCP, Vol. 2, 4.3.3/D, pages 311ff.
Best

Kai-Uwe Bux
Oct 9 '06 #13
"Phlip" <ph******@yahoo.comwrote in message
news:fC*******************@newssvr13.news.prodigy. com...
: Yet note that Google agreed with me, because I included the search
regex
: vector.int! And note the result I found was clearly labeled
"homework".
....
: Could you limit each element manually to a managably small positive
range?
: Waste bits to make the code more maintainable?

Of course.
Actually, if printing out the number (in base-10 representation) is
a key operation, using e.g. base-10000 (for a 16-bit short, 32-bit
long), might be a good choice.
That's what I would do if I needed a quick-and-dirty bigint class to
print Fibonacci numbers or factorials as a homework assignment...

hth-Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com

Oct 9 '06 #14

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

Similar topics

4
by: DJTB | last post by:
Hi, I'm trying to manually parse a dataset stored in a file. The data should be converted into Python objects. Here is an example of a single line of a (small) dataset: 3 13 17 19...
6
by: Michael Zielinski | last post by:
I was wondering if anybody knows of a package that does arbitrarily large integers that I can get preferably for free.
4
by: oddstray | last post by:
Hi, I have a number which is larger than the max unsigned long int. I don't have 64-bit integers available to me. I need to get the resulting 40-bit hex string. I can't find any algorithm...
19
by: shanx__=|;- | last post by:
hi i need some help regarding use of very very long integer datatype in 'c'.. i need it to store result of large number's factorial.. if someone can healp it would be a delight..
42
by: yong | last post by:
Hi all I have an large integer in this format x1*256^5 + x2*256^4 + x3*256^3 + x4*256^2 + x5*256 + x6 now I must convert it to this format y1*900^4 + y2*900^3 + y3*900^2 + y4*900 + y5 x1-x5...
22
by: Frinton | last post by:
Hi, I am trying to do some calculations on large numbers (ie 7,768,489,957,892,578,474,792,094 / 12,280) and no matter what I do it doesn't get it quite right. Its always somewhere between 10...
58
by: mailursubbu | last post by:
Hi How to write a program to get the factorial of 4096. I am working on a Linux m/c. Best Regards, Subra
4
by: Shisou | last post by:
hello everyone, Well I tried to solve this one on my own but it seems i need your help again :\ I'm trying to write a program to add or subtract two large integers... easy enough.. Where i...
5
by: sachin770 | last post by:
i am a beginner .can you give me idea how we can manipulate "LARGE" integers (positive or negative) of arbitrary size. These integers could be greater than MAX_INT or smaller than MIN_INT. using...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.