< Summary

Information
Class: Renci.SshNet.Security.Org.BouncyCastle.Math.EC.Abc.SimpleBigDecimal
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\BouncyCastle\math\ec\abc\SimpleBigDecimal.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 121
Coverable lines: 121
Total lines: 241
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 26
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
GetInstance(...)100%10%
.ctor(...)0%20%
.ctor(...)100%10%
CheckScale(...)0%20%
AdjustScale(...)0%40%
Add(...)100%10%
Add(...)100%10%
Negate()100%10%
Subtract(...)100%10%
Subtract(...)100%10%
Multiply(...)100%10%
Multiply(...)100%10%
Divide(...)100%10%
Divide(...)100%10%
ShiftLeft(...)100%10%
CompareTo(...)100%10%
CompareTo(...)100%10%
Floor()100%10%
Round()100%10%
get_IntValue()100%10%
get_LongValue()100%10%
get_Scale()100%10%
ToString()0%120%
Equals(...)0%60%
GetHashCode()100%10%

File(s)

\home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\BouncyCastle\math\ec\abc\SimpleBigDecimal.cs

#LineLine coverage
 1using System;
 2using System.Text;
 3
 4namespace 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)
 036    {
 037      return new SimpleBigDecimal(val.ShiftLeft(scale), scale);
 038    }
 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    */
 047    public SimpleBigDecimal(BigInteger bigInt, int scale)
 048    {
 049      if (scale < 0)
 050        throw new ArgumentException("scale may not be negative");
 51
 052      this.bigInt = bigInt;
 053      this.scale = scale;
 054    }
 55
 056    private SimpleBigDecimal(SimpleBigDecimal limBigDec)
 057    {
 058      bigInt = limBigDec.bigInt;
 059      scale = limBigDec.scale;
 060    }
 61
 62    private void CheckScale(SimpleBigDecimal b)
 063    {
 064      if (scale != b.scale)
 065        throw new ArgumentException("Only SimpleBigDecimal of same scale allowed in arithmetic operations");
 066    }
 67
 68    public SimpleBigDecimal AdjustScale(int newScale)
 069    {
 070      if (newScale < 0)
 071        throw new ArgumentException("scale may not be negative");
 72
 073      if (newScale == scale)
 074        return this;
 75
 076      return new SimpleBigDecimal(bigInt.ShiftLeft(newScale - scale), newScale);
 077    }
 78
 79    public SimpleBigDecimal Add(SimpleBigDecimal b)
 080    {
 081      CheckScale(b);
 082      return new SimpleBigDecimal(bigInt.Add(b.bigInt), scale);
 083    }
 84
 85    public SimpleBigDecimal Add(BigInteger b)
 086    {
 087      return new SimpleBigDecimal(bigInt.Add(b.ShiftLeft(scale)), scale);
 088    }
 89
 90    public SimpleBigDecimal Negate()
 091    {
 092      return new SimpleBigDecimal(bigInt.Negate(), scale);
 093    }
 94
 95    public SimpleBigDecimal Subtract(SimpleBigDecimal b)
 096    {
 097      return Add(b.Negate());
 098    }
 99
 100    public SimpleBigDecimal Subtract(BigInteger b)
 0101    {
 0102      return new SimpleBigDecimal(bigInt.Subtract(b.ShiftLeft(scale)), scale);
 0103    }
 104
 105    public SimpleBigDecimal Multiply(SimpleBigDecimal b)
 0106    {
 0107      CheckScale(b);
 0108      return new SimpleBigDecimal(bigInt.Multiply(b.bigInt), scale + scale);
 0109    }
 110
 111    public SimpleBigDecimal Multiply(BigInteger b)
 0112    {
 0113      return new SimpleBigDecimal(bigInt.Multiply(b), scale);
 0114    }
 115
 116    public SimpleBigDecimal Divide(SimpleBigDecimal b)
 0117    {
 0118      CheckScale(b);
 0119      BigInteger dividend = bigInt.ShiftLeft(scale);
 0120      return new SimpleBigDecimal(dividend.Divide(b.bigInt), scale);
 0121    }
 122
 123    public SimpleBigDecimal Divide(BigInteger b)
 0124    {
 0125      return new SimpleBigDecimal(bigInt.Divide(b), scale);
 0126    }
 127
 128    public SimpleBigDecimal ShiftLeft(int n)
 0129    {
 0130      return new SimpleBigDecimal(bigInt.ShiftLeft(n), scale);
 0131    }
 132
 133    public int CompareTo(SimpleBigDecimal val)
 0134    {
 0135      CheckScale(val);
 0136      return bigInt.CompareTo(val.bigInt);
 0137    }
 138
 139    public int CompareTo(BigInteger val)
 0140    {
 0141      return bigInt.CompareTo(val.ShiftLeft(scale));
 0142    }
 143
 144    public BigInteger Floor()
 0145    {
 0146      return bigInt.ShiftRight(scale);
 0147    }
 148
 149    public BigInteger Round()
 0150    {
 0151      SimpleBigDecimal oneHalf = new SimpleBigDecimal(BigInteger.One, 1);
 0152      return Add(oneHalf.AdjustScale(scale)).Floor();
 0153    }
 154
 155    public int IntValue
 156    {
 0157      get { return Floor().IntValue; }
 158    }
 159
 160    public long LongValue
 161    {
 0162      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    {
 0177      get { return scale; }
 178    }
 179
 180    public override string ToString()
 0181    {
 0182      if (scale == 0)
 0183        return bigInt.ToString();
 184
 0185      BigInteger floorBigInt = Floor();
 186
 0187      BigInteger fract = bigInt.Subtract(floorBigInt.ShiftLeft(scale));
 0188      if (bigInt.SignValue < 0)
 0189      {
 0190        fract = BigInteger.One.ShiftLeft(scale).Subtract(fract);
 0191      }
 192
 0193      if ((floorBigInt.SignValue == -1) && (!(fract.Equals(BigInteger.Zero))))
 0194      {
 0195        floorBigInt = floorBigInt.Add(BigInteger.One);
 0196      }
 0197      string leftOfPoint = floorBigInt.ToString();
 198
 0199      char[] fractCharArr = new char[scale];
 0200        string fractStr = fract.ToString(2);
 0201      int fractLen = fractStr.Length;
 0202      int zeroes = scale - fractLen;
 0203      for (int i = 0; i < zeroes; i++)
 0204      {
 0205        fractCharArr[i] = '0';
 0206      }
 0207      for (int j = 0; j < fractLen; j++)
 0208      {
 0209        fractCharArr[zeroes + j] = fractStr[j];
 0210      }
 0211      string rightOfPoint = new string(fractCharArr);
 212
 0213      StringBuilder sb = new StringBuilder(leftOfPoint);
 0214      sb.Append(".");
 0215      sb.Append(rightOfPoint);
 216
 0217      return sb.ToString();
 0218    }
 219
 220    public override bool Equals(
 221      object obj)
 0222    {
 0223      if (this == obj)
 0224        return true;
 225
 0226      SimpleBigDecimal other = obj as SimpleBigDecimal;
 227
 0228      if (other == null)
 0229        return false;
 230
 0231      return bigInt.Equals(other.bigInt)
 0232        && scale == other.scale;
 0233    }
 234
 235    public override int GetHashCode()
 0236    {
 0237      return bigInt.GetHashCode() ^ scale;
 0238    }
 239
 240  }
 241}