BigInteger的对数 [英] Logarithm for BigInteger

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

问题描述

我有一个 BigInteger 号码,例如超过2 64
现在我想计算 BigInteger 数字的对数,但方法 BigInteger.log()不存在。如何计算我的大 BigInteger 值的(自然)对数?

I have a BigInteger number, for example beyond 264. Now i want to calculate the logarithm of that BigInteger number, but the method BigInteger.log() does not exist. How do I calculate the (natural) logarithm of my large BigInteger value?

推荐答案

如果你想支持任意大整数,那就不安全了

If you want to support arbitrarily big integers, it's not safe to just do

Math.log(bigInteger.doubleValue());

因为如果参数超过 double 范围(大约2 ^ 1024或10 ^ 308,即超过300个十进制数字)。

because this would fail if the argument exceeds the double range (about 2^1024 or 10^308, i.e. more than 300 decimal digits ).

这是我自己的类提供方法

Here's my own class that provides the methods

double logBigInteger(BigInteger val);
double logBigDecimal(BigDecimal val);
BigDecimal expBig(double exponent);
BigDecimal powBig(double a, double b);

即使BigDecimal / BigInteger太大(或太小)无法表示,它们也可以安全地工作a double 类型。

They work safely even when the BigDecimal/BigInteger are too big (or too small) to be representable as a double type.

/**
 * Provides some mathematical operations on BigDecimal and BigInteger
 */
public class BigMath {

    protected static final double LOG2 = Math.log(2.0);
    protected static final double LOG10 = Math.log(10.0);

    // numbers greater than 10^MAX_DIGITS_10 or e^MAX_DIGITS_EXP are considered unsafe ('too big') for floating point operations
    protected static final int MAX_DIGITS_EXP = 677;
    protected static final int MAX_DIGITS_10 = 294; // ~ MAX_DIGITS_EXP/LN(10)
    protected static final int MAX_DIGITS_2 = 977; // ~ MAX_DIGITS_EXP/LN(2)

    /**
     * Computes the natural logarithm of a BigInteger. 
     * 
     * Works for really big integers (practically unlimited), even when the argument 
     * falls outside the <tt>double</tt> range
     * 
     * Returns Nan if argument is negative, NEGATIVE_INFINITY if zero.
     * 
     * @param val Argument
     * @return Natural logarithm, as in <tt>Math.log()</tt>
     */
    public static double logBigInteger(BigInteger val) {
        if (val.signum() < 1)
            return val.signum() < 0 ? Double.NaN : Double.NEGATIVE_INFINITY;
        int blex = val.bitLength() - MAX_DIGITS_2; // any value in 60..1023 works ok here
        if (blex > 0)
            val = val.shiftRight(blex);
        double res = Math.log(val.doubleValue());
        return blex > 0 ? res + blex * LOG2 : res;
    }

    /**
     * Computes the natural logarithm of a BigDecimal. 
     * 
     * Works for really big (or really small) arguments, even outside the double range.
     * 
     * Returns Nan if argument is negative, NEGATIVE_INFINITY if zero.
    *
     * @param val Argument
     * @return Natural logarithm, as in <tt>Math.log()</tt>
     */
    public static double logBigDecimal(BigDecimal val) {
        if (val.signum() < 1)
            return val.signum() < 0 ? Double.NaN : Double.NEGATIVE_INFINITY;
        int digits = val.precision() - val.scale(); 
        if (digits < MAX_DIGITS_10 && digits > -MAX_DIGITS_10)
            return Math.log(val.doubleValue());
        else
            return logBigInteger(val.unscaledValue()) - val.scale() * LOG10;
    }

    /**
     * Computes the exponential function, returning a BigDecimal (precision ~ 16).
     *  
     * Works for very big and very small exponents, even when the result 
     * falls outside the double range
     *
     * @param exponent Any finite value (infinite or Nan throws IllegalArgumentException)
     * @return The value of e (base of the natural logarithms) raised to the given exponent, as in <tt>Math.exp()</tt>
     */
    public static BigDecimal expBig(double exponent) {
        if (!Double.isFinite(exponent))
            throw new IllegalArgumentException("Infinite not accepted: " + exponent);
        // e^b = e^(b2+c) = e^b2 2^t with e^c = 2^t 
        double bc = MAX_DIGITS_EXP;
        if (exponent < bc && exponent > -bc)
            return new BigDecimal(Math.exp(exponent), MathContext.DECIMAL64);
        boolean neg = false;
        if (exponent < 0) {
            neg = true;
            exponent = -exponent;
        }
        double b2 = bc;
        double c = exponent - bc;
        int t = (int) Math.ceil(c / LOG10);
        c = t * LOG10;
        b2 = exponent - c;
        if (neg) {
            b2 = -b2;
            t = -t;
        }
        return new BigDecimal(Math.exp(b2), MathContext.DECIMAL64).movePointRight(t);
    }

    /**
     * Same as Math.pow(a,b) but returns a BigDecimal (precision ~ 16). 
     * 
     * Works even for outputs that fall outside the <tt>double</tt> range
     * 
     * The only limit is that b * log(a) does not overflow the double range 
     * 
     * @param a Base. Should be non-negative 
     * @param b Exponent. Should be finite (and non-negative if base is zero)
     * @return Returns the value of the first argument raised to the power of the second argument.
     */
    public static BigDecimal powBig(double a, double b) {
        if (!(Double.isFinite(a) && Double.isFinite(b)))
            throw new IllegalArgumentException(Double.isFinite(b) ? "base not finite: a=" + a : "exponent not finite: b=" + b);
        if (b == 0)
            return BigDecimal.ONE;
        if (b == 1)
            return BigDecimal.valueOf(a);
        if (a == 0) {
            if (b >= 0)
                return BigDecimal.ZERO;
            else
                throw new IllegalArgumentException("0**negative = infinite");
        }
        if (a < 0) {
            throw new IllegalArgumentException("negative base a=" + a);
        }
        double x = b * Math.log(a);
        if (Math.abs(x) < MAX_DIGITS_EXP)
            return BigDecimal.valueOf(Math.pow(a, b));
        else
            return expBig(x);
    }

}

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

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