Exemplo n.º 1
0
        public Int32 CompareTo(BigFloat o)
        {
            if (Equals(o))
            {
                return(0);
            }

            if (Sign && !o.Sign)
            {
                return(1);
            }
            else if (!Sign && !o.Sign)
            {
                return(-1);
            }

            AlignExponent(o);
            o.AlignExponent(this);

            Int32 expDifference = (_exp + _data.Count) - (o._exp + o._data.Count);

            if (expDifference > 0)
            {
                return(1);
            }
            else if (expDifference < 0)
            {
                return(-1);
            }

            // by now both will have the same length
            for (int i = Length - 1; i >= 0; i--)
            {
                if (this[i] > o[i])
                {
                    return(1);
                }
                if (this[i] < o[i])
                {
                    return(-1);
                }
            }

            return(0);
        }
Exemplo n.º 2
0
        private static BigFloat Subtract(BigFloat a, BigFloat b, Boolean normalise)
        {
            a.Normalise();
            b.Normalise();

            a = (BigFloat)a.Clone();
            b = (BigFloat)b.Clone();

            if (a.IsZero && b.IsZero)
            {
                return(a);
            }
            if (a.Sign && !b.Sign)
            {
                b._sign = true;
                return(Add(a, b, normalise));
            }
            if (!a.Sign && b.Sign)
            {
                a._sign = true;
                BigFloat added = Add(a, b, normalise);
                return((BigFloat)added.Negate());
            }


            BigFloat result = new BigFloat();

            a.AlignExponent(b);
            b.AlignExponent(a);

            result._exp = a._exp;

            Boolean wasSwapped = false;

            if (b.Length > a.Length)              // then switch them around
            {
                BigFloat temp = a;
                a          = b;
                b          = temp;
                wasSwapped = true;
            }
            else
            {
                // if same length, check magnitude
                BigFloat a1 = (BigFloat)a.Absolute();
                BigFloat b1 = (BigFloat)b.Absolute();
                if (a1 < b1)
                {
                    BigFloat temp = a;
                    a          = b;
                    b          = temp;
                    wasSwapped = true;
                }
                else if (!(a1 > b1))                      // i.e. equal
                {
                    return(new BigFloat());               // return zero
                }
            }

            // Do work
            // it's a sad day when the preparation for an operation is just as long as the operation itself


            SByte digit = 0;
            SByte take  = 0;

            for (int i = 0; i < b.Length; i++)
            {
                digit = (SByte)(a[i] - b[i] - take);
                if (digit < 0)
                {
                    take  = 1;
                    digit = (SByte)(10 + digit);
                }
                else
                {
                    take = 0;
                }
                result._data.Add(digit);
            }

            for (int i = b.Length; i < a.Length; i++)
            {
                digit = (SByte)(a[i] - take);
                if (digit < 0)
                {
                    take  = 1;
                    digit = (SByte)(10 + digit);
                }
                else
                {
                    take = 0;
                }
                result._data.Add(digit);
            }

            result._sign = a.Sign && b.Sign ? !wasSwapped : wasSwapped;

/*			if(normalise)
 *                              result.Normalise(); */

            return(result);
        }
Exemplo n.º 3
0
        private static BigFloat Add(BigFloat a, BigFloat b, Boolean normalise)
        {
            a.Normalise();
            b.Normalise();

            a = (BigFloat)a.Clone();
            b = (BigFloat)b.Clone();

            if (a.IsZero && b.IsZero)
            {
                return(a);
            }
            if (a.Sign && !b.Sign)
            {
                b._sign = true;
                return(Subtract(a, b, normalise));
            }
            if (!a.Sign && b.Sign)
            {
                b._sign = true;
                return(Subtract(b, a, normalise));
            }

            BigFloat result = new BigFloat();

            a.AlignExponent(b);
            b.AlignExponent(a);

            result._exp = a._exp;

            if (b.Length > a.Length)              // then switch them around
            {
                BigFloat temp = a;
                a = b;
                b = temp;
            }

            // the work:
            //
            SByte digit = 0;
            SByte carry = 0;

            for (int i = 0; i < b.Length; i++)
            {
                digit = (SByte)((a[i] + b[i] + carry) % 10);
                carry = (SByte)((a[i] + b[i] + carry) / 10);
                result._data.Add(digit);
            }
            for (int i = b.Length; i < a.Length; i++)
            {
                digit = (SByte)((a[i] + carry) % 10);
                carry = (SByte)((a[i] + carry) / 10);
                result._data.Add(digit);
            }
            if (carry > 0)
            {
                result._data.Add(carry);
            }

            result._sign = a.Sign && b.Sign;

/*			if(normalise) // normalising shouldn't be necessary, but what the heck
 *                              result.Normalise(); */

            return(result);
        }
