#include <iostream>
#include <math.h>
int main()
{
double result = sqrt(4732);
int intResult = (int)result;
result = result - (double)intResult;
double first = result;
while(1)
{
result = 1/result;
intResult = (int)result;
result = result - intResult;
std::cout<<intResult<<std::endl;
double absDiff = result > first ? (result-first):(first-result);
if(absDiff < 0.000001)
break;
}
return 0;
}
I am trying to calculate continued fraction of square root of 4732. Here is the wiki description for the continued fraction. https://en.wikipedia.org/wiki/Continued_fraction
The correct answer is [68;1,3,1,3,45,1,1,2,11,15,5,34,5,15,11,2,1,1,45,3,1,3,1,136]. My code goes in an infinite loop. Here is the output from the first few iterations. [68;1,3,1,3,45,1,1,2,11,15,5,126,.. Note that the output starts diverging from here.
I have checked this code for other numbers like 9999, 13, 12, etc. and it is giving the correct answer. Can anybody point me to the problem in the code? Thanks in advance.
Update: I used float128 as well. But it does not solve the issue. It looks like I may have to use some another algorithm to avoid losing precision.