954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How high can c++ count?

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?

valestrom
Junior Poster in Training
54 posts since Mar 2011
Reputation Points: 27
Solved Threads: 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.

Clinton Portis
Practically a Posting Shark
833 posts since Oct 2005
Reputation Points: 237
Solved Threads: 118
 
it can go as high as you need it to go.


Then why does it hit negative?

valestrom
Junior Poster in Training
54 posts since Mar 2011
Reputation Points: 27
Solved Threads: 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...

Zssffssz
Junior Poster
180 posts since Sep 2011
Reputation Points: 6
Solved Threads: 2
 

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

Chilton
Junior Poster
106 posts since Oct 2009
Reputation Points: 34
Solved Threads: 16
 
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.

valestrom
Junior Poster in Training
54 posts since Mar 2011
Reputation Points: 27
Solved Threads: 0
 

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

Chilton
Junior Poster
106 posts since Oct 2009
Reputation Points: 34
Solved Threads: 16
 

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.

Zssffssz
Junior Poster
180 posts since Sep 2011
Reputation Points: 6
Solved Threads: 2
 

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 <a href="http://en.wikipedia.org/wiki/Stdint.h"><stdint.h></a> 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 .

mike_2000_17
Posting Virtuoso
Moderator
2,139 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457
 

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

Clinton Portis
Practically a Posting Shark
833 posts since Oct 2005
Reputation Points: 237
Solved Threads: 118
 

Here is a general progression for using large numbers:

1. use a standard variable

What is astandard 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.

L7Sqr
Practically a Master Poster
657 posts since Feb 2011
Reputation Points: 201
Solved Threads: 124
 

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

Clinton Portis
Practically a Posting Shark
833 posts since Oct 2005
Reputation Points: 237
Solved Threads: 118
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: