public void TestDivide() { var fr = new RandomGenerator(); for (var i = 0; i < 500; ++i) { ERational er = RandomObjects.RandomERational(fr); ERational er2 = RandomObjects.RandomERational(fr); if (er2.IsZero || !er2.IsFinite) { continue; } if (er.IsZero || !er.IsFinite) { continue; } ERational ermult = er.Multiply(er2); ERational erdiv = ermult.Divide(er); TestCommon.CompareTestEqual(erdiv, er2); erdiv = ermult.Divide(er2); TestCommon.CompareTestEqual(erdiv, er); } }
public static CBORObject Multiply(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; bool apos = valueA > 0L; bool bpos = valueB > 0L; if ( (apos && ((!bpos && (Int64.MinValue / valueA) > valueB) || (bpos && valueA > (Int64.MaxValue / valueB)))) || (!apos && ((!bpos && valueA != 0L && (Int64.MaxValue / valueA) > valueB) || (bpos && valueA < (Int64.MinValue / valueB))))) { // would overflow, convert to EInteger var bvalueA = (EInteger)valueA; var bvalueB = (EInteger)valueB; return(CBORObject.FromObject(bvalueA * (EInteger)bvalueB)); } 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.Multiply(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.Multiply(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.Multiply(e2))); } else { EInteger b1 = CBORObject.GetNumberInterface(typeA).AsEInteger(objA); EInteger b2 = CBORObject.GetNumberInterface(typeB).AsEInteger(objB); return(CBORObject.FromObject(b1 * (EInteger)b2)); } }
/// <summary>Returns a CBOR number expressing the product of this /// number and the given number.</summary> /// <param name='b'>The second operand to the multiplication /// operation.</param> /// <returns>A number expressing the product of this number and the /// given number.</returns> /// <exception cref='ArgumentNullException'>The parameter <paramref /// name='b'/> is null.</exception> public CBORNumber Multiply(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; bool apos = valueA > 0L; bool bpos = valueB > 0L; if ( (apos && ((!bpos && (Int64.MinValue / valueA) > valueB) || (bpos && valueA > (Int64.MaxValue / valueB)))) || (!apos && ((!bpos && valueA != 0L && (Int64.MaxValue / valueA) > valueB) || (bpos && valueA < (Int64.MinValue / valueB))))) { // would overflow, convert to EInteger var bvalueA = (EInteger)valueA; var bvalueB = (EInteger)valueB; return(CBORNumber.FromObject(bvalueA * (EInteger)bvalueB)); } return(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.Multiply(e2))); } if (typeA == Kind.EDecimal || typeB == Kind.EDecimal) { EDecimal e1 = GetNumberInterface(typeA).AsExtendedDecimal(objA); EDecimal e2 = GetNumberInterface(typeB).AsExtendedDecimal(objB); return(CBORNumber.FromObject(e1.Multiply(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.Multiply(e2))); } else { EInteger b1 = GetNumberInterface(typeA).AsEInteger(objA); EInteger b2 = GetNumberInterface(typeB).AsEInteger(objB); return(new CBORNumber(Kind.EInteger, b1 * (EInteger)b2)); } }