Integer types and overflow

Hello!

I'm very much a beginner at programming. I'm teaching myself by trial-and-error whilst my partner is taking a basic C++ class at Uni. He wrote most of this program, but I've done a fair bit of the nitpicky stuff, like stdout to text file and the initial looping function. I'm pretty familiar with basic syntax, but I know about as much as someone would after a week of class. Suffice it to say, I'm very new and inexperienced!

So this program we've got is a basic Proth number tester. It takes the formula N=k*(2^n)-1 and asks user for k as well as lower and upper bound of n. It then tests N for k at all values of n between n(lower) and n(upper). It then prints those to a neat text file.

The main problem that irks me at the moment is that I don't know how to overcome an overflow error. The best way I can describe it is that it's similar to what I've seen in Java - cannot have more than 2^31 of "something."

Hence, our results file might look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
You chose 5 as your value of k.
You chose 28 as your lower bound and 29 as your upper bound.

Here are the results...

5*2^28-1 is 1342177279.

1342177279 is divisible by 17989 and has the factors 17989 and 74611.

1342177279 appears to be composite.


5*2^29-1 is -2147483648.

-2147483648 is divisible by 4 and has the factors 4 and -536870912.

-2147483648 appears to be composite.


All done.


I have heard that we ought to be using unsigned long. I've seen some examples of this, but can't input it in a syntactically correct manner.

From my basic understanding, the length of the integer N=5*2^29-1 is more than 10 digits and therefore cannot be represented by our program.

How do I make the program able to deal with larger (or very large) numbers?

Also, is there a way to exceed 2^64 (which I understand to be the limit of unsigned long in C++) without being on a 64-bit system?

Thanks in advance for any help/explanation. I recognize this is quite the newbie issue, but I can't figure out proper implementation no matter how many articles I read.
closed account (zb0S216C)
There's two possible scenarios when an overflow can occur (as far as I know of):

1) When an integer is assigned a value that's beyond its maximum value
2) When the program pushes too much information onto the stack which exceeds the 1MB limit.

Without your program, it's hard to determine.

Wazzak
Luckily, I made a Google Code project so we can work on it together across distances (he's 100 miles away). The code's in the source section, here: http://code.google.com/p/gjsieve/source/browse/main_4.cpp
Last edited on
closed account (zb0S216C)
I can't see any stack violations, so it must be a numerical range violation. There's two solutions I can think of at this time:

1) Impose integral constraints on the input values
2) Use the long long type for lower, k and upper. This gives roughly twice the range of signed int.

Wazzak
Last edited on
Tip: You always use unsigned types unless you have a need for negative numbers.
The second option sounds like a lot of things I've read about.

The only issue is that I can't figure out where to put that. Currently what I have is inline float pow(float x, float y) {return pow((long double)x,(long double)y);} and I understand I can't simply replace float with long long.

Does this simply involve changing int in int lower, k, upper; to long long? Because that still gives overflow errors. I suppose I'm missing something.
Topic archived. No new replies allowed.