Beispiel #1
0
        public static LexoInteger Parse(string strFull, ILexoNumeralSystem system)
        {
            var str  = strFull;
            var sign = 1;

            if (strFull.IndexOf(system.GetPositiveChar()) == 0)
            {
                str = strFull.Substring(1);
            }
            else if (strFull.IndexOf(system.GetNegativeChar()) == 0)
            {
                str  = strFull.Substring(1);
                sign = -1;
            }

            var mag      = new int[str.Length];
            var strIndex = mag.Length - 1;

            for (var magIndex = 0; strIndex >= 0; ++magIndex)
            {
                mag[magIndex] = system.ToDigit(str[strIndex]);
                --strIndex;
            }

            return(Make(system, sign, mag));
        }
Beispiel #2
0
        private static int[] Subtract(ILexoNumeralSystem sys, int[] l, int[] r)
        {
            var rComplement = Complement(sys, r, l.Length);
            var rSum        = Add(sys, l, rComplement);

            rSum[rSum.Length - 1] = 0;
            return(Add(sys, rSum, OneMag));
        }
Beispiel #3
0
        private static int[] Complement(ILexoNumeralSystem sys, int[] mag, int digits)
        {
            if (digits <= 0)
            {
                throw new LexoException("Expected at least 1 digit");
            }

            var nmag = new int[digits];

            nmag.Fill(sys.GetBase() - 1);

            for (var i = 0; i < mag.Length; ++i)
            {
                nmag[i] = sys.GetBase() - 1 - mag[i];
            }

            return(nmag);
        }
        public static LexoDecimal Parse(string str, ILexoNumeralSystem system)
        {
            var partialIndex = str.IndexOf(system.GetRadixPointChar());

            if (str.LastIndexOf(system.GetRadixPointChar()) != partialIndex)
            {
                throw new FormatException("More than one " + system.GetRadixPointChar());
            }

            if (partialIndex < 0)
            {
                return(Make(LexoInteger.Parse(str, system), 0));
            }

            var intStr = str.Substring(0, partialIndex) + str.Substring(partialIndex + 1);

            return(Make(LexoInteger.Parse(intStr, system), str.Length - 1 - partialIndex));
        }
Beispiel #5
0
        private static int[] Multiply(ILexoNumeralSystem sys, int[] l, int[] r)
        {
            var result = new int[l.Length + r.Length];

            for (var li = 0; li < l.Length; ++li)
            {
                for (var ri = 0; ri < r.Length; ++ri)
                {
                    var resultIndex = li + ri;

                    for (result[resultIndex] += l[li] * r[ri];
                         result[resultIndex] >= sys.GetBase();
                         result[resultIndex] -= sys.GetBase())
                    {
                        ++result[resultIndex + 1];
                    }
                }
            }

            return(result);
        }
Beispiel #6
0
        private static int[] Add(ILexoNumeralSystem sys, IReadOnlyList <int> l, IReadOnlyList <int> r)
        {
            var estimatedSize = Math.Max(l.Count, r.Count);
            var result        = new int[estimatedSize];
            var carry         = 0;

            for (var i = 0; i < estimatedSize; ++i)
            {
                var lNum = i < l.Count ? l[i] : 0;
                var rNum = i < r.Count ? r[i] : 0;
                var sum  = lNum + rNum + carry;

                for (carry = 0; sum >= sys.GetBase(); sum -= sys.GetBase())
                {
                    ++carry;
                }

                result[i] = sum;
            }

            return(ExtendWithCarry(result, carry));
        }
Beispiel #7
0
        public static LexoInteger Make(ILexoNumeralSystem sys, int sign, int[] mag)
        {
            int actualLength;

            for (actualLength = mag.Length; actualLength > 0 && mag[actualLength - 1] == 0; --actualLength)
            {
            }

            if (actualLength == 0)
            {
                return(Zero(sys));
            }

            if (actualLength == mag.Length)
            {
                return(new LexoInteger(sys, sign, mag));
            }

            var nmag = new int[actualLength];

            Array.Copy(mag, 0, nmag, 0, actualLength);
            return(new LexoInteger(sys, sign, nmag));
        }
Beispiel #8
0
 internal static LexoInteger One(ILexoNumeralSystem sys)
 {
     return(Make(sys, 1, OneMag));
 }
Beispiel #9
0
 internal static LexoInteger Zero(ILexoNumeralSystem sys)
 {
     return(new LexoInteger(sys, 0, ZeroMag));
 }
Beispiel #10
0
 private LexoInteger(ILexoNumeralSystem system, int sign, int[] mag)
 {
     _sys  = system;
     _sign = sign;
     _mag  = mag;
 }
Beispiel #11
0
        public static LexoDecimal Half(ILexoNumeralSystem sys)
        {
            var mid = sys.GetBase() / 2;

            return(Make(LexoInteger.Make(sys, 1, new[] { mid }), 1));
        }