我對連續分數的理解是,它總會給出分數形式的小數表示。我認爲連續分數總是會返回小於或等於十進制數的值。不幸的是,我的代碼偶爾會返回大於十進制輸入的分數值。連續分數
我對連續分數的理解是否正確?如果可以,請解釋我的代碼中的錯誤在哪裏。
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);
}
}
這甚至意味着如何編譯? 'reci'被引用爲局部變量,而不是方法。 – Makoto
應該是「繼續分數」(http://en.wikipedia.org/wiki/Continued_fraction) – colcarroll