We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,731 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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?

6
Contributors
11
Replies
3 Hours
Discussion Span
1 Year Ago
Last Updated
12
Views
valestrom
Junior Poster in Training
58 posts since Mar 2011
Reputation Points: 27
Solved Threads: 0
Skill Endorsements: 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: 119
Skill Endorsements: 4

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

Then why does it hit negative?

valestrom
Junior Poster in Training
58 posts since Mar 2011
Reputation Points: 27
Solved Threads: 0
Skill Endorsements: 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
Skill Endorsements: 0

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

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

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

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

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

Zssffssz
Junior Poster
180 posts since Sep 2011
Reputation Points: 6
Solved Threads: 2
Skill Endorsements: 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.

mike_2000_17
21st Century Viking
Moderator
3,135 posts since Jul 2010
Reputation Points: 2,050
Solved Threads: 625
Skill Endorsements: 41

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: 119
Skill Endorsements: 4

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.

L7Sqr
Practically a Posting Shark
849 posts since Feb 2011
Reputation Points: 253
Solved Threads: 155
Skill Endorsements: 7

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: 119
Skill Endorsements: 4

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0870 seconds using 2.71MB