11 Replies - 2472 Views - Last Post: 12 December 2009 - 01:53 AM Rate Topic: -----

#1 dnelson1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 18-November 06

C++ Fibonacci program, More accuracy needed

Posted 11 December 2009 - 12:01 AM

Hello i have written a short code to calculate the Fibonacci sequence to a user requested Nth place in the sequence (eg user enters 5 and the following is outputted, 0,1,1,2,3,) the problem is its only really accurate to the top value of an unsigned long int. im not sure how else to put that. here is my code.
//////////////////////////////////////////////////////////////////////////
// This program will be able to calculate Fibonacci numbers up to a	 //
// User entered number of numbers in the sequence EG.				   //
// User enters 5 1,1,2,3,5 would be the output						  //
//////////////////////////////////////////////////////////////////////////
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
   unsigned int var1, var2, var3, var4, var5;
   var5=0;
   cout << "enter the number of places in the fibonacci sequence you wish to calculate:\n ";
   cin >> var1;
			  
   
   cout << "there will be " << var1 << "places in the sequence calculated\n ";
   
   var2=0;
   var3=1;
   
   while (var1>0)
		 {
				 
			   var4=var2+var3;  
			   cout << var2 << "\n";
			   --var1;
			   var2=var3;
			   var3=var4;
			   ++var5;
		   if (var4>=4000000000)
			{
			 cout << var5 << "places were calculated\n";
			 goto end;
			 }	  
		 }

	end:		
	system("PAUSE");
	return EXIT_SUCCESS;
}



Now with this it stops after 68 iterations of the loop. I realize that i put in a break command if the value is over 40000000000 but thats the margin of accuracy ive recieved. i would like it to be able to do thousands of iterations of the loop outputing the result accurately. so that if i asked it for 10,000 numbers in the sequence it will happily do it accurately. Any help would be appreciated. Im also just learning C++, which is why i chose to do a relatively simple code.
EDIT i just ran the code again and realized i should have put the failsafe for a unsigned int only but i put it for an unsigned long. please dont mind that.

This post has been edited by dnelson1: 11 December 2009 - 12:04 AM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ Fibonacci program, More accuracy needed

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: C++ Fibonacci program, More accuracy needed

Posted 11 December 2009 - 12:11 AM

You would need to use one of the "Big Number" libraries to get past the limitations of int.

Here's a couple of examples
GMP
http://gmplib.org/
MAPM
http://www.tc.umn.ed.../mapm-main.html
but there are others to look at.

Could I also suggest that you start on a good path and stop using variable names like "var3".
Meaningful variable names will help you and others understand your code far better.
For example, At a quick glance it looks like "var1" is a counter.
Why not lash out and call it "counter"?
It costs you nothing (beyond a few keystrokes) and will make your code far far better in the long run.
Was This Post Helpful? 0
  • +
  • -

#3 dnelson1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 18-November 06

Re: C++ Fibonacci program, More accuracy needed

Posted 11 December 2009 - 12:14 AM

Ok I think that will help, since im new to this all would you mind explaining how to use the library? and i will cahnge those variable names now thanks.
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: C++ Fibonacci program, More accuracy needed

Posted 11 December 2009 - 01:07 AM

View Postdnelson1, on 10 Dec, 2009 - 11:14 PM, said:

would you mind explaining how to use the library?


Best to read the doco at the links.
Any effort I made would be sure to be worse than what the "owners" of the libraries will provide you.

I'd actually suggest it might be best to mark this as a "follow up later" item in your learning plan and keep moving forward with other ideas and come back to this slightly advanced stuff later when you have a few more of the fundamental under full control.
Was This Post Helpful? 0
  • +
  • -

#5 dnelson1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 18-November 06

Re: C++ Fibonacci program, More accuracy needed

Posted 11 December 2009 - 01:11 AM

I agree with the "follow up later" idea but all the projects i want to do deal with extremely huge numbers and i would like to be able to figure out how to implement them now. i have read a bit on the sites you linked but i dont really follow what they are saying. the one thing i guess i really need to know is after the library is compiled how do i point my Dev C++ compiler to use them?
Was This Post Helpful? 0
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: C++ Fibonacci program, More accuracy needed

Posted 11 December 2009 - 01:20 AM

View Postdnelson1, on 11 Dec, 2009 - 12:11 AM, said:

I agree with the "follow up later" idea but all the projects i want to do deal with extremely huge numbers and i would like to be able to figure out how to implement them now. i have read a bit on the sites you linked but i dont really follow what they are saying. the one thing i guess i really need to know is after the library is compiled how do i point my Dev C++ compiler to use them?


Arrrrgghhh Dev C++ - the horror :evil: :D

Just #include the library in your code.

e.g.
#include <gmp.h>
Was This Post Helpful? 0
  • +
  • -

#7 dnelson1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 18-November 06

Re: C++ Fibonacci program, More accuracy needed

Posted 11 December 2009 - 01:28 AM

is there any special place i have to put the library after its compiled?
Was This Post Helpful? 0
  • +
  • -

#8 dnelson1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 18-November 06

Re: C++ Fibonacci program, More accuracy needed

Posted 11 December 2009 - 01:48 AM

you might think im a total simpleton but how the heck do i compile this stuff. i downloaded the first link you supplied and theres no way i can figure it out on my own if you dont mind.
Was This Post Helpful? 0
  • +
  • -

#9 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: C++ Fibonacci program, More accuracy needed

Posted 11 December 2009 - 01:49 AM

View Postdnelson1, on 11 Dec, 2009 - 12:28 AM, said:

is there any special place i have to put the library after its compiled?


It needs to be where your compiler expects to find such libraries.

You'll need to check the documentation for Dev C++ (try the Google) to confirm where that location is for your compiler.
Was This Post Helpful? 0
  • +
  • -

#10 dnelson1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 18-November 06

Re: C++ Fibonacci program, More accuracy needed

Posted 11 December 2009 - 10:42 PM

Hey since im new to programming i was wondering if you guys could help me out by suggestiong programs to write then i upload code and you guys look over it and advise me where i could do better.
Was This Post Helpful? 0
  • +
  • -

#11 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: C++ Fibonacci program, More accuracy needed

Posted 11 December 2009 - 10:45 PM

Here's a good list of exercises
http://www.eit.ihk-e...k/subjects/cpp/
Was This Post Helpful? 0
  • +
  • -

#12 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: C++ Fibonacci program, More accuracy needed

Posted 12 December 2009 - 01:53 AM

If you need to reach the level of expertise to write programs using arbitrary precision, C++ is the wrong language to pick.

Pick languages like Mathematica, Matlab, or more general programming languages like Python which support arbitrary precision arithmetic. If you pick a language like C++, you will have to acquire significant knowledge (example, information on the compilation process) before you can write even basic programs that depend on bignum support.

However, using us as a crutch for your work, which you can't do because of your lack of knowledge, really isn't acceptable.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1