为什么Apache-Commons-Math库中的BigFraction类返回错误的除法结果?

时间:2010-05-14 06:45:17

标签: java biginteger apache-commons-math

本着使用现有的,经过测试和稳定的代码库的精神,我开始使用Apache-Commons-Math library及其BigFraction class来为我正在编写的称为RationalCalc的Android应用程序执行一些合理的计算。 / p>

除了一个唠叨问题外,它对我抛出的每一项任务都很有用。在划分某些BigFraction值时,我得到的结果不正确。

如果我使用除数的倒数创建BigFraction并相乘,我会得到相同的错误答案,但也许这就是图书馆在内部做的事情。

有谁知道我做错了什么?

该分部正常运作,BigFraction为2.5但不是2.51,2.49等......

[UPDATE]

这确实是apache-commons-math 2.0库中的一个错误。该错误已在v.2.1中修复。

现在列在错误跟踪器的“已修复问题”部分中:

When multiplying two BigFraction objects with numerators larger than will fit in an java-primitive int the result of BigFraction.ZERO is incorrectly returned.

感谢@BartK尝试重现问题并让我走上正轨。

[/ UPDATE]

// *** incorrect! ***
BigFraction one = new BigFraction(1.524);
//one: 1715871458028159 / 1125899906842624

BigFraction two = new BigFraction(2.51);
//two: 1413004383087493 / 562949953421312

BigFraction three = one.divide(two);
//three: 0

Log.i("solve", three.toString());
//should be 0.607171315  ??
//returns 0


// *** correct! ****
BigFraction four = new BigFraction(1.524);
//four: 1715871458028159 / 1125899906842624

BigFraction five = new BigFraction(2.5);
//five: 5 / 2

BigFraction six = four.divide(five);
//six: 1715871458028159 / 2814749767106560

Log.i("solve", six.toString());
//should be 0.6096  ??
//returns 0.6096

1 个答案:

答案 0 :(得分:3)

在构造函数中提供double会导致舍入错误。使用精确的分子和分母将产生预期的结果:

public class CommonsMathTest {

    public static void main(String[] args) {

        BigFraction one = new BigFraction(1524, 1000);
        System.out.println("one   = " + one);

        BigFraction two = new BigFraction(251, 100);
        System.out.println("two   = " + two);

        BigFraction three = one.divide(two);
        System.out.println("three = " + three);

        BigFraction four = new BigFraction(1524, 1000);
        System.out.println("four  = " + four);

        BigFraction five = new BigFraction(5, 2);
        System.out.println("five  = " + five);

        BigFraction six = four.divide(five);
        System.out.println("six   = " + six + " = " + six.bigDecimalValue());
    }
}

产生

one   = 381 / 250
two   = 251 / 100
three = 762 / 1255
four  = 381 / 250
five  = 5 / 2
six   = 381 / 625 = 0.6096

修改

顺便说一下,我无法重现你的输出。使用Commons-Math 2.1,以下内容:

BigFraction one = new BigFraction(1.524);
BigFraction two = new BigFraction(2.51);
BigFraction three = one.divide(two);
System.out.println(three.toString() + " = " +three.doubleValue());
如你所说,

不会产生0,但会打印:

1715871458028159 / 2826008766174986 = 0.6071713147410359