Exemplo n.º 4
0
        private static BigFloat Subtract(BigFloat a, BigFloat b, Boolean normalise)
        {
            a.Normalise();
            b.Normalise();

            a = (BigFloat)a.Clone();
            b = (BigFloat)b.Clone();

            if( a.IsZero && b.IsZero ) return a;
            if( a.Sign && !b.Sign ) {
                b._sign = true;
                return Add(a, b, normalise);
            }
            if( !a.Sign && b.Sign ) {
                a._sign = true;
                BigFloat added = Add(a, b, normalise);
                return (BigFloat)added.Negate();
            }

            BigFloat result = new BigFloat();

            a.AlignExponent( b );
            b.AlignExponent( a );

            result._exp = a._exp;

            Boolean wasSwapped = false;
            if(b.Length > a.Length) { // then switch them around
                BigFloat temp = a;
                a = b;
                b = temp;
                wasSwapped = true;
            } else {
                // if same length, check magnitude
                BigFloat a1 = (BigFloat)a.Absolute();
                BigFloat b1 = (BigFloat)b.Absolute();
                if(a1 < b1) {
                    BigFloat temp = a;
                    a = b;
                    b = temp;
                    wasSwapped = true;
                } else if( !(a1 > b1) ) { // i.e. equal
                    return new BigFloat(); // return zero
                }
            }

            // Do work
            // it's a sad day when the preparation for an operation is just as long as the operation itself

            SByte digit = 0;
            SByte take = 0;

            for(int i=0;i<b.Length;i++ ) {
                digit = (SByte)( a[i] - b[i] - take );
                if( digit < 0 ) {
                    take = 1;
                    digit = (SByte)( 10 + digit );
                } else {
                    take = 0;
                }
                result._data.Add( digit );
            }

            for(int i=b.Length;i<a.Length;i++ ) {
                digit = (SByte)( a[i] - take );
                if( digit < 0 ) {
                    take = 1;
                    digit = (SByte)( 10 + digit );
                } else {
                    take = 0;
                }
                result._data.Add( digit );
            }

            result._sign = a.Sign && b.Sign ? !wasSwapped : wasSwapped;

            /*			if(normalise)
                result.Normalise(); */

            return result;
        }
Exemplo n.º 5
0
        private static BigFloat Add(BigFloat a, BigFloat b, Boolean normalise)
        {
            a.Normalise();
            b.Normalise();

            a = (BigFloat)a.Clone();
            b = (BigFloat)b.Clone();

            if(a.IsZero && b.IsZero) return a;
            if(a.Sign && !b.Sign) {
                b._sign = true;
                return Subtract(a, b, normalise);
            }
            if(!a.Sign && b.Sign) {
                b._sign = true;
                return Subtract(b, a, normalise);
            }

            BigFloat result = new BigFloat();

            a.AlignExponent( b );
            b.AlignExponent( a );

            result._exp = a._exp;

            if(b.Length > a.Length) { // then switch them around
                BigFloat temp = a;
                a = b;
                b = temp;
            }

            // the work:
            //
            SByte digit = 0;
            SByte carry = 0;
            for(int i=0;i<b.Length;i++) {
                digit = (SByte)( ( a[i] + b[i] + carry ) % 10 );
                carry = (SByte)( ( a[i] + b[i] + carry ) / 10 );
                result._data.Add( digit );
            }
            for(int i=b.Length;i<a.Length;i++) {
                digit = (SByte)( ( a[i] + carry ) % 10 );
                carry = (SByte)( ( a[i] + carry ) / 10 );
                result._data.Add( digit );
            }
            if( carry > 0 ) result._data.Add( carry );

            result._sign = a.Sign && b.Sign;

            /*			if(normalise) // normalising shouldn't be necessary, but what the heck
                result.Normalise(); */

            return result;
        }
Exemplo n.º 6
0
        public Int32 CompareTo(BigFloat o)
        {
            if( Equals(o) ) return 0;

            if(Sign && !o.Sign) return 1;
            else if(!Sign && !o.Sign) return -1;

            AlignExponent(o);
            o.AlignExponent(this);

            Int32 expDifference = ( _exp + _data.Count ) - ( o._exp + o._data.Count );
            if(expDifference > 0) return 1;
            else if(expDifference < 0) return -1;

            // by now both will have the same length
            for(int i=Length-1;i>=0;i--) {
                if( this[i] > o[i] ) return 1;
                if( this[i] < o[i] ) return -1;
            }

            return 0;
        }