Thread: Negetive Numbers

  1. #1
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77

    Negetive Numbers

    I wrote this program, and when it generates 50 or so numbers, it starts making them negative. Any ideas why?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int fibonacci(int n);
    
    int main()
    {
    	cout << "Ryan's Fibonacci Number calculator.\n";
    	int input;
    	while(input != 0)
    	{
    		if(input == 0) break;
    		
    		cout << "Enter how many Fibonacci Numbers to calculate.\nEnter 0 to quit: ";
    		cin >> input;
    		fibonacci(input);
    	}
    }
    
    int fibonacci(int n)
    {
    	int first = 0;
    	int second = 1;
    	while(n--)
    	{	
    		cout << first << "\n";
    		int tmp = first+second;
    		first = second;
    		second = tmp;
    	}
    }

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    You're exceeding the range of an int, so it wraps. To help that, you could set a max number, make it unsigned, and/or increase to an int64

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Eventually even an int64 would run out. You can implement your own infinite-digit number class or you could use something like GMP.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by dwks
    Eventually even an int64 would run out. You can implement your own infinite-digit number class or you could use something like GMP.
    It is not possible to implement an infinite-digit number class, because you are always constrained by the amount of addressable memory available to your program. So any value you can represent is always finite (albeit able to hold pretty large values).

  5. #5
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    Last edited by Decrypt; 03-14-2006 at 09:29 AM. Reason: added link
    There is a difference between tedious and difficult.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Rational Numbers (C++)
    By cloudjc in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2008, 04:03 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM