我对连续分数的理解是它总是以分数形式表示小数。我认为连续分数总是返回小于或等于小数的值。不幸的是,我的代码有时会返回大于十进制输入的小数值。
我对连续分数的理解正确吗?如果是这样,请解释一下我的代码中的错误所在。
public static Rational contFrac(double a, int i,int n){
if(i<n){
boolean neg = false;
if(a<0){
neg = true;//need a helper method to take care of this
}
double reci = Math.abs(1/a);//the reciprocal of a given decimal value
double remain = reci%1;//the decimal portion of the reciprocal
double intprt = reci - remain;//the 'integer' portion of the reciprocal
Rational inter = new Rational((long)intprt);//creates a new rational number using the 'integer' portion of the reciprocal
if(remain !=0){
inter = inter.add(contFrac(remain,i+1,n));
}
return (reciprocal(inter));//gets the reciprocal of a rational number
}
else{
return new Rational(0);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)