Exemple #1
0
        public override MMC.Numbers.CNumber sub(MMC.Numbers.CNumber other)
        {
            if (other.MyType != MyType)
            {
                throw new NotImplementedException();
            }
            CNumber_Integer res = new CNumber_Integer(this);

            if (_Sign != ((CNumber_Integer)other)._Sign)
            {
                res.Addition((CNumber_Integer)other);
            }
            else
            {
                res.Subtraction((CNumber_Integer)other);
            }
            return(res);
        }
Exemple #2
0
        //------------------------------------------------------------
        // divide by another number and return the remainder
        protected virtual CNumber_Integer Divide(CNumber_Integer other)
        {
            CNumber_Integer Save = new CNumber_Integer(this);       // save the old value

            setZero();                                              // set the initial result to 0

            int o_len = other._Values.Count;
            int t_len = _Values.Count;
            int pos   = t_len - o_len;

            if (pos < 0)
            {
                return(Save);                                       // fewer digits means the result is zero and the remainder are just we
            }
            CNumber_Integer Rem = new CNumber_Integer();            // set the remainder to 0
            int             len = o_len - 1;

            while (pos >= 0)
            {
                Rem.InsertDigit(Save._Values[pos]);                 // take the next digit

                byte q = Rem._Values[len];
                q /= other._Values[len];                            // and make an estimate for the quotient

                CNumber_Integer tmp = new CNumber_Integer(other);   // and calculate the product for this estimated value
                tmp._Sign = false;
                tmp.Multiply(q);
                Rem.Subtraction(tmp);                               // see how good we estimated
                if (Rem._Sign)                                      // if we are too low just correct by one
                {
                    --q;
                    Rem.add(other);
                }

                InsertDigit(q);                                     // and write down the quotients digit
                --pos;                                              // walk one further in our (this) digits
            }

            _Sign = (_Sign != other._Sign);
            Trim();
            return(Rem);
        }