コード例 #1
0
        public static BigInt operator /(BigInt B1, BigInt B2)
        {
            B2 = (BigInt)B1.SameSize(B2);
            BigInt B2Copy = B2.Clone();
            BigInt Q      = new BigInt(B2Copy.Size, 0);
            BigInt R      = new BigInt(B2Copy.Size, 0);

            switch (B1.LookAtSign(B2Copy))
            {
            case signt2.pp:
                _Divmod(ref Q, ref R, ref B1, ref B2Copy, signt.positive);
                return(Q);

            case signt2.np:
                _Divmod(ref Q, ref R, ref B1, ref B2Copy, signt.negative);
                return(Q);

            case signt2.pn:
                _Divmod(ref Q, ref R, ref B1, ref B2Copy, signt.negative);
                return(Q);

            case signt2.nn:
                _Divmod(ref Q, ref R, ref B1, ref B2Copy, signt.positive);
                return(Q);

            default:
                throw new Exception("Could not determine the sign of the result value.");
            }
        }
コード例 #2
0
        public static BigInt operator %(BigInt B1, BigInt B2)
        {
            B2 = (BigInt)B1.SameSize(B2);
            BigInt B2Copy = B2.Clone();
            BigInt Q      = new BigInt(B2Copy.Size, 0);
            BigInt R      = new BigInt(B2Copy.Size, 0);

            switch (B1.LookAtSign(B2Copy))
            {
            case signt2.pp:
                _Divmod(ref Q, ref R, ref B1, ref B2Copy, signt.positive);
                break;

            case signt2.np:
                _Divmod(ref Q, ref R, ref B1, ref B2Copy, signt.negative);
                break;

            case signt2.pn:
                _Divmod(ref Q, ref R, ref B1, ref B2Copy, signt.negative);
                break;

            case signt2.nn:
                _Divmod(ref Q, ref R, ref B1, ref B2Copy, signt.positive);
                break;

            default:
                throw new Exception("Could not determine the sign of the result value.");
            }

            if (B1.Sign == signt.negative && R != 0)
            {
                BigInt RCopy = R.Clone();
                RCopy.Sign  = signt.negative;
                B2Copy.Sign = signt.positive;

                RCopy = RCopy + B2Copy;
                while (!(RCopy >= 0 && RCopy.Sign == signt.positive))
                {
                    RCopy = RCopy + B2Copy;
                }
                R      = RCopy.Clone();
                R.Sign = signt.positive;
            }
            return(R);
        }