Exemplo n.º 1
0
        public void Subtract(LongNumber value)
        {
            VerifySameSize(value);

            int index = size - 1;

            while (index >= 0 && value.number[index] == 0)
            {
                index--;
            }

            UInt32 borrow = 0;

            while (index >= 0)
            {
                UInt64 result = 0x100000000U + (UInt64)number[index] -
                                value.number[index] - borrow;
                number[index] = (UInt32)result;
                if (result >= 0x100000000U)
                {
                    borrow = 0;
                }
                else
                {
                    borrow = 1;
                }
                index--;
            }
        }
Exemplo n.º 2
0
        public void Add(LongNumber value)
        {
            VerifySameSize(value);

            int index = size - 1;

            while (index >= 0 && value.number[index] == 0)
            {
                index--;
            }

            UInt32 carry = 0;

            while (index >= 0)
            {
                UInt64 result = (UInt64)number[index] +
                                value.number[index] + carry;
                number[index] = (UInt32)result;
                if (result >= 0x100000000U)
                {
                    carry = 1;
                }
                else
                {
                    carry = 0;
                }
                index--;
            }
        }
Exemplo n.º 3
0
        public string Print()
        {
            LongNumber temp = new LongNumber(maxDigits);

            temp.Assign(this);

            StringBuilder sb = new StringBuilder();

            sb.Append(temp.number[0]);
            sb.Append(System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator);

            int digitCount = 0;

            while (digitCount < maxDigits)
            {
                temp.number[0] = 0;
                temp.Multiply(100000);
                sb.AppendFormat("{0:D5}", temp.number[0]);
                digitCount += 5;
            }
            string result = sb.ToString();
            int    rest   = maxDigits % 5;

            if (rest != 0)
            {
                result = result.Substring(0, result.Length - (5 - rest));
            }
            return(result);
        }
Exemplo n.º 4
0
 public void Assign(LongNumber value)
 {
     VerifySameSize(value);
     for (int i = 0; i < size; i++)
     {
         number[i] = value.number[i];
     }
 }
Exemplo n.º 5
0
        private static async Task <string> CalculatePiPrecision(int precision, CancellationToken cancellationToken = default)
        {
            LongNumber x = new LongNumber(precision);
            LongNumber y = new LongNumber(precision);
            await x.ArcTan(16, 5, cancellationToken);

            await y.ArcTan(4, 239, cancellationToken);

            x.Subtract(y);
            return(x.Print());
        }
Exemplo n.º 6
0
 private void VerifySameSize(LongNumber value)
 {
     if (Object.ReferenceEquals(this, value))
     {
         throw new Exception("LongNumbers cannot operate on themselves");
     }
     if (value.size != this.size)
     {
         throw new Exception("LongNumbers must have the same size");
     }
 }
Exemplo n.º 7
0
        public async Task ArcTan(UInt32 multiplicand, UInt32 reciprocal, CancellationToken cancellationToken = default)
        {
            LongNumber X = new LongNumber(maxDigits, multiplicand);

            X.Divide(reciprocal);
            reciprocal *= reciprocal;

            this.Assign(X);

            LongNumber term         = new LongNumber(maxDigits);
            UInt32     divisor      = 1;
            bool       subtractTerm = true;

            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();
                X.Divide(reciprocal);
                term.Assign(X);
                divisor += 2;
                term.Divide(divisor);
                if (term.IsZero())
                {
                    break;
                }

                if (subtractTerm)
                {
                    this.Subtract(term);
                }
                else
                {
                    this.Add(term);
                }
                subtractTerm = !subtractTerm;
            }
        }