从 BigDecimal 中获取减少的分数

Get reduced fraction from BigDecimal

我正在做一些非常精确的小数计算,最后我将这些计算变成约分数。小数点需要精确到 96 位小数。

由于精度非常重要,所以我使用了 BigDecimal 和 BigInteger。

BigDecimal 的计算总是 returns 正确的十进制值,但在某些情况下我将此小数转换为分数的函数失败了

假设我有一个 BigDecimal d

d.toString() = 32.222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222223

当我的函数试图将其转换为分数时,它会输出

Decimal from BigDecimal is:  
32.222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222223

// Run the BigDecimal into getFraction
Denominator before reducing:
1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Numerator before reducing:
32222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222223

// Reduced fraction turns into:
-1/0


// But should output
290/9

这是我将小数化为分数的函数:

static int[] getFraction(BigDecimal x) {
        BigDecimal x1 = x.stripTrailingZeros();
        //System.out.println(x.toString() + " stripped from zeroes");
        //System.out.println(x1.scale());

        // If scale is 0 or under we got a whole number fraction something/1
        if(x1.scale() <= 0) {
            //System.out.println("Whole number");
            int[] rf = { x.intValue(), 1 };
            return rf;
        }

        // If the decimal is 
        if(x.compareTo(BigDecimal.ZERO) < 0) {
            // Add "-" to fraction when printing from main function
            // Flip boolean to indicate negative decimal number
            negative = true;

            // Flip the BigDecimal
            x = x.negate();
            // Perform same function on flipped
            return getFraction(x);
        }

        // Split BigDecimal into the intval and fractional val as strings
        String[] parts = x.toString().split("\.");

        // Get starting numerator and denominator
        BigDecimal denominator = BigDecimal.TEN.pow(parts[1].length()); 
        System.out.println("Denominator :" + denominator.toString());

        BigDecimal numerator = (new BigDecimal(parts[0]).multiply(denominator)).add(new BigDecimal(parts[1]));
        System.out.println("Numerator :" + numerator.toString());

        // Now we reduce
        return reduceFraction(numerator.intValue(), denominator.intValue());
    }

    static int[] reduceFraction(int numerator, int denominator) {
        // First find gcd
        int gcd = BigInteger.valueOf(numerator).gcd(BigInteger.valueOf(denominator)).intValue(); 
        //System.out.println(gcd);


        // Then divide numerator and denominator by gcd
        int[] reduced = { numerator / gcd, denominator / gcd };

        // Return the fraction
        return reduced;
    }

如果有人能指出我的错误,我将不胜感激!

** 更新 **

更改了 reduceFraction 函数: 现在 returns 一个 String[] 而不是 int[]


static String[] reduceFraction(BigDecimal numerator, BigDecimal denominator) {
        // First find gcd
        BigInteger nu = new BigInteger(numerator.toString());
        BigInteger de = new BigInteger(denominator.toString());
        BigInteger gcd = nu.gcd(de);

        // Then divide numerator and denominator by gcd
        nu = nu.divide(gcd);
        de = de.divide(gcd);
        String[] reduced = { nu.toString(), de.toString() };

        // Return the fraction
        return reduced;
    }

获取分数returns:

// Now we reduce, send BigDecimals for numerator and denominator
        return reduceFraction(num, den);

而不是

// Now we reduce
        return reduceFraction(numerator.intValue(), denominator.intValue());

仍然从函数中得到错误答案

输出分数现在是

// Gcd value
gcd = 1

// Fraction is then:
32222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222223/1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000


//gcd Value should be:
gcd = 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

// Whit this gcd the fraction reduces to: 
290/9
// Now we reduce
return reduceFraction(numerator.intValue(), denominator.intValue());

好吧,在这种情况下这一定会失败,因为分子或分母都不能放入此处的 int

调用intValue()后分子变为-1908874353,分母变为0。您必须携带 BigIntegers 直到计算结束。

在将它们转换为 intlong 之前,如果您必须这样做,您可以检查它们是否可以在不损失精度的情况下转换为这些类型,方法是对照 Integer.MIN_VALUEInteger.MAX_VALUELong.MIN_VALUELong.MAX_VALUE

你似乎让这件事变得比需要的要难得多。这是我的初步尝试:

public static BigInteger[] toRational(BigDecimal decimal)
{
    int scale = decimal.scale();
    if (scale <= 0) {
        return new BigInteger[]{decimal.toBigInteger(), BigInteger.ONE};
    } else {
        BigInteger denominator = BigInteger.TEN.pow(scale);
        BigInteger numerator = decimal.unscaledValue();
        BigInteger d = numerator.gcd(denominator);
        return new BigInteger[]{numerator.divide(d), denominator.divide(d)};
    }
}

有理数总是以最低的形式返回。请注意,如果 decimal 为 0,则返回 0/1 作为有理数。如果 decimal 为负数,则返回分子为负数的有理数。