Example #1
0
        /// <summary>Returns a number that expresses this number minus
        /// another.</summary>
        /// <param name='b'>The second operand to the subtraction.</param>
        /// <returns>A CBOR number that expresses this number minus the given
        /// number.</returns>
        /// <exception cref='ArgumentNullException'>The parameter <paramref
        /// name='b'/> is null.</exception>
        public CBORNumber Subtract(CBORNumber b)
        {
            if (b == null)
            {
                throw new ArgumentNullException(nameof(b));
            }
            CBORNumber a     = this;
            object     objA  = a.value;
            object     objB  = b.value;
            Kind       typeA = a.kind;
            Kind       typeB = b.kind;

            if (typeA == Kind.Integer && typeB == Kind.Integer)
            {
                var valueA = (long)objA;
                var valueB = (long)objB;
                if ((valueB < 0 && Int64.MaxValue + valueB < valueA) ||
                    (valueB > 0 && Int64.MinValue + valueB > valueA))
                {
                    // would overflow, convert to EInteger
                    return(CBORNumber.FromObject(
                               EInteger.FromInt64(valueA).Subtract(EInteger.FromInt64(valueB))));
                }
                return(new CBORNumber(Kind.Integer, valueA - valueB));
            }
            if (typeA == Kind.ERational || typeB == Kind.ERational)
            {
                ERational e1 = GetNumberInterface(typeA).AsExtendedRational(objA);
                ERational e2 = GetNumberInterface(typeB).AsExtendedRational(objB);
                return(new CBORNumber(Kind.ERational, e1.Subtract(e2)));
            }
            if (typeA == Kind.EDecimal || typeB == Kind.EDecimal)
            {
                EDecimal e1 = GetNumberInterface(typeA).AsExtendedDecimal(objA);
                EDecimal e2 = GetNumberInterface(typeB).AsExtendedDecimal(objB);
                return(new CBORNumber(Kind.EDecimal, e1.Subtract(e2)));
            }
            if (typeA == Kind.EFloat || typeB == Kind.EFloat ||
                typeA == Kind.Double || typeB == Kind.Double)
            {
                EFloat e1 = GetNumberInterface(typeA).AsExtendedFloat(objA);
                EFloat e2 = GetNumberInterface(typeB).AsExtendedFloat(objB);
                return(new CBORNumber(Kind.EFloat, e1.Subtract(e2)));
            }
            else
            {
                EInteger b1 = GetNumberInterface(typeA).AsEInteger(objA);
                EInteger b2 = GetNumberInterface(typeB).AsEInteger(objB);
                return(new CBORNumber(Kind.EInteger, b1 - (EInteger)b2));
            }
        }
Example #2
0
        public static CBORObject Subtract(CBORObject a, CBORObject b)
        {
            if (a == null)
            {
                throw new ArgumentNullException("a");
            }
            if (b == null)
            {
                throw new ArgumentNullException("b");
            }
            if (a.Type != CBORType.Number)
            {
                throw new ArgumentException("a.Type (" + a.Type +
                                            ") is not equal to " + CBORType.Number);
            }
            if (b.Type != CBORType.Number)
            {
                throw new ArgumentException("b.Type (" + b.Type +
                                            ") is not equal to " + CBORType.Number);
            }
            object objA  = a.ThisItem;
            object objB  = b.ThisItem;
            int    typeA = a.ItemType;
            int    typeB = b.ItemType;

            if (typeA == CBORObject.CBORObjectTypeInteger && typeB ==
                CBORObject.CBORObjectTypeInteger)
            {
                var valueA = (long)objA;
                var valueB = (long)objB;
                if ((valueB < 0 && Int64.MaxValue + valueB < valueA) ||
                    (valueB > 0 && Int64.MinValue + valueB > valueA))
                {
                    // would overflow, convert to EInteger
                    return(CBORObject.FromObject(((EInteger)valueA) -
                                                 (EInteger)valueB));
                }
                return(CBORObject.FromObject(valueA - valueB));
            }
            if (typeA == CBORObject.CBORObjectTypeExtendedRational ||
                typeB == CBORObject.CBORObjectTypeExtendedRational)
            {
                ERational e1 =
                    CBORObject.GetNumberInterface(typeA).AsExtendedRational(objA);
                ERational e2 =
                    CBORObject.GetNumberInterface(typeB).AsExtendedRational(objB);
                return(CBORObject.FromObject(e1.Subtract(e2)));
            }
            if (typeA == CBORObject.CBORObjectTypeExtendedDecimal ||
                typeB == CBORObject.CBORObjectTypeExtendedDecimal)
            {
                EDecimal e1 =
                    CBORObject.GetNumberInterface(typeA).AsExtendedDecimal(objA);
                EDecimal e2 =
                    CBORObject.GetNumberInterface(typeB).AsExtendedDecimal(objB);
                return(CBORObject.FromObject(e1.Subtract(e2)));
            }
            if (typeA == CBORObject.CBORObjectTypeExtendedFloat || typeB ==
                CBORObject.CBORObjectTypeExtendedFloat ||
                typeA == CBORObject.CBORObjectTypeDouble || typeB ==
                CBORObject.CBORObjectTypeDouble ||
                typeA == CBORObject.CBORObjectTypeSingle || typeB ==
                CBORObject.CBORObjectTypeSingle)
            {
                EFloat e1 =
                    CBORObject.GetNumberInterface(typeA).AsExtendedFloat(objA);
                EFloat e2 = CBORObject.GetNumberInterface(typeB).AsExtendedFloat(objB);
                return(CBORObject.FromObject(e1.Subtract(e2)));
            }
            else
            {
                EInteger b1 = CBORObject.GetNumberInterface(typeA).AsEInteger(objA);
                EInteger b2 = CBORObject.GetNumberInterface(typeB).AsEInteger(objB);
                return(CBORObject.FromObject(b1 - (EInteger)b2));
            }
        }