分數項:2/1, 3/2, 5/3, 8/5, 13/8, ... 分子分母皆為斐波那契數列 F(n+2)/F(n+1)。
int a1=1, a2=2, a3=3;
double t = a3 / a2; // 整數除法 = 1,精度丟失
double t = (double)a3 / (double)a2; // 強制浮點除法
// 或
double t = 1.0 * a3 / a2;
double sum = 0;
long f1 = 1, f2 = 2; // F(2), F(3)
for (int i = 1; i <= n; ++i) {
long f3 = f1 + f2;
sum += (double)f3 / f2;
f1 = f2; f2 = f3;
}
long 64 位元可支援至 n≈90,超過需用 BigInteger 或高精度庫。