Start New Discussion within our Software Development Community

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?

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.

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

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.

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

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.

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.

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

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.

This article has been dead for over six months. Start a new discussion instead.