1,105,578 Community Members

How high can c++ count?

Member Avatar
valestrom
Junior Poster in Training
61 posts since Mar 2011
Reputation Points: 17 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Just wondering, how high can c++ really count? Because I made a fibonacci program, and it starts acting weird and printing negatives at about 10-11 places. With both long, and int variables? Is it impossible to count higher? If not, how?

Member Avatar
Clinton Portis
Practically a Posting Shark
831 posts since Oct 2005
Reputation Points: 211 [?]
Q&As Helped to Solve: 120 [?]
Skill Endorsements: 8 [?]
 
0
 

it can go as high as you need it to go. there is no limitation. even if hardware limitations are exceeded, ye' can still use a net based resource, which is limitless.

Member Avatar
valestrom
Junior Poster in Training
61 posts since Mar 2011
Reputation Points: 17 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

it can go as high as you need it to go.

Then why does it hit negative?

Member Avatar
Zssffssz
Junior Poster
180 posts since Sep 2011
Reputation Points: -4 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

I guess an array of longs might help (like a c style string) it would increase the total size Iitcould store. Wired I just asked my math teacher what the fibinatchi series was today...

Member Avatar
Chilton
Junior Poster
107 posts since Oct 2009
Reputation Points: 22 [?]
Q&As Helped to Solve: 16 [?]
Skill Endorsements: 0 [?]
 
0
 

It's based on the type of the variable. They each have their limits.

Member Avatar
valestrom
Junior Poster in Training
61 posts since Mar 2011
Reputation Points: 17 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

It's based on the type of the variable. They each have their limits.

I tried both var, and long. I'm not sure what variable is higher than that.

Member Avatar
Chilton
Junior Poster
107 posts since Oct 2009
Reputation Points: 22 [?]
Q&As Helped to Solve: 16 [?]
Skill Endorsements: 0 [?]
 
0
 

You can extend your range by using unsigned integers, since your numbers aren't going to be negative.

Member Avatar
Zssffssz
Junior Poster
180 posts since Sep 2011
Reputation Points: -4 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

It is compiler dependent. The C++ standard just says that Long has to be the same size or bigger than int. Same with double and float. I have also never heard of var before this.

Member Avatar
mike_2000_17
21st Century Viking
4,083 posts since Jul 2010
Reputation Points: 2,253 [?]
Q&As Helped to Solve: 800 [?]
Skill Endorsements: 73 [?]
Moderator
Featured
Sponsor
 
0
 

Given the way the Fibonacci sequence works (by adding the last two values) you can pretty safely use the double type, results will not be exact (floating-point operations never are), but the range will go up to really large values (about 1.8 x 10^308).

Also, the int and long types are the same in most implementations. You'll have better luck with the uint64_t type from the <stdint.h> header. But that type will only go up to 2^64 (about 1.845 x 10^19). I think some platforms even provide uint128_t.

For higher integer numbers, you will have to use variable length integer types, which only come from special libraries, such as BigInt.

Member Avatar
Clinton Portis
Practically a Posting Shark
831 posts since Oct 2005
Reputation Points: 211 [?]
Q&As Helped to Solve: 120 [?]
Skill Endorsements: 8 [?]
 
0
 

Here is a general progression for using large numbers:

1. use a standard variable
2. use a long variable
3. use an unsigned long variable
4. split the number is several variables
5. use a c-string buffer
6. use an aggregate data type
7. use a library or dll for handling large numbers
8. upgrade hardware locally to enhance physical and/or logical memory
9. use a database or internet API

Member Avatar
L7Sqr
Veteran Poster
1,006 posts since Feb 2011
Reputation Points: 179 [?]
Q&As Helped to Solve: 168 [?]
Skill Endorsements: 11 [?]
 
0
 

Here is a general progression for using large numbers:

1. use a standard variable

What is a standard variable? I assume that you mean something smaller than a long , but there are several, so you should specify.

5. use a c-string buffer

I'd seriously caution against using a c-string as a means to calculate large numbers. An integer array, perhaps, but certainly not strings.

Although, there are libraries readily available for this type of activity. I'd skip the entire progression here and claim that there are two options:
1 - find the largest native type supported on your system ( int64_t ?)
2 - any need for something larger should be addressed immediately with a big-num library.

Member Avatar
Clinton Portis
Practically a Posting Shark
831 posts since Oct 2005
Reputation Points: 211 [?]
Q&As Helped to Solve: 120 [?]
Skill Endorsements: 8 [?]
 
0
 

short int or int for example, would be a 'standard' or most commonly used among the native c++ variables.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: