public bool CanFitInDouble(object obj) { var bigintItem = (EInteger)obj; EFloat ef = EFloat.FromEInteger(bigintItem); EFloat ef2 = EFloat.FromDouble(ef.ToDouble()); return(ef.CompareTo(ef2) == 0); }
public void MathComparisonTest() { var r = new ERational(142342, 432); var f = EFloat.FromString("1.5", null); Console.WriteLine("{0}, {1}", r, f); Assert.IsTrue(r.CompareToBinary(f) > 0); }
public static void TestStringEqualRoundTrip(EFloat obj) { Assert.AreEqual( EDecimal.PositiveInfinity, EDecimal.FromSingle(Single.PositiveInfinity)); Assert.AreEqual( EDecimal.NegativeInfinity, EDecimal.FromSingle(Single.NegativeInfinity)); Assert.AreEqual( EFloat.PositiveInfinity, EFloat.FromDouble(Double.PositiveInfinity)); Assert.AreEqual( EFloat.NegativeInfinity, EFloat.FromDouble(Double.NegativeInfinity)); Assert.AreEqual( EFloat.PositiveInfinity, EFloat.FromSingle(Single.PositiveInfinity)); Assert.AreEqual( EFloat.NegativeInfinity, EFloat.FromSingle(Single.NegativeInfinity)); Assert.AreEqual( ERational.PositiveInfinity, ERational.FromDouble(Double.PositiveInfinity)); Assert.AreEqual( ERational.NegativeInfinity, ERational.FromDouble(Double.NegativeInfinity)); Assert.AreEqual( ERational.PositiveInfinity, ERational.FromSingle(Single.PositiveInfinity)); Assert.AreEqual( ERational.NegativeInfinity, ERational.FromSingle(Single.NegativeInfinity)); Assert.AreEqual( ERational.PositiveInfinity, ERational.FromEDecimal(EDecimal.PositiveInfinity)); Assert.AreEqual( ERational.NegativeInfinity, ERational.FromEDecimal(EDecimal.NegativeInfinity)); Assert.AreEqual( ERational.PositiveInfinity, ERational.FromEFloat(EFloat.PositiveInfinity)); Assert.AreEqual( ERational.NegativeInfinity, ERational.FromEFloat(EFloat.NegativeInfinity)); Assert.IsTrue(Double.IsPositiveInfinity(ERational.PositiveInfinity.ToDouble())); Assert.IsTrue(Double.IsNegativeInfinity(ERational.NegativeInfinity.ToDouble())); Assert.IsTrue(Single.IsPositiveInfinity(ERational.PositiveInfinity.ToSingle())); Assert.IsTrue(Single.IsNegativeInfinity(ERational.NegativeInfinity.ToSingle())); }
/// <summary>Returns the sum of this number and another /// number.</summary> /// <param name='b'>The number to add with this one.</param> /// <returns>The sum of this number and another number.</returns> /// <exception cref='ArgumentNullException'>The parameter <paramref /// name='b'/> is null.</exception> public CBORNumber Add(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 ((valueA < 0 && valueB < Int64.MinValue - valueA) || (valueA > 0 && valueB > Int64.MaxValue - valueA)) { // would overflow, convert to EInteger return(CBORNumber.FromObject( EInteger.FromInt64(valueA).Add(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.Add(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.Add(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.Add(e2))); } else { EInteger b1 = GetNumberInterface(typeA).AsEInteger(objA); EInteger b2 = GetNumberInterface(typeB).AsEInteger(objB); return(new CBORNumber(Kind.EInteger, b1 + (EInteger)b2)); } }
/// <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)); } }
/// <include file='../docs.xml' /// path='docs/doc[@name="M:PeterO.ExtendedFloat.Create(PeterO.BigInteger,PeterO.BigInteger)"]/*'/> public static ExtendedFloat Create( BigInteger mantissa, BigInteger exponent) { if (mantissa == null) { throw new ArgumentNullException("mantissa"); } if (exponent == null) { throw new ArgumentNullException("exponent"); } return(new ExtendedFloat(EFloat.Create(mantissa.Ei, exponent.Ei))); }
public void TestToDouble() { // test for correct rounding double dbl; dbl = ERational.FromEDecimal( EDecimal.FromString( "1.972579273363468721491642554610734805464744567871093749999999999999")) .ToDouble(); { string stringTemp = EFloat.FromDouble(dbl).ToPlainString(); Assert.AreEqual( "1.9725792733634686104693400920950807631015777587890625", stringTemp); } }
public static ExtendedFloat FromString( string str, int offset, int length, PrecisionContext ctx) { try { return(new ExtendedFloat( EFloat.FromString( str, offset, length, ctx == null ? null : ctx.Ec))); } catch (ETrapException ex) { throw TrapException.Create(ex); } }
public static EFloat RandomEFloat(RandomGenerator r) { if (r.UniformInt(100) == 0) { int x = r.UniformInt(3); if (x == 0) { return(EFloat.PositiveInfinity); } if (x == 1) { return(EFloat.NegativeInfinity); } if (x == 2) { return(EFloat.NaN); } } return(EFloat.Create( RandomEInteger(r), (EInteger)(r.UniformInt(400) - 200))); }
internal static CBORObject ConvertToDecimalFrac( CBORObject o, bool isDecimal, bool extended) { if (o.Type != CBORType.Array) { throw new CBORException("Big fraction must be an array"); } if (o.Count != 2) { throw new CBORException("Big fraction requires exactly 2 items"); } if (!o[0].IsIntegral) { throw new CBORException("Exponent is not an integer"); } if (!o[1].IsIntegral) { throw new CBORException("Mantissa is not an integer"); } EInteger exponent = o[0].AsEInteger(); EInteger mantissa = o[1].AsEInteger(); if (exponent.GetSignedBitLength() > 64 && !extended) { throw new CBORException("Exponent is too big"); } if (exponent.IsZero) { // Exponent is 0, so return mantissa instead return(CBORObject.FromObject(mantissa)); } // NOTE: Discards tags. See comment in CBORTag2. return(isDecimal ? CBORObject.FromObject(EDecimal.Create(mantissa, exponent)) : CBORObject.FromObject(EFloat.Create(mantissa, exponent))); }
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 EFloat AsEFloat(object obj) { return(EFloat.FromDouble((double)obj)); }
public float AsSingle(object obj) { return(EFloat.FromEInteger((EInteger)obj).ToSingle()); }