first commit
This commit is contained in:
46
java/expression/Log.java
Normal file
46
java/expression/Log.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package expression;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
|
||||
/**
|
||||
* @author Doschennikov Nikita (me@fymio.us)
|
||||
*/
|
||||
public class Log extends AbstractBinaryOperation {
|
||||
public Log(AbstractExpression l, AbstractExpression r) { super(l, r); }
|
||||
|
||||
@Override protected String getOperator() { return "//"; }
|
||||
@Override protected int getPriority() { return 3; }
|
||||
@Override protected boolean isRightAssoc() { return true; }
|
||||
|
||||
@Override
|
||||
protected int applyInt(int a, int b) {
|
||||
if (a <= 0) throw new ArithmeticException("logarithm of non-positive number");
|
||||
if (b <= 1) throw new ArithmeticException("logarithm base must be > 1");
|
||||
int result = 0;
|
||||
int power = 1;
|
||||
while (power <= a / b) {
|
||||
power *= b;
|
||||
result++;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected BigInteger applyBi(BigInteger a, BigInteger b) {
|
||||
if (a.signum() <= 0) throw new ArithmeticException("logarithm of non-positive number");
|
||||
if (b.compareTo(BigInteger.TWO) < 0) throw new ArithmeticException("logarithm base must be > 1");
|
||||
int result = 0;
|
||||
BigInteger power = BigInteger.ONE;
|
||||
while (power.multiply(b).compareTo(a) <= 0) {
|
||||
power = power.multiply(b);
|
||||
result++;
|
||||
}
|
||||
return BigInteger.valueOf(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected BigDecimal applyBd(BigDecimal a, BigDecimal b) {
|
||||
throw new UnsupportedOperationException("log not supported for BigDecimal");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user