1.11M Members

How high can c++ count?

 
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?

 
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.

 
0
 

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

Then why does it hit negative?

 
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...

 
0
 

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

 
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.

 
0
 

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

 
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.

 
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.

 
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

 
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.

 
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 six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: