RandomERational() public static méthode

public static RandomERational ( RandomGenerator rand ) : PeterO.Numbers.ERational
rand PeterO.RandomGenerator
Résultat PeterO.Numbers.ERational
Exemple #1
0
        public static CBORObject RandomNumberOrRational(RandomGenerator rand)
        {
            switch (rand.UniformInt(7))
            {
            case 0:
                return(CBORObject.FromObject(
                           RandomObjects.RandomDouble(
                               rand,
                               Int32.MaxValue)));

            case 1:
                return(CBORObject.FromObject(
                           RandomObjects.RandomSingle(
                               rand,
                               Int32.MaxValue)));

            case 2:
                return(CBORObject.FromObject(RandomObjects.RandomEInteger(rand)));

            case 3:
                return(CBORObject.FromObject(RandomObjects.RandomEFloat(rand)));

            case 4:
                return
                    (CBORObject.FromObject(RandomObjects.RandomEDecimal(rand)));

            case 5:
                return(CBORObject.FromObject(RandomObjects.RandomInt64(rand)));

            case 6:
                return(CBORObject.FromObject(RandomObjects.RandomERational(rand)));

            default: throw new ArgumentException();
            }
        }
Exemple #2
0
        public void TestCompareTo()
        {
            var r = new RandomGenerator();

            for (var i = 0; i < 500; ++i)
            {
                ERational bigintA = RandomObjects.RandomERational(r);
                ERational bigintB = RandomObjects.RandomERational(r);
                ERational bigintC = RandomObjects.RandomERational(r);
                TestCommon.CompareTestRelations(bigintA, bigintB, bigintC);
            }
            TestCommon.CompareTestLess(ERational.Zero, ERational.NaN);
            ERational rat, rat2;

            for (var i = 0; i < 100; ++i)
            {
                EInteger num = RandomObjects.RandomEInteger(r);
                if (num.IsZero)
                {
                    // Skip if number is 0; 0/1 and 0/2 are
                    // equal in that case
                    continue;
                }
                num  = num.Abs();
                rat  = ERational.Create(num, EInteger.One);
                rat2 = ERational.Create(num, (EInteger)2);
                TestCommon.CompareTestLess(rat2, rat);
                TestCommon.CompareTestGreater(rat, rat2);
            }
            TestCommon.CompareTestLess(
                ERational.Create(EInteger.One, (EInteger)2),
                ERational.Create((EInteger)4, EInteger.One));
            for (var i = 0; i < 100; ++i)
            {
                EInteger num = RandomObjects.RandomEInteger(r);
                EInteger den = RandomObjects.RandomEInteger(r);
                if (den.IsZero)
                {
                    den = EInteger.One;
                }
                rat = ERational.Create(num, den);
                for (int j = 0; j < 10; ++j)
                {
                    EInteger num2 = num;
                    EInteger den2 = den;
                    EInteger mult = RandomObjects.RandomEInteger(r);
                    if (mult.IsZero || mult.Equals(EInteger.One))
                    {
                        mult = (EInteger)2;
                    }
                    num2 *= (EInteger)mult;
                    den2 *= (EInteger)mult;
                    rat2  = ERational.Create(num2, den2);
                    TestCommon.CompareTestEqual(rat, rat2);
                }
            }
        }
Exemple #3
0
        public void TestToString()
        {
            var       fr = new RandomGenerator();
            ERational dec;

            for (var i = 0; i < 1000; ++i)
            {
                dec = RandomObjects.RandomERational(fr);
                ExtraTest.TestStringEqualRoundTrip(dec);
            }
            dec = ERational.FromString("-0/500");
            ExtraTest.TestStringEqualRoundTrip(dec);
        }
Exemple #4
0
        public void TestCompareToDecimal()
        {
            var fr = new RandomGenerator();

            for (var i = 0; i < 100; ++i)
            {
                ERational er  = RandomObjects.RandomERational(fr);
                int       exp = -100000 + fr.UniformInt(200000);
                EDecimal  ed  = EDecimal.Create(
                    RandomObjects.RandomEInteger(fr),
                    (EInteger)exp);
                ERational er2 = ERational.FromEDecimal(ed);
                int       c2r = er.CompareTo(er2);
                int       c2d = er.CompareToDecimal(ed);
                Assert.AreEqual(c2r, c2d);
            }
        }
Exemple #5
0
        public static CBORObject RandomNumberOrRational(IRandomGenExtended rand)
        {
            object o = null;

            switch (rand.GetInt32(7))
            {
            case 0:
                o = RandomObjects.RandomDouble(
                    rand,
                    Int32.MaxValue);
                return(CBORObject.FromObject(o));

            case 1:
                o = RandomObjects.RandomSingle(
                    rand,
                    Int32.MaxValue);
                return(CBORObject.FromObject(o));

            case 2:
                return(CBORObject.FromObject(
                           RandomObjects.RandomEInteger(rand)));

            case 3:
                return(CBORObject.FromObject(
                           RandomObjects.RandomEFloat(rand)));

            case 4:
                o = RandomObjects.RandomEDecimal(rand);
                return(CBORObject.FromObject(o));

            case 5:
                o = RandomObjects.RandomInt64(rand);
                return(CBORObject.FromObject(o));

            case 6:
                o = RandomObjects.RandomERational(rand);
                return(CBORObject.FromObject(o));

            default: throw new InvalidOperationException();
            }
        }
Exemple #6
0
        public static CBORObject RandomNumberOrRational(RandomGenerator rand)
        {
            object o = null;

            switch (rand.UniformInt(7))
            {
            case 0:
                o = RandomObjects.RandomDouble(
                    rand,
                    Int32.MaxValue);
                return(ToObjectTest.TestToFromObjectRoundTrip(o));

            case 1:
                o = RandomObjects.RandomSingle(
                    rand,
                    Int32.MaxValue);
                return(ToObjectTest.TestToFromObjectRoundTrip(o));

            case 2:
                return(ToObjectTest.TestToFromObjectRoundTrip(
                           RandomObjects.RandomEInteger(rand)));

            case 3:
                return(ToObjectTest.TestToFromObjectRoundTrip(
                           RandomObjects.RandomEFloat(rand)));

            case 4:
                o = RandomObjects.RandomEDecimal(rand);
                return(ToObjectTest.TestToFromObjectRoundTrip(o));

            case 5:
                o = RandomObjects.RandomInt64(rand);
                return(ToObjectTest.TestToFromObjectRoundTrip(o));

            case 6:
                o = RandomObjects.RandomERational(rand);
                return(ToObjectTest.TestToFromObjectRoundTrip(o));

            default: throw new InvalidOperationException();
            }
        }
Exemple #7
0
        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);
            }
        }
Exemple #8
0
        public void TestConversions()
        {
            var fr = new RandomGenerator();

            for (var i = 0; i < 20000; ++i)
            {
                bool      isNum, isTruncated, isInteger;
                EInteger  eint;
                ERational enumber = RandomObjects.RandomERational(fr);
                if (!enumber.IsFinite)
                {
                    try {
                        enumber.ToByteChecked();
                        Assert.Fail("Should have failed");
                    } catch (OverflowException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    Assert.AreEqual(
                        EInteger.Zero,
                        EInteger.FromByte(enumber.ToByteUnchecked()));
                    try {
                        enumber.ToByteIfExact();
                        Assert.Fail("Should have failed");
                    } catch (OverflowException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    try {
                        enumber.ToInt16Checked();
                        Assert.Fail("Should have failed");
                    } catch (OverflowException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    Assert.AreEqual(
                        EInteger.Zero,
                        EInteger.FromInt16(enumber.ToInt16Unchecked()));
                    try {
                        enumber.ToInt16IfExact();
                        Assert.Fail("Should have failed");
                    } catch (OverflowException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    try {
                        enumber.ToInt32Checked();
                        Assert.Fail("Should have failed");
                    } catch (OverflowException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    Assert.AreEqual(
                        EInteger.Zero,
                        EInteger.FromInt32(enumber.ToInt32Unchecked()));
                    try {
                        enumber.ToInt32IfExact();
                        Assert.Fail("Should have failed");
                    } catch (OverflowException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    try {
                        enumber.ToInt64Checked();
                        Assert.Fail("Should have failed");
                    } catch (OverflowException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    Assert.AreEqual(
                        EInteger.Zero,
                        EInteger.FromInt64(enumber.ToInt64Unchecked()));
                    try {
                        enumber.ToInt64IfExact();
                        Assert.Fail("Should have failed");
                    } catch (OverflowException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    continue;
                }
                ERational enumberInteger = ERational.FromEInteger(enumber.ToEInteger());
                isInteger = enumberInteger.CompareTo(enumber) == 0;
                eint      = enumber.ToEInteger();
                isNum     = enumber.CompareTo(
                    ERational.FromString("0")) >= 0 && enumber.CompareTo(
                    ERational.FromString("255")) <= 0;
                isTruncated = enumber.ToEInteger().CompareTo(
                    EInteger.FromString("0")) >= 0 && enumber.ToEInteger().CompareTo(
                    EInteger.FromString("255")) <= 0;
                if (isNum)
                {
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromByte(enumber.ToByteChecked()));
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromByte(enumber.ToByteUnchecked()));
                    if (isInteger)
                    {
                        TestCommon.AssertEquals(
                            eint,
                            EInteger.FromByte(enumber.ToByteIfExact()));
                    }
                    else
                    {
                        try {
                            enumber.ToByteIfExact();
                            Assert.Fail("Should have failed");
                        } catch (ArithmeticException) {
                            new Object();
                        } catch (Exception ex) {
                            Assert.Fail(ex.ToString());
                            throw new InvalidOperationException(String.Empty, ex);
                        }
                    }
                }
                else if (isTruncated)
                {
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromByte(enumber.ToByteChecked()));
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromByte(enumber.ToByteUnchecked()));
                    try {
                        enumber.ToByteIfExact();
                        Assert.Fail("Should have failed");
                    } catch (ArithmeticException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                }
                else
                {
                    try {
                        enumber.ToByteChecked();
                        Assert.Fail("Should have failed");
                    } catch (OverflowException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    try {
                        enumber.ToByteUnchecked();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    if (isInteger)
                    {
                        try {
                            enumber.ToByteIfExact();
                            Assert.Fail("Should have failed");
                        } catch (OverflowException) {
                            new Object();
                        } catch (Exception ex) {
                            Assert.Fail(ex.ToString());
                            throw new InvalidOperationException(String.Empty, ex);
                        }
                    }
                    else
                    {
                        try {
                            enumber.ToByteIfExact();
                            Assert.Fail("Should have failed");
                        } catch (ArithmeticException) {
                            new Object();
                        } catch (Exception ex) {
                            Assert.Fail(ex.ToString());
                            throw new InvalidOperationException(String.Empty, ex);
                        }
                    }
                }
                isNum = enumber.CompareTo(
                    ERational.FromString("-32768")) >= 0 && enumber.CompareTo(
                    ERational.FromString("32767")) <= 0;
                isTruncated = enumber.ToEInteger().CompareTo(
                    EInteger.FromString("-32768")) >= 0 && enumber.ToEInteger().CompareTo(
                    EInteger.FromString("32767")) <= 0;
                if (isNum)
                {
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromInt16(enumber.ToInt16Checked()));
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromInt16(enumber.ToInt16Unchecked()));
                    if (isInteger)
                    {
                        TestCommon.AssertEquals(
                            eint,
                            EInteger.FromInt16(enumber.ToInt16IfExact()));
                    }
                    else
                    {
                        try {
                            enumber.ToInt16IfExact();
                            Assert.Fail("Should have failed");
                        } catch (ArithmeticException) {
                            new Object();
                        } catch (Exception ex) {
                            Assert.Fail(ex.ToString());
                            throw new InvalidOperationException(String.Empty, ex);
                        }
                    }
                }
                else if (isTruncated)
                {
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromInt16(enumber.ToInt16Checked()));
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromInt16(enumber.ToInt16Unchecked()));
                    try {
                        enumber.ToInt16IfExact();
                        Assert.Fail("Should have failed");
                    } catch (ArithmeticException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                }
                else
                {
                    try {
                        enumber.ToInt16Checked();
                        Assert.Fail("Should have failed");
                    } catch (OverflowException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    try {
                        enumber.ToInt16Unchecked();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    if (isInteger)
                    {
                        try {
                            enumber.ToInt16IfExact();
                            Assert.Fail("Should have failed");
                        } catch (OverflowException) {
                            new Object();
                        } catch (Exception ex) {
                            Assert.Fail(ex.ToString());
                            throw new InvalidOperationException(String.Empty, ex);
                        }
                    }
                    else
                    {
                        try {
                            enumber.ToInt16IfExact();
                            Assert.Fail("Should have failed");
                        } catch (ArithmeticException) {
                            new Object();
                        } catch (Exception ex) {
                            Assert.Fail(ex.ToString());
                            throw new InvalidOperationException(String.Empty, ex);
                        }
                    }
                }
                isNum = enumber.CompareTo(
                    ERational.FromString("-2147483648")) >= 0 && enumber.CompareTo(
                    ERational.FromString("2147483647")) <= 0;
                isTruncated = enumber.ToEInteger().CompareTo(
                    EInteger.FromString("-2147483648")) >= 0 &&
                              enumber.ToEInteger().CompareTo(
                    EInteger.FromString("2147483647")) <= 0;
                if (isNum)
                {
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromInt32(enumber.ToInt32Checked()));
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromInt32(enumber.ToInt32Unchecked()));
                    if (isInteger)
                    {
                        TestCommon.AssertEquals(
                            eint,
                            EInteger.FromInt32(enumber.ToInt32IfExact()));
                    }
                    else
                    {
                        try {
                            enumber.ToInt32IfExact();
                            Assert.Fail("Should have failed");
                        } catch (ArithmeticException) {
                            new Object();
                        } catch (Exception ex) {
                            Assert.Fail(ex.ToString());
                            throw new InvalidOperationException(String.Empty, ex);
                        }
                    }
                }
                else if (isTruncated)
                {
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromInt32(enumber.ToInt32Checked()));
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromInt32(enumber.ToInt32Unchecked()));
                    try {
                        enumber.ToInt32IfExact();
                        Assert.Fail("Should have failed");
                    } catch (ArithmeticException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                }
                else
                {
                    try {
                        enumber.ToInt32Checked();
                        Assert.Fail("Should have failed");
                    } catch (OverflowException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    try {
                        enumber.ToInt32Unchecked();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    if (isInteger)
                    {
                        try {
                            enumber.ToInt32IfExact();
                            Assert.Fail("Should have failed");
                        } catch (OverflowException) {
                            new Object();
                        } catch (Exception ex) {
                            Assert.Fail(ex.ToString());
                            throw new InvalidOperationException(String.Empty, ex);
                        }
                    }
                    else
                    {
                        try {
                            enumber.ToInt32IfExact();
                            Assert.Fail("Should have failed");
                        } catch (ArithmeticException) {
                            new Object();
                        } catch (Exception ex) {
                            Assert.Fail(ex.ToString());
                            throw new InvalidOperationException(String.Empty, ex);
                        }
                    }
                }
                isNum = enumber.CompareTo(
                    ERational.FromString("-9223372036854775808")) >= 0 && enumber.CompareTo(
                    ERational.FromString("9223372036854775807")) <= 0;
                isTruncated = enumber.ToEInteger().CompareTo(
                    EInteger.FromString("-9223372036854775808")) >= 0 &&
                              enumber.ToEInteger().CompareTo(
                    EInteger.FromString("9223372036854775807")) <= 0;
                if (isNum)
                {
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromInt64(enumber.ToInt64Checked()));
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromInt64(enumber.ToInt64Unchecked()));
                    if (isInteger)
                    {
                        TestCommon.AssertEquals(
                            eint,
                            EInteger.FromInt64(enumber.ToInt64IfExact()));
                    }
                    else
                    {
                        try {
                            enumber.ToInt64IfExact();
                            Assert.Fail("Should have failed");
                        } catch (ArithmeticException) {
                            new Object();
                        } catch (Exception ex) {
                            Assert.Fail(ex.ToString());
                            throw new InvalidOperationException(String.Empty, ex);
                        }
                    }
                }
                else if (isTruncated)
                {
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromInt64(enumber.ToInt64Checked()));
                    TestCommon.AssertEquals(
                        eint,
                        EInteger.FromInt64(enumber.ToInt64Unchecked()));
                    try {
                        enumber.ToInt64IfExact();
                        Assert.Fail("Should have failed");
                    } catch (ArithmeticException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                }
                else
                {
                    try {
                        enumber.ToInt64Checked();
                        Assert.Fail("Should have failed");
                    } catch (OverflowException) {
                        new Object();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    try {
                        enumber.ToInt64Unchecked();
                    } catch (Exception ex) {
                        Assert.Fail(ex.ToString());
                        throw new InvalidOperationException(String.Empty, ex);
                    }
                    if (isInteger)
                    {
                        try {
                            enumber.ToInt64IfExact();
                            Assert.Fail("Should have failed");
                        } catch (OverflowException) {
                            new Object();
                        } catch (Exception ex) {
                            Assert.Fail(ex.ToString());
                            throw new InvalidOperationException(String.Empty, ex);
                        }
                    }
                    else
                    {
                        try {
                            enumber.ToInt64IfExact();
                            Assert.Fail("Should have failed");
                        } catch (ArithmeticException) {
                            new Object();
                        } catch (Exception ex) {
                            Assert.Fail(ex.ToString());
                            throw new InvalidOperationException(String.Empty, ex);
                        }
                    }
                }
            }
        }