Break statement failure

My code works except for the break statement, and I can't figure out why. Basically, I need to find the first instance in the fibonacci sequence that the terms evaluate to the golden ratio with five decimal places, which is a fancy way to say

((fibonacci[x]+fibonacci[x-1])/fibonacci[x])
must equal
1.61803

My output works fine, so I know the numbers are being calculated, but the statement does not break... I have no idea what's wrong. Anyway, here's the code.


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
#include <stdlib.h>
#include <iostream.h>
#include <iomanip.h>
#include <time.h>
#include <vector>
#include <algorithm>

using std::vector;

std::vector<double> fibonacci;

double golden_ratio=0;
double golden_ratio_actual=1.61803;
int x=2;

int main()
{
	cout << setprecision(6);

	fibonacci.push_back(1); //fib[0]
	fibonacci.push_back(1); //fib[1]
	
	cout << fibonacci[0] << endl;
	cout << fibonacci[1] << endl;


	for(int x=2; x<30; x++)
	{
		//fibonacci.push_back( (fibonacci[1]+fibonacci[x-1]) ) This fibonacci formula is INCORRECT though given on the paper
		fibonacci.push_back( (fibonacci[x-1]+fibonacci[x-2]) );
		
		golden_ratio=((fibonacci[x]+fibonacci[x-1])/fibonacci[x]);

		cout << fibonacci[x] << '\t' << golden_ratio << endl;

		if( golden_ratio==golden_ratio_actual ) //golden ratio, must find to five dec.
		{break;}

	}

	return 0;
}


EDIT: I know about iostream.h, but I'm using VC++ 6.0 which still needs iostream.h...
Last edited on
You appear to be comparing two floating point numbers. This is flawed.

http://floating-point-gui.de/
and in particular
http://floating-point-gui.de/errors/comparison/
1
2
		if( abs(golden_ratio-golden_ratio_actual)<0.00001 ) //golden ratio, must find to five dec.
		{break;}


When I use this, or when I do not use this, regardless, my program exits the loop at the first iteration. I don't understand why.
When I use this, or when I do not use this, regardless, my program exits the loop at the first iteration. I don't understand why.


You might want add a getch(); (library: conio.h) before the return 0;.
Last edited on
You might want add a getch(); (library: conio.h) before the return 0;.


Is this just to pause the program? Right now my main concern is why the if statement evaluates to true as soon as I run the loop
Is this just to pause the program? Right now my main concern is why the if statement evaluates to true as soon as I run the loop


I doesn't, for me. I get 13 lines of output, the last of which is:

233       1.61803
Last edited on
That's... weird...EDIT: some playing around revealed that my line:
abs(golden_ratio-golden_ratio_actual)

Always evaluates to zero. Why is that?
Last edited on
I figured it out. I was using the abs() function which only works with ints (because I have to include the C version of the library, because my compiler is silly). I had to use fabs() to get the float version. Thanks for all your help!
Topic archived. No new replies allowed.