Continued Fraction Calculator

Hi, so I want to write a program that represents anything as a continued fraction (please see http://en.wikipedia.org/wiki/Continued_fraction).
Here's my code (something's not quite working). I think that the problem is that I have to stop immediately after int(alpha)=alpha, but I don't really see how.

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
// Continued Fraction Representation.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;

void con_frac(float alpha)
{
	while(int(alpha)!=alpha)
	{
		int a=int(alpha);
		float b=1.0/(alpha-a);
		/*alpha=a+1/b*/ 
		cout << a << ",";
		alpha=b;
	}
}

int main()
{
	con_frac(0.5);
	getchar();
}


Thanks a lot for the help
So what's the problem? Is it not actually stopping?

If so, I'd think it's probably because it is difficult for a float to exactly be equal to some other floating point value due to rounding errors.
The problem is that it's not returning accurate values. I think that I need to make that loop run until the moment that int(alpha)=alpha, and put down that value as the last one, but I don't know how to do that.
Topic archived. No new replies allowed.