mas*_*her 41 java logarithm bigdecimal
如何计算BigDecimal的对数?有谁知道我可以使用的任何算法?
到目前为止,我的谷歌搜索已经提出了(无用的)只需转换为双精度并使用Math.log的想法.
我将提供所需答案的精确度.
编辑:任何基地都会这样做.如果它在基数x中更容易,我会这样做.
Pet*_*ter 21
Java Number Cruncher:Java程序员数值计算指南使用牛顿方法提供了一个解决方案.书中的源代码可在此处获得.以下内容摘自第12.5章Big Decmial Functions(p330&p331):
/**
* Compute the natural logarithm of x to a given scale, x > 0.
*/
public static BigDecimal ln(BigDecimal x, int scale)
{
// Check that x > 0.
if (x.signum() <= 0) {
throw new IllegalArgumentException("x <= 0");
}
// The number of digits to the left of the decimal point.
int magnitude = x.toString().length() - x.scale() - 1;
if (magnitude < 3) {
return lnNewton(x, scale);
}
// Compute magnitude*ln(x^(1/magnitude)).
else {
// x^(1/magnitude)
BigDecimal root = intRoot(x, magnitude, scale);
// ln(x^(1/magnitude))
BigDecimal lnRoot = lnNewton(root, scale);
// magnitude*ln(x^(1/magnitude))
return BigDecimal.valueOf(magnitude).multiply(lnRoot)
.setScale(scale, BigDecimal.ROUND_HALF_EVEN);
}
}
/**
* Compute the natural logarithm of x to a given scale, x > 0.
* Use Newton's algorithm.
*/
private static BigDecimal lnNewton(BigDecimal x, int scale)
{
int sp1 = scale + 1;
BigDecimal n = x;
BigDecimal term;
// Convergence tolerance = 5*(10^-(scale+1))
BigDecimal tolerance = BigDecimal.valueOf(5)
.movePointLeft(sp1);
// Loop until the approximations converge
// (two successive approximations are within the tolerance).
do {
// e^x
BigDecimal eToX = exp(x, sp1);
// (e^x - n)/e^x
term = eToX.subtract(n)
.divide(eToX, sp1, BigDecimal.ROUND_DOWN);
// x - (e^x - n)/e^x
x = x.subtract(term);
Thread.yield();
} while (term.compareTo(tolerance) > 0);
return x.setScale(scale, BigDecimal.ROUND_HALF_EVEN);
}
/**
* Compute the integral root of x to a given scale, x >= 0.
* Use Newton's algorithm.
* @param x the value of x
* @param index the integral root value
* @param scale the desired scale of the result
* @return the result value
*/
public static BigDecimal intRoot(BigDecimal x, long index,
int scale)
{
// Check that x >= 0.
if (x.signum() < 0) {
throw new IllegalArgumentException("x < 0");
}
int sp1 = scale + 1;
BigDecimal n = x;
BigDecimal i = BigDecimal.valueOf(index);
BigDecimal im1 = BigDecimal.valueOf(index-1);
BigDecimal tolerance = BigDecimal.valueOf(5)
.movePointLeft(sp1);
BigDecimal xPrev;
// The initial approximation is x/index.
x = x.divide(i, scale, BigDecimal.ROUND_HALF_EVEN);
// Loop until the approximations converge
// (two successive approximations are equal after rounding).
do {
// x^(index-1)
BigDecimal xToIm1 = intPower(x, index-1, sp1);
// x^index
BigDecimal xToI =
x.multiply(xToIm1)
.setScale(sp1, BigDecimal.ROUND_HALF_EVEN);
// n + (index-1)*(x^index)
BigDecimal numerator =
n.add(im1.multiply(xToI))
.setScale(sp1, BigDecimal.ROUND_HALF_EVEN);
// (index*(x^(index-1))
BigDecimal denominator =
i.multiply(xToIm1)
.setScale(sp1, BigDecimal.ROUND_HALF_EVEN);
// x = (n + (index-1)*(x^index)) / (index*(x^(index-1)))
xPrev = x;
x = numerator
.divide(denominator, sp1, BigDecimal.ROUND_DOWN);
Thread.yield();
} while (x.subtract(xPrev).abs().compareTo(tolerance) > 0);
return x;
}
/**
* Compute e^x to a given scale.
* Break x into its whole and fraction parts and
* compute (e^(1 + fraction/whole))^whole using Taylor's formula.
* @param x the value of x
* @param scale the desired scale of the result
* @return the result value
*/
public static BigDecimal exp(BigDecimal x, int scale)
{
// e^0 = 1
if (x.signum() == 0) {
return BigDecimal.valueOf(1);
}
// If x is negative, return 1/(e^-x).
else if (x.signum() == -1) {
return BigDecimal.valueOf(1)
.divide(exp(x.negate(), scale), scale,
BigDecimal.ROUND_HALF_EVEN);
}
// Compute the whole part of x.
BigDecimal xWhole = x.setScale(0, BigDecimal.ROUND_DOWN);
// If there isn't a whole part, compute and return e^x.
if (xWhole.signum() == 0) return expTaylor(x, scale);
// Compute the fraction part of x.
BigDecimal xFraction = x.subtract(xWhole);
// z = 1 + fraction/whole
BigDecimal z = BigDecimal.valueOf(1)
.add(xFraction.divide(
xWhole, scale,
BigDecimal.ROUND_HALF_EVEN));
// t = e^z
BigDecimal t = expTaylor(z, scale);
BigDecimal maxLong = BigDecimal.valueOf(Long.MAX_VALUE);
BigDecimal result = BigDecimal.valueOf(1);
// Compute and return t^whole using intPower().
// If whole > Long.MAX_VALUE, then first compute products
// of e^Long.MAX_VALUE.
while (xWhole.compareTo(maxLong) >= 0) {
result = result.multiply(
intPower(t, Long.MAX_VALUE, scale))
.setScale(scale, BigDecimal.ROUND_HALF_EVEN);
xWhole = xWhole.subtract(maxLong);
Thread.yield();
}
return result.multiply(intPower(t, xWhole.longValue(), scale))
.setScale(scale, BigDecimal.ROUND_HALF_EVEN);
}
Run Code Online (Sandbox Code Playgroud)
一个适用于大数字的hacky小算法使用这种关系log(AB) = log(A) + log(B).以下是如何在基数10(您可以轻松转换为任何其他对数基数)中执行此操作:
计算答案中的小数位数.这是对数的组成部分,加上一个.示例:floor(log10(123456)) + 1是6,因为123456有6位数.
如果您只需要对数的整数部分,则可以在此处停止:只需从步骤1的结果中减去1即可.
要获得对数的小数部分,除以数字10^(number of digits),然后计算使用的对数math.log10()(或者其他;如果没有别的可用则使用简单的序列近似),并将其添加到整数部分.示例:获取小数部分log10(123456),计算math.log10(0.123456) = -0.908...并将其添加到步骤1的结果中:6 + -0.908 = 5.092即log10(123456).请注意,您基本上只是将小数点添加到大数字的前面; 在你的用例中可能有一种很好的方法来优化它,对于非常大的数字你甚至不需要费心去抓取所有的数字 - 这log10(0.123)是一个很好的近似值log10(0.123456789).
你可以使用分解它
log(a * 10^b) = log(a) + b * log(10)
Run Code Online (Sandbox Code Playgroud)
基本上b+1是数字中的位数,并且a是 0 到 1 之间的值,您可以使用常规double算术计算其对数。
或者您可以使用一些数学技巧 - 例如,可以通过级数展开来计算接近 1 的数字的对数
ln(x + 1) = x - x^2/2 + x^3/3 - x^4/4 + ...
Run Code Online (Sandbox Code Playgroud)
根据您想要取对数的数字,您可能可以使用类似的东西。
编辑:要获得以 10 为底的对数,您可以将自然对数除以ln(10),或类似地除以任何其他底数。
这个超级快,因为:
toString()BigInteger数学(牛顿/续分数)BigInteger一个呼叫大约需要20微秒(每秒大约50k个呼叫)
但:
BigInteger解决方法BigDecimal(未测试速度):
toBigInteger()(div内部使用一个)该算法利用了以下事实:可以将对数计算为指数与尾数的对数之和.例如:
12345有5位数,所以基数10日志在4到5之间.log(12345)= 4 + log(1.2345)= 4.09149 ...(基数10日志)
此函数计算基数2日志,因为查找占用位数是微不足道的.
public double log(BigInteger val)
{
// Get the minimum number of bits necessary to hold this value.
int n = val.bitLength();
// Calculate the double-precision fraction of this number; as if the
// binary point was left of the most significant '1' bit.
// (Get the most significant 53 bits and divide by 2^53)
long mask = 1L << 52; // mantissa is 53 bits (including hidden bit)
long mantissa = 0;
int j = 0;
for (int i = 1; i < 54; i++)
{
j = n - i;
if (j < 0) break;
if (val.testBit(j)) mantissa |= mask;
mask >>>= 1;
}
// Round up if next bit is 1.
if (j > 0 && val.testBit(j - 1)) mantissa++;
double f = mantissa / (double)(1L << 52);
// Add the logarithm to the number of bits, and subtract 1 because the
// number of bits is always higher than necessary for a number
// (ie. log2(val)<n for every val).
return (n - 1 + Math.log(f) * 1.44269504088896340735992468100189213742664595415298D);
// Magic number converts from base e to base 2 before adding. For other
// bases, correct the result, NOT this number!
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27944 次 |
| 最近记录: |