I'm trying to write a Windows C program (Visual C++ 2019) to generate the continued fraction expansion of π.
The correct values from WolframAlhpa and OEIS shows:
[3; 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14, 2, 1, 1, 2, 2, 2, 2, 1,
However, my values start deviating after 14
[3; 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14, 3, 3, 2, 1, 3, 2, 1, 19,
Here's a demo of the code running.
#include <stdio.h>
#include <stdint.h>
#include <math.h>
int main()
{
long double u = 3.1415926535897932384626433832795028841971693993751058209749445923;
printf("[%lld; ",(unsigned long long)u);
for (int i = 0; i < 20; i++)
{
u = 1.0 / (u - floorl(u));
printf("%lld, ",(unsigned long long)u);
}
return 0;
}
Question
Is the code losing some kind of decimal precision, causing the incorrect values?