2

My program calculates the following Fibonacci sequences:

  • 77th 5527939700884757

  • 78th 8944394323791464

  • 79th 14472334024676220

But clearly, if I add last two digits of number 77th and 78th number it should 1, I cannot understand this problem

long double iterative_fib(int n){
   long double firstNumber = 0;
   long double secondNumber = 1;
   long double thirdNumber = 0;

   for (int i = 0;i <n-1;i++)
   {
       if ( n == 0)
       {
           cout << "The fibonacci number is: " << n;
       }
       else
       {
           thirdNumber = firstNumber + secondNumber;
           firstNumber = secondNumber;
           secondNumber = thirdNumber;
       }
   }
   return thirdNumber;
}
StuartLC
  • 104,537
  • 17
  • 209
  • 285
ram singh
  • 41
  • 5
  • 1
    Welcome to StackOverflow! We will not be able to tell what the problem is from looking at your screen results. Please provide a minimum amount of code for us to look at – Haris Nadeem Jun 10 '18 at 06:41
  • 2
    Are you using long long int as your datatype? – Deepak Tatyaji Ahire Jun 10 '18 at 06:42
  • 1
    @HarisNadeem I wrote the code. Thank you. – ram singh Jun 10 '18 at 06:45
  • @DeepakAhire I am using long double. – ram singh Jun 10 '18 at 06:45
  • Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Jun 10 '18 at 06:45
  • 1
    I also recommend you read [this Stack Overflow question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/), that you [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/), and that you read *all* of http://idownvotedbecau.se/ to learn some reasons you get negative votes. – Some programmer dude Jun 10 '18 at 06:46

2 Answers2

4

By using long double as your cumulative data type, you are opening yourself up to the precision limits of the type, which is usually around 17 digits.

Since fibonacci numbers are all positive integers, I would instead use a unsigned long long to represent the integers - this is good for a signed integer representation of 2^64-1, i.e. at least 19 digits of precision.

unsigned long long iterative_fib(int n){
   unsigned long long firstNumber = 0;
   unsigned long long secondNumber = 1;
   unsigned long long thirdNumber = 0;

   for (int i = 0; i < n-1; i++)
   {
       thirdNumber = firstNumber + secondNumber;
       firstNumber = secondNumber;
       secondNumber = thirdNumber;
   }
   return thirdNumber;
}

Which returns the correct answer, 14472334024676221

IdeOne example here

However, beyond 19 digits, you'll need to resort to a Big Integer representation library, or roll your own.

StuartLC
  • 104,537
  • 17
  • 209
  • 285
1

When working with number sequences such as Fibonacci numbers, if one uses an approach where one tries to store a number in a sequence in a specific type, such as long long or unsigned long long, eventually it will become impossible to store large values, so one needs to change the strategy. In that case, for example, strings could be used to store digits of a large number, and any operation you perform would have to be digit-wise. Also, issue similar to the one you have has been addressed in this question here

Akhaim
  • 122
  • 7