📄 simplebigdecimal.java
字号:
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space
// Source File Name: SimpleBigDecimal.java
package org.bouncycastle.math.ec;
import java.math.BigInteger;
// Referenced classes of package org.bouncycastle.math.ec:
// ECConstants
class SimpleBigDecimal
{
private static final long serialVersionUID = 1L;
private final BigInteger bigInt;
private final int scale;
public static SimpleBigDecimal getInstance(BigInteger value, int scale)
{
return new SimpleBigDecimal(value.shiftLeft(scale), scale);
}
public SimpleBigDecimal(BigInteger bigInt, int scale)
{
if (scale < 0)
{
throw new IllegalArgumentException("scale may not be negative");
} else
{
this.bigInt = bigInt;
this.scale = scale;
return;
}
}
private SimpleBigDecimal(SimpleBigDecimal limBigDec)
{
bigInt = limBigDec.bigInt;
scale = limBigDec.scale;
}
private void checkScale(SimpleBigDecimal b)
{
if (scale != b.scale)
throw new IllegalArgumentException("Only SimpleBigDecimal of same scale allowed in arithmetic operations");
else
return;
}
public SimpleBigDecimal adjustScale(int newScale)
{
if (newScale < 0)
throw new IllegalArgumentException("scale may not be negative");
if (newScale == scale)
return new SimpleBigDecimal(this);
else
return new SimpleBigDecimal(bigInt.shiftLeft(newScale - scale), newScale);
}
public SimpleBigDecimal add(SimpleBigDecimal b)
{
checkScale(b);
return new SimpleBigDecimal(bigInt.add(b.bigInt), scale);
}
public SimpleBigDecimal add(BigInteger b)
{
return new SimpleBigDecimal(bigInt.add(b.shiftLeft(scale)), scale);
}
public SimpleBigDecimal negate()
{
return new SimpleBigDecimal(bigInt.negate(), scale);
}
public SimpleBigDecimal subtract(SimpleBigDecimal b)
{
return add(b.negate());
}
public SimpleBigDecimal subtract(BigInteger b)
{
return new SimpleBigDecimal(bigInt.subtract(b.shiftLeft(scale)), scale);
}
public SimpleBigDecimal multiply(SimpleBigDecimal b)
{
checkScale(b);
return new SimpleBigDecimal(bigInt.multiply(b.bigInt), scale + scale);
}
public SimpleBigDecimal multiply(BigInteger b)
{
return new SimpleBigDecimal(bigInt.multiply(b), scale);
}
public SimpleBigDecimal divide(SimpleBigDecimal b)
{
checkScale(b);
BigInteger dividend = bigInt.shiftLeft(scale);
return new SimpleBigDecimal(dividend.divide(b.bigInt), scale);
}
public SimpleBigDecimal divide(BigInteger b)
{
return new SimpleBigDecimal(bigInt.divide(b), scale);
}
public SimpleBigDecimal shiftLeft(int n)
{
return new SimpleBigDecimal(bigInt.shiftLeft(n), scale);
}
public int compareTo(SimpleBigDecimal val)
{
checkScale(val);
return bigInt.compareTo(val.bigInt);
}
public int compareTo(BigInteger val)
{
return bigInt.compareTo(val.shiftLeft(scale));
}
public BigInteger floor()
{
return bigInt.shiftRight(scale);
}
public BigInteger round()
{
SimpleBigDecimal oneHalf = new SimpleBigDecimal(ECConstants.ONE, 1);
return add(oneHalf.adjustScale(scale)).floor();
}
public int intValue()
{
return floor().intValue();
}
public long longValue()
{
return floor().longValue();
}
public double doubleValue()
{
return Double.valueOf(toString()).doubleValue();
}
public float floatValue()
{
return Float.valueOf(toString()).floatValue();
}
public int getScale()
{
return scale;
}
public String toString()
{
if (scale == 0)
return bigInt.toString();
BigInteger floorBigInt = floor();
BigInteger fract = bigInt.subtract(floorBigInt.shiftLeft(scale));
if (bigInt.signum() == -1)
fract = ECConstants.ONE.shiftLeft(scale).subtract(fract);
if (floorBigInt.signum() == -1 && !fract.equals(ECConstants.ZERO))
floorBigInt = floorBigInt.add(ECConstants.ONE);
String leftOfPoint = floorBigInt.toString();
char fractCharArr[] = new char[scale];
String fractStr = fract.toString(2);
int fractLen = fractStr.length();
int zeroes = scale - fractLen;
for (int i = 0; i < zeroes; i++)
fractCharArr[i] = '0';
for (int j = 0; j < fractLen; j++)
fractCharArr[zeroes + j] = fractStr.charAt(j);
String rightOfPoint = new String(fractCharArr);
StringBuffer sb = new StringBuffer(leftOfPoint);
sb.append(".");
sb.append(rightOfPoint);
return sb.toString();
}
public boolean equals(Object o)
{
if (this == o)
return true;
if (!(o instanceof SimpleBigDecimal))
{
return false;
} else
{
SimpleBigDecimal other = (SimpleBigDecimal)o;
return bigInt.equals(other.bigInt) && scale == other.scale;
}
}
public int hashCode()
{
return bigInt.hashCode() ^ scale;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -