/// <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)); } }
public static EFloat CloseToPowerOfTwo(IRandomGenExtended rg) { if (rg == null) { throw new ArgumentNullException(nameof(rg)); } int pwr = (rg.GetInt32(100) < 80) ? IntInRange(rg, -20, 20) : IntInRange(rg, -300, 300); int pwr2 = pwr - (rg.GetInt32(100) < 80 ? IntInRange(rg, 51, 61) : IntInRange(rg, 2, 300)); EFloat ef = null; ef = (rg.GetInt32(2) == 0) ? EFloat.Create(1, pwr).Add(EFloat.Create(1, pwr2)) : EFloat.Create(1, pwr).Subtract(EFloat.Create(1, pwr2)); if (rg.GetInt32(10) == 0) { pwr2 = pwr - (rg.GetInt32(100) < 80 ? IntInRange(rg, 51, 61) : IntInRange(rg, 2, 300)); ef = (rg.GetInt32(2) == 0) ? ef.Add(EFloat.Create(1, pwr2)) : ef.Subtract(EFloat.Create(1, pwr2)); } return(ef); }
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)); } }