| | | 1 | | using System; |
| | | 2 | | using System.Text; |
| | | 3 | | |
| | | 4 | | namespace Renci.SshNet.Security.Org.BouncyCastle.Math.EC.Abc |
| | | 5 | | { |
| | | 6 | | /** |
| | | 7 | | * Class representing a simple version of a big decimal. A |
| | | 8 | | * <code>SimpleBigDecimal</code> is basically a |
| | | 9 | | * {@link java.math.BigInteger BigInteger} with a few digits on the right of |
| | | 10 | | * the decimal point. The number of (binary) digits on the right of the decimal |
| | | 11 | | * point is called the <code>scale</code> of the <code>SimpleBigDecimal</code>. |
| | | 12 | | * Unlike in {@link java.math.BigDecimal BigDecimal}, the scale is not adjusted |
| | | 13 | | * automatically, but must be set manually. All <code>SimpleBigDecimal</code>s |
| | | 14 | | * taking part in the same arithmetic operation must have equal scale. The |
| | | 15 | | * result of a multiplication of two <code>SimpleBigDecimal</code>s returns a |
| | | 16 | | * <code>SimpleBigDecimal</code> with double scale. |
| | | 17 | | */ |
| | | 18 | | internal class SimpleBigDecimal |
| | | 19 | | // : Number |
| | | 20 | | { |
| | | 21 | | // private static final long serialVersionUID = 1L; |
| | | 22 | | |
| | | 23 | | private readonly BigInteger bigInt; |
| | | 24 | | private readonly int scale; |
| | | 25 | | |
| | | 26 | | /** |
| | | 27 | | * Returns a <code>SimpleBigDecimal</code> representing the same numerical |
| | | 28 | | * value as <code>value</code>. |
| | | 29 | | * @param value The value of the <code>SimpleBigDecimal</code> to be |
| | | 30 | | * created. |
| | | 31 | | * @param scale The scale of the <code>SimpleBigDecimal</code> to be |
| | | 32 | | * created. |
| | | 33 | | * @return The such created <code>SimpleBigDecimal</code>. |
| | | 34 | | */ |
| | | 35 | | public static SimpleBigDecimal GetInstance(BigInteger val, int scale) |
| | 0 | 36 | | { |
| | 0 | 37 | | return new SimpleBigDecimal(val.ShiftLeft(scale), scale); |
| | 0 | 38 | | } |
| | | 39 | | |
| | | 40 | | /** |
| | | 41 | | * Constructor for <code>SimpleBigDecimal</code>. The value of the |
| | | 42 | | * constructed <code>SimpleBigDecimal</code> Equals <code>bigInt / |
| | | 43 | | * 2<sup>scale</sup></code>. |
| | | 44 | | * @param bigInt The <code>bigInt</code> value parameter. |
| | | 45 | | * @param scale The scale of the constructed <code>SimpleBigDecimal</code>. |
| | | 46 | | */ |
| | 0 | 47 | | public SimpleBigDecimal(BigInteger bigInt, int scale) |
| | 0 | 48 | | { |
| | 0 | 49 | | if (scale < 0) |
| | 0 | 50 | | throw new ArgumentException("scale may not be negative"); |
| | | 51 | | |
| | 0 | 52 | | this.bigInt = bigInt; |
| | 0 | 53 | | this.scale = scale; |
| | 0 | 54 | | } |
| | | 55 | | |
| | 0 | 56 | | private SimpleBigDecimal(SimpleBigDecimal limBigDec) |
| | 0 | 57 | | { |
| | 0 | 58 | | bigInt = limBigDec.bigInt; |
| | 0 | 59 | | scale = limBigDec.scale; |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | private void CheckScale(SimpleBigDecimal b) |
| | 0 | 63 | | { |
| | 0 | 64 | | if (scale != b.scale) |
| | 0 | 65 | | throw new ArgumentException("Only SimpleBigDecimal of same scale allowed in arithmetic operations"); |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | public SimpleBigDecimal AdjustScale(int newScale) |
| | 0 | 69 | | { |
| | 0 | 70 | | if (newScale < 0) |
| | 0 | 71 | | throw new ArgumentException("scale may not be negative"); |
| | | 72 | | |
| | 0 | 73 | | if (newScale == scale) |
| | 0 | 74 | | return this; |
| | | 75 | | |
| | 0 | 76 | | return new SimpleBigDecimal(bigInt.ShiftLeft(newScale - scale), newScale); |
| | 0 | 77 | | } |
| | | 78 | | |
| | | 79 | | public SimpleBigDecimal Add(SimpleBigDecimal b) |
| | 0 | 80 | | { |
| | 0 | 81 | | CheckScale(b); |
| | 0 | 82 | | return new SimpleBigDecimal(bigInt.Add(b.bigInt), scale); |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | public SimpleBigDecimal Add(BigInteger b) |
| | 0 | 86 | | { |
| | 0 | 87 | | return new SimpleBigDecimal(bigInt.Add(b.ShiftLeft(scale)), scale); |
| | 0 | 88 | | } |
| | | 89 | | |
| | | 90 | | public SimpleBigDecimal Negate() |
| | 0 | 91 | | { |
| | 0 | 92 | | return new SimpleBigDecimal(bigInt.Negate(), scale); |
| | 0 | 93 | | } |
| | | 94 | | |
| | | 95 | | public SimpleBigDecimal Subtract(SimpleBigDecimal b) |
| | 0 | 96 | | { |
| | 0 | 97 | | return Add(b.Negate()); |
| | 0 | 98 | | } |
| | | 99 | | |
| | | 100 | | public SimpleBigDecimal Subtract(BigInteger b) |
| | 0 | 101 | | { |
| | 0 | 102 | | return new SimpleBigDecimal(bigInt.Subtract(b.ShiftLeft(scale)), scale); |
| | 0 | 103 | | } |
| | | 104 | | |
| | | 105 | | public SimpleBigDecimal Multiply(SimpleBigDecimal b) |
| | 0 | 106 | | { |
| | 0 | 107 | | CheckScale(b); |
| | 0 | 108 | | return new SimpleBigDecimal(bigInt.Multiply(b.bigInt), scale + scale); |
| | 0 | 109 | | } |
| | | 110 | | |
| | | 111 | | public SimpleBigDecimal Multiply(BigInteger b) |
| | 0 | 112 | | { |
| | 0 | 113 | | return new SimpleBigDecimal(bigInt.Multiply(b), scale); |
| | 0 | 114 | | } |
| | | 115 | | |
| | | 116 | | public SimpleBigDecimal Divide(SimpleBigDecimal b) |
| | 0 | 117 | | { |
| | 0 | 118 | | CheckScale(b); |
| | 0 | 119 | | BigInteger dividend = bigInt.ShiftLeft(scale); |
| | 0 | 120 | | return new SimpleBigDecimal(dividend.Divide(b.bigInt), scale); |
| | 0 | 121 | | } |
| | | 122 | | |
| | | 123 | | public SimpleBigDecimal Divide(BigInteger b) |
| | 0 | 124 | | { |
| | 0 | 125 | | return new SimpleBigDecimal(bigInt.Divide(b), scale); |
| | 0 | 126 | | } |
| | | 127 | | |
| | | 128 | | public SimpleBigDecimal ShiftLeft(int n) |
| | 0 | 129 | | { |
| | 0 | 130 | | return new SimpleBigDecimal(bigInt.ShiftLeft(n), scale); |
| | 0 | 131 | | } |
| | | 132 | | |
| | | 133 | | public int CompareTo(SimpleBigDecimal val) |
| | 0 | 134 | | { |
| | 0 | 135 | | CheckScale(val); |
| | 0 | 136 | | return bigInt.CompareTo(val.bigInt); |
| | 0 | 137 | | } |
| | | 138 | | |
| | | 139 | | public int CompareTo(BigInteger val) |
| | 0 | 140 | | { |
| | 0 | 141 | | return bigInt.CompareTo(val.ShiftLeft(scale)); |
| | 0 | 142 | | } |
| | | 143 | | |
| | | 144 | | public BigInteger Floor() |
| | 0 | 145 | | { |
| | 0 | 146 | | return bigInt.ShiftRight(scale); |
| | 0 | 147 | | } |
| | | 148 | | |
| | | 149 | | public BigInteger Round() |
| | 0 | 150 | | { |
| | 0 | 151 | | SimpleBigDecimal oneHalf = new SimpleBigDecimal(BigInteger.One, 1); |
| | 0 | 152 | | return Add(oneHalf.AdjustScale(scale)).Floor(); |
| | 0 | 153 | | } |
| | | 154 | | |
| | | 155 | | public int IntValue |
| | | 156 | | { |
| | 0 | 157 | | get { return Floor().IntValue; } |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | public long LongValue |
| | | 161 | | { |
| | 0 | 162 | | get { return Floor().LongValue; } |
| | | 163 | | } |
| | | 164 | | |
| | | 165 | | // public double doubleValue() |
| | | 166 | | // { |
| | | 167 | | // return new Double(ToString()).doubleValue(); |
| | | 168 | | // } |
| | | 169 | | // |
| | | 170 | | // public float floatValue() |
| | | 171 | | // { |
| | | 172 | | // return new Float(ToString()).floatValue(); |
| | | 173 | | // } |
| | | 174 | | |
| | | 175 | | public int Scale |
| | | 176 | | { |
| | 0 | 177 | | get { return scale; } |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | public override string ToString() |
| | 0 | 181 | | { |
| | 0 | 182 | | if (scale == 0) |
| | 0 | 183 | | return bigInt.ToString(); |
| | | 184 | | |
| | 0 | 185 | | BigInteger floorBigInt = Floor(); |
| | | 186 | | |
| | 0 | 187 | | BigInteger fract = bigInt.Subtract(floorBigInt.ShiftLeft(scale)); |
| | 0 | 188 | | if (bigInt.SignValue < 0) |
| | 0 | 189 | | { |
| | 0 | 190 | | fract = BigInteger.One.ShiftLeft(scale).Subtract(fract); |
| | 0 | 191 | | } |
| | | 192 | | |
| | 0 | 193 | | if ((floorBigInt.SignValue == -1) && (!(fract.Equals(BigInteger.Zero)))) |
| | 0 | 194 | | { |
| | 0 | 195 | | floorBigInt = floorBigInt.Add(BigInteger.One); |
| | 0 | 196 | | } |
| | 0 | 197 | | string leftOfPoint = floorBigInt.ToString(); |
| | | 198 | | |
| | 0 | 199 | | char[] fractCharArr = new char[scale]; |
| | 0 | 200 | | string fractStr = fract.ToString(2); |
| | 0 | 201 | | int fractLen = fractStr.Length; |
| | 0 | 202 | | int zeroes = scale - fractLen; |
| | 0 | 203 | | for (int i = 0; i < zeroes; i++) |
| | 0 | 204 | | { |
| | 0 | 205 | | fractCharArr[i] = '0'; |
| | 0 | 206 | | } |
| | 0 | 207 | | for (int j = 0; j < fractLen; j++) |
| | 0 | 208 | | { |
| | 0 | 209 | | fractCharArr[zeroes + j] = fractStr[j]; |
| | 0 | 210 | | } |
| | 0 | 211 | | string rightOfPoint = new string(fractCharArr); |
| | | 212 | | |
| | 0 | 213 | | StringBuilder sb = new StringBuilder(leftOfPoint); |
| | 0 | 214 | | sb.Append("."); |
| | 0 | 215 | | sb.Append(rightOfPoint); |
| | | 216 | | |
| | 0 | 217 | | return sb.ToString(); |
| | 0 | 218 | | } |
| | | 219 | | |
| | | 220 | | public override bool Equals( |
| | | 221 | | object obj) |
| | 0 | 222 | | { |
| | 0 | 223 | | if (this == obj) |
| | 0 | 224 | | return true; |
| | | 225 | | |
| | 0 | 226 | | SimpleBigDecimal other = obj as SimpleBigDecimal; |
| | | 227 | | |
| | 0 | 228 | | if (other == null) |
| | 0 | 229 | | return false; |
| | | 230 | | |
| | 0 | 231 | | return bigInt.Equals(other.bigInt) |
| | 0 | 232 | | && scale == other.scale; |
| | 0 | 233 | | } |
| | | 234 | | |
| | | 235 | | public override int GetHashCode() |
| | 0 | 236 | | { |
| | 0 | 237 | | return bigInt.GetHashCode() ^ scale; |
| | 0 | 238 | | } |
| | | 239 | | |
| | | 240 | | } |
| | | 241 | | } |