连续分数 [英] Continuous Fractions

查看:126
本文介绍了连续分数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对连续分数的理解是,它将始终以分数形式表示小数.我认为连续分数总是返回小于或等于十进制数的值.不幸的是,我的代码有时会返回大于小数输入的小数值.

My understanding of continuous fractions was that it will always give a representation of a decimal in fraction form. I thought that continuous fraction would always return value less than or equal to the decimal number. Unfortunately my code occasionally returns fractional values greater than the decimal input.

我对连续分数的理解正确吗?如果可以的话,请您说明错误在代码中的位置.

Is my understanding of continuous fractions correct? If so can you please explain where in my code the error lies.

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);
    }       
}

推荐答案

我确定计算机正在对您的1/a进行四舍五入.

I'm sure that the computer is rounding your 1/a.

这篇关于连续分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