473,804 Members | 2,265 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3541
* 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<intchunk s_; // 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<unsigned is 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<intchunk s_; // 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<unsigned is 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<unsigne dis 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

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

Similar topics

4
2207
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 -626177023 -1688330994 -834622062 -409108332 297174549 955187488 589884464 -1547848504 857311165 585616830 -749910209 194940864 -1102778558 -1282985276 -1220931512 792256075 -340699912 1496177106 1760327384
6
2878
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
5908
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 or solution when I search the web. I've seen a couple of suggestions searching Usenet, but neither of them works. One tried to use a union, and the other tried to convert each byte of the float.
19
7282
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
3383
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 is given.I must get y1-y4 from it. How can I do this on my 32bit PC?
22
4506
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 and and 5000 out :( I have a suspition is could be down to one of the number functions I am using along the way but im not sure.
58
6760
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
5973
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 run into problems is working with pointers. Here's where i'm at right now. I have a function that takes in the integers as strings and puts it into a struct
5
1773
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 arrays or string?
0
9594
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
10346
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
10347
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,...
0
10090
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9173
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5531
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...
1
4308
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
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.