Exemple #1
0
        public static byte[] ToByteArray(this OmgNum num)
        {
            if (num.IsZero())
            {
                return(new byte[1]);
            }

            var byteList = new List <byte>();

            for (int i = 0; i < num.Size; i++)
            {
                UInt32 digit = num.Raw.Digits[i];
                byteList.Add((byte)(digit & ((1 << 8) - 1)));
                digit >>= 8;
                byteList.Add((byte)(digit & ((1 << 8) - 1)));
            }

            while (byteList.Count > 0 && byteList[byteList.Count - 1] == 0)
            {
                byteList.RemoveAt(byteList.Count - 1);
            }

            byteList.Add((num.IsNegative) ? (byte)1 : (byte)0);
            return(byteList.ToArray());
        }
Exemple #2
0
 public static (OmgNum div, OmgNum mod) DivMod(OmgNum left, OmgNum right)
 {
     if (right.IsZero())
     {
         throw new OmgFailException("division by zero");
     }
     return(m_divider.DivMod(left, right));
 }
Exemple #3
0
        public static bool Equal(OmgNum left, OmgNum right)
        {
            if (left.IsNegative != right.IsNegative)
            {
                return(left.IsZero() && right.IsZero());
            }

            return(m_comparer.Equal(left.Raw, right.Raw));
        }
Exemple #4
0
        public static OmgNum Sqrt(OmgNum left)
        {
            if (left.IsZero())
            {
                return(OmgPool.GetZero());
            }

            if (left.IsNegative)
            {
                throw new OmgFailException("can't square root negative number");
            }

            return(_Positive(m_rooter.Sqrt(left.Raw)));
        }
Exemple #5
0
        private static T _WithModded <T> (OmgNum left, OmgNum right, OmgNum mod, Func <OmgNum, OmgNum, T> func)
        {
            if (mod == null)
            {
                throw new OmgFailException("mod is null");
            }
            if (mod.IsZero())
            {
                throw new OmgFailException("mod is zero");
            }

            var leftModded  = Mod(left, mod);
            var rightModded = Mod(right, mod);

            T result = func(leftModded, rightModded);

            leftModded.Release();
            rightModded.Release();

            return(result);
        }