#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

int main() {
	double phi=(sqrt(5)-1)/2.0; // phi
	double phi_n=1; //phi^n, n=0
	double phi_nplus1=phi; // phi^n+1, n=0
	bool diff01=true;
	bool diff50=true;
        int n=0;
	float phifloat=(sqrt(5)-1)/2.0; // phi, float
	float phi_nfloat=1; //phi^n, n=0 , float
	float phi_nplus1float=phifloat; // phi^n+1, n=0 , float
	bool diff01float=true;
	bool diff50float=true;
	ofstream phifile("phi.txt");
	phifile << "n " << " \t phi_n :  \t phi_n_float : \texp(n ln phi) : \t diff(%) :  diff float (%) " << endl;

	while (diff50) { // keep increasing n while diff < 50 percent for double
	   double phi_nplus2=phi_n-phi_nplus1;
	   float phi_nplus2float=phi_nfloat-phi_nplus1float;
	   double phicalc=exp((n+2)*log(phi));
	   double diffpercent  = 100 * (phi_nplus2/phicalc - 1);
	   float  diffpercentfloat = 100 *(phi_nplus2float/phicalc -1);
// print results

	   phifile << n+2 << "\t" << phi_nplus2 << "\t" << phi_nplus2float << "\t" << phicalc << "\t" << diffpercent <<  "\t" <<  diffpercentfloat << endl;
	
           if (diff01) {
// print results if difference larger than 0.1 percent, but only at that n-value
	      diff01 = fabs(diffpercent)<0.1;
	      if (!diff01) {
		  cout << " difference larger than 0.1 percent for double: n="<<n+2<<" difference n-1 :"  << (phi_nplus1/exp((n+1)*log(phi))-1)*100 << "  diff n: " << diffpercent << endl;
	      }
	   }
           if (diff50) {
// print results if difference larger than 50 percent, but only at that n-value
	      diff50 = fabs(diffpercent)<50;
	      if (!diff50) {
		  cout << " difference larger than 50 percent for double: n="<<n+2<<" difference n-1 :"  << (phi_nplus1/exp((n+1)*log(phi))-1)*100 << "  diff n: " << diffpercent << endl;
	      }
	   }
           if (diff01float) {
// print results if difference larger than 0.1 percent, but only at that n-value
	      diff01float = fabs(diffpercentfloat)<0.1;
	      if (!diff01float) {
		  cout << " difference larger than 0.1 percent for float at n= "<<n+2<<" difference n-1 : "  << (phi_nplus1float/exp((n+1)*log(phi))-1)*100 << "% diff n: " << diffpercentfloat << endl;
	      }
	   }
           if (diff50float) {
// print results if difference larger than 50 percent, but only at that n-value
	      diff50float = fabs(diffpercentfloat)<50;
	      if (!diff50float) {
		  cout << " difference larger than 50 percent for float n= "<<n+2<<" difference n-1 : "  << 100*(phi_nplus1float/exp((n+1)*log(phi))-1) << "  diff n: " << diffpercentfloat << endl;
	      }
	   }
           phi_n=phi_nplus1;
           phi_nplus1=phi_nplus2;
           phi_nfloat=phi_nplus1float;
           phi_nplus1float=phi_nplus2float;
	   ++n;
	}
    return 0;
}

