Beispiel #1
0
        public void TestRemainder()
        {
            var fr = new RandomGenerator();

            for (var i = 0; i < 100; ++i)
            {
                ERational er;
                ERational er2;
                er = ERational.Create(
                    RandomObjects.RandomEInteger(fr),
                    EInteger.One);
                er2 = ERational.Create(
                    RandomObjects.RandomEInteger(fr),
                    EInteger.One);
                if (er2.IsZero || !er2.IsFinite)
                {
                    continue;
                }
                if (er.IsZero || !er.IsFinite)
                {
                    // Code below will divide by "er",
                    // so skip if "er" is zero
                    continue;
                }
                ERational ermult = er.Multiply(er2);
                ERational erdiv  = ermult.Divide(er);
                erdiv = ermult.Remainder(er);
                if (!erdiv.IsZero)
                {
                    Assert.Fail(ermult + "; " + er);
                }
                erdiv = ermult.Remainder(er2);
                if (!erdiv.IsZero)
                {
                    Assert.Fail(er + "; " + er2);
                }
            }
        }
Beispiel #2
0
        /// <summary>Returns the remainder when this number is divided by
        /// another number.</summary>
        /// <param name='b'>The right-hand side (dividend) of the remainder
        /// operation.</param>
        /// <returns>The remainder when this number is divided by the other
        /// number.</returns>
        /// <exception cref='ArgumentNullException'>The parameter <paramref
        /// name='b'/> is null.</exception>
        public CBORNumber Remainder(CBORNumber b)
        {
            if (b == null)
            {
                throw new ArgumentNullException(nameof(b));
            }
            object objA  = this.value;
            object objB  = b.value;
            Kind   typeA = this.kind;
            Kind   typeB = b.kind;

            if (typeA == Kind.Integer && typeB == Kind.Integer)
            {
                var valueA = (long)objA;
                var valueB = (long)objB;
                return((valueA == Int64.MinValue && valueB == -1) ?
                       CBORNumber.FromObject(0) : CBORNumber.FromObject(valueA % valueB));
            }
            if (typeA == Kind.ERational ||
                typeB == Kind.ERational)
            {
                ERational e1 =
                    GetNumberInterface(typeA).AsExtendedRational(objA);
                ERational e2 = GetNumberInterface(typeB).AsExtendedRational(objB);
                return(CBORNumber.FromObject(e1.Remainder(e2)));
            }
            if (typeA == Kind.EDecimal ||
                typeB == Kind.EDecimal)
            {
                EDecimal e1 =
                    GetNumberInterface(typeA).AsExtendedDecimal(objA);
                EDecimal e2 = GetNumberInterface(typeB).AsExtendedDecimal(objB);
                return(CBORNumber.FromObject(e1.Remainder(e2, null)));
            }
            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(CBORNumber.FromObject(e1.Remainder(e2, null)));
            }
            else
            {
                EInteger b1 = GetNumberInterface(typeA).AsEInteger(objA);
                EInteger b2 = GetNumberInterface(typeB).AsEInteger(objB);
                return(CBORNumber.FromObject(b1 % (EInteger)b2));
            }
        }
Beispiel #3
0
        public static CBORObject Remainder(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;
                return((valueA == Int64.MinValue && valueB == -1) ?
                       CBORObject.FromObject(0) : 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.Remainder(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.Remainder(e2, null)));
            }
            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.Remainder(e2, null)));
            }
            else
            {
                EInteger b1 = CBORObject.GetNumberInterface(typeA).AsEInteger(objA);
                EInteger b2 = CBORObject.GetNumberInterface(typeB).AsEInteger(objB);
                return(CBORObject.FromObject(b1 % (EInteger)b2));
            }
        }