Print first n Fibonacci Numbers

Pages: 12
What is the point of the test
fib < numeric_limits<int>::max()

Apart from the very tiny possibility that there is equality (==) rather than less than (<) surely this is effectively always true? As soon as there is any danger of fib exceeding this limit it will "wrap round" to a smaller value (negative if using int; just smaller if using unsigned int).

(And yes, I can see that the original question asked for int rather than any other integer type, but it is much more educational to investigate the other types as well - extending the accessible range of Fibonnacci numbers if nothing else.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <limits>
#include <iomanip>
using namespace std;


int main()
{
   int MAXINT = numeric_limits<int>::max();
   int fib;

   cout << "Maximum integer representable as an int is " << MAXINT << endl;
   
   cout << "\nHere are the numbers either side and whether they are <= MAXINT: " << endl;

   int near = MAXINT - 5;
   for ( int i = 0; i <= 10; i++ )
   {
      fib = near + i;
      cout << fib << "   " << boolalpha << ( fib <= MAXINT ) << endl;
   }
}


Maximum integer representable as an int is 2147483647

Here are the numbers either side and whether they are <= MAXINT: 
2147483642   true
2147483643   true
2147483644   true
2147483645   true
2147483646   true
2147483647   true
-2147483648   true
-2147483647   true
-2147483646   true
-2147483645   true
-2147483644   true



EDIT: Now that's intriguing. Using integer overflow seem to have done a spectacularly effective job of crashing both cpp-shell and my web browser, even though g++ in MinGW didn't complain in the slightest.
Last edited on
The test
fib < numeric_limits<int>::max()
in a while-loop seems good for checking when the highest Fibonacci number representable in a regular int is found, though, no? In a while-loop, that condition would be used to stop the loop once the last number within that limit is found.

And yeah, it does work for regular ints (i.e. signed int):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// chapter5ex11.cpp : Defines the entry point for the console application.
// Osman Zakir
// 1 / 2 / 2017
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 5 Exercise 11
/**
 * Write a program that writes out the first so many values of the Fibonacci
 * series, that is, the series that starts with 1 1 2 3 5 8 13 21 34. The next
 * number of the series is the sum of the two previous ones. Find the largest
 * Fibonacci number that fits in an int.
 */

#include "../../std_lib_facilities.h"

int fibonacci(int number);

int main()
{
	cout << "How many Fibonacci numbers do you want to see?\n";
	int n;
	cin >> n;
	cin.ignore();

	int largest = 0;
	int number = 1;
	int series_number = 0;
	int fib;
	while ((fib = fibonacci(number)) && fib > 0 && fib < numeric_limits<int>::max())
	{
		if (number <= n)
		{
			cout << fibonacci(number) << ' ';
		}
		// find largest Fibonacci number for an int
		largest = fibonacci(number);
		// try to find the number that when passed into fibonacci()
		// would give us the largest Fiboncci number representable in an int
		series_number = number;
		++number;
	}
	cout << "\nThe largest Fibonacci number for an int is " << largest
		<< ", which is " << series_number << "th in the series\n";
	keep_window_open();
}

int fibonacci(int number)
{
	int previous = 1;
	int current = 1;
	int next = 1;
	for (int i = 3; i <= number; ++i)
	{
		next = current + previous;
		previous = current;
		current = next;
	}
	return next;
}


example output (I told it to go until the 46th Fibonacci number, but you can have it go to any number within the range and it'll still be fine):

How many Fibonacci numbers do you want to see?
46
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155 165580141 267914296 433494437 701408733 1134903170 1836311903
The largest Fibonacci number for an int is 1836311903, which is 46th in the series
Please enter a character to exit
l
Press any key to continue . . .
Last edited on
in a while-loop seems good for checking when the highest Fibonacci number representable in a regular int is found, though, no?


But what is the point of checking something that is always true? (Apart from the single value of exact equality?) What is the difference (if any) if you remove this test?
Yeah, you're right, there's no difference. I took the test out and it still produces the correct result. Thanks.
Topic archived. No new replies allowed.
Pages: 12