霍纳的递推算法小数部分 - Java的 [英] Horner's recursive algorithm for fractional part - Java

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

问题描述

我想创建一个使用霍纳的算法来转换一个分数的基础n至10为基数,我在这里和全国各地搜查,但未能找到详细处理小数部分的任何地方递归方法。作为负责人,我是pretty的薄弱递归因为我还没有正式学过在我的编程类呢,但都被赋予它另一个类。

我是能够使一个方法处理数量的整数部分,只是没有小数部分。

我觉得我写的方法是相当密切的,因为它让我加倍对我的测试数字的答案(也许是因为我测试基地2)。

传递的第一个参数是一个int数组填充系数。我不是太在意,因为我正在做的所有系数相同,以测试它的系数的顺序。

第二个参数是基础。第三参数被初始化为我也用于整数部分方法的系数减去1的个数。我试图使用的系数的数量,但是,步骤从数组中。

我试图通过基地把一个更多的时间,因为这将让我正确的答案,但如果我的基本情况return语句,或在最后return语句的结尾这样做这是行不通的。

所以,当我尝试转换的 0.1111 基地2个基地10,我的方法返回的 1.875 (双正确答案的 0.9375 ) 。

任何提示将AP preciated!

  // TL; DR
系数[0] = 1;系数[1] = 1;系数[2] = 1;系数[3] = 1;
基地= 2;它= 3;
//结果在1.875,而不是正确0.9375



公共静态双fracHorner(INT []系数,INT基地,诠释吧){
    如果(它== 0){
        返回系数[它]
    }
    返程((浮点)1 /底座* fracHorner(系数,基地,它-1))+系数[它]
}
 

解决方案

观察到 fracHorner 始终返回值至少等于系数[它] ,因为它要么返回系数[它] 或一些积极加入到系数[它] 。由于系数[它]>在您的测试= 1 ,它总是会返回一个大于或等于一

这是比较容易解决:分两种系数[它] 基础

 公共静态双fracHorner(INT []系数,INT基地,诠释吧){
    如果(它== 0){
        返回((双)系数[它])/基地;
    }
    返回(fracHorner(系数,碱,它-1)+系数[它])/基;
}
 

I am trying to create a recursive method that uses Horner's algorithm to convert a fractional number in base n to base 10. I've searched here and all over but couldn't find anywhere that dealt with the fractional part in detail. As a heads up, I'm pretty weak in recursion as I have not formally learned it in my programming classes yet, but have been assigned it by another class.

I was able to make a method that handles the integer part of the number, just not the fractional part.

I feel like the method I've written is fairly close as it gets me to double the answer for my test figures (maybe because I'm testing base 2).

The first param passed is an int array filled with the coefficients. I'm not too concerned with the order of the coefficients as I'm making all the coefficients the same to test it out.

The second param is the base. The third param is initialized to the number of coefficients minus 1 which I also used for the integer part method. I tried using the number of coefficients, but that steps out of the array.

I tried dividing by the base one more time as that would give me the right answer, but it doesn't work if I do so in the base case return statement or at the end of the final return statement.

So, when I try to convert 0.1111 base 2 to base 10, my method returns 1.875 (double the correct answer of 0.9375).

Any hints would be appreciated!

//TL;DR
coef[0] = 1; coef[1] = 1; coef[2] = 1; coef[3] = 1;
base = 2; it = 3;
//results in 1.875 instead of the correct 0.9375



public static double fracHorner(int[] coef, int base, int it) {
    if (it == 0) {
        return coef[it];
    }
    return ((float)1/base * fracHorner(coef, base, it-1)) + coef[it];
}

解决方案

Observe that fracHorner always returns a value at least equal to coef[it] because it either returns coef[it] or something positive added to coef[it]. Since coef[it] >= 1 in your tests, it will always return a number greater than or equal to one.

It's relatively easy to fix: divide both coef[it] by base:

public static double fracHorner(int[] coef, int base, int it) {
    if (it == 0) {
        return ((double)coef[it])/base;
    }
    return (fracHorner(coef, base, it-1) + coef[it])/base;
}

这篇关于霍纳的递推算法小数部分 - Java的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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