Example #1
0
        public void Abs(int n, int d)
        {
            var actual   = BigRational.Abs(new BigRational(n, d));
            var expected = new BigRational(Math.Abs(n), Math.Abs(d));

            AssertEqual(expected, actual);
        }
    public void E()
    {
        var expected = Math.E;
        var actual   = BigRational.E;

        Assert.True(BigRational.Abs(expected - actual) < new BigRational(1, 100000000000000));
    }
    public void SqrtIrrational()
    {
        var expected = BigRational.SqrtAsRationals(2).Skip(100).First();
        var actual   = BigRational.Sqrt(new(2), 13);

        Assert.True(BigRational.Abs(expected - actual) < new BigRational(1, 1000000000));
    }
Example #4
0
 public static object Abs(object a)
 {
     if (a is int)
     {
         return(Math.Abs((int)a));
     }
     else if (a is long)
     {
         return(Math.Abs((long)a));
     }
     else if (a is decimal)
     {
         return(Math.Abs((decimal)a));
     }
     else if (a is double)
     {
         return(Math.Abs((double)a));
     }
     else if (a is BigInteger)
     {
         return(BigInteger.Abs((BigInteger)a));
     }
     else if (a is BigRational)
     {
         return(BigRational.Abs((BigRational)a));
     }
     else
     {
         return(Complex.Abs(AsComplex(a)));
     }
 }
Example #5
0
        public bool HasValueNear(BigRational value, BigRational?epsilon = null)
        {
            var eps = epsilon ?? new BigRational(1, 1000000);
            var y   = BigRational.Abs(Constraint.EvaluateAt(value));

            return(y < eps);
        }
Example #6
0
        private static string GetMixedFormat(BigRational value, char separator)
        {
            StringBuilder sb = new();

            BigInteger  whole        = value.GetWholePart();
            BigRational fractionPart = value.GetFractionPart();

            if (!whole.IsZero)
            {
                sb.Append(whole.ToString("R", CultureInfo.CurrentCulture));
                if (separator != DisplaySeparator)
                {
                    sb.Append(separator);
                }
                else
                {
                    // When the DisplaySeparator is being used,
                    // then the whole and fractional parts need
                    // to be separated by whitespace.
                    sb.Append(' ');
                }

                // If the rational number was negative, then the
                // whole part will have been rendered as a negative
                // number, so we need to ensure that the fractional
                // part doesn't render as a negative value too.
                fractionPart = BigRational.Abs(fractionPart);
            }

            AppendCommonFormat(sb, fractionPart, separator);

            return(sb.ToString());
        }
Example #7
0
        public void BigRational_Abs_ReturnsAbsoluteValue()
        {
            var value1 = new BigRational((BigInteger)10);

            Assert.Equal("10/1", value1.Abs().ToString("B", null));

            var value2 = new BigRational(-(BigInteger)10);

            Assert.Equal("10/1", value2.Abs().ToString("B", null));

            var value3 = BigRational.Zero;

            Assert.Equal("0/1", value3.Abs().ToString("B", null));

            var value4 = BigRational.Create(VeryBigNumber);

            Assert.Equal($"{VeryBigNumber}/1", value4.Abs().ToString("B", null));

            var value5 = BigRational.Create($"-{VeryBigNumber}");

            Assert.Equal($"{VeryBigNumber}/1", value4.Abs().ToString("B", null));
        }
Example #8
0
        private Value decimalToFraction(BigRational Decimal)
        {
            BigInteger  fractionNumerator   = int.MaxValue;
            BigInteger  fractionDenominator = 1;
            BigRational accuracyFactor      = .0000001;
            int         decimalSign;
            BigRational Z;
            BigInteger  previousDenominator;
            BigInteger  scratchValue;

            if (Decimal < 0)
            {
                decimalSign = -1;
            }
            else
            {
                decimalSign = 1;
            }
            Decimal = BigRational.Abs(Decimal);
            if (Decimal == (BigInteger)Decimal)
            {
                fractionNumerator   = (int)Decimal * decimalSign;
                fractionDenominator = 1;
                return(new Value(fractionDenominator, fractionNumerator, NumberType.rational));
            }
            Z = Decimal;
            previousDenominator = 0;
            while (!(Z == (BigInteger)Z) && (BigRational.Abs((Decimal - ((BigRational)fractionNumerator / fractionDenominator))) > accuracyFactor))
            {
                Z                   = 1 / (Z - (BigInteger)Z);
                scratchValue        = fractionDenominator;
                fractionDenominator = fractionDenominator * (BigInteger)Z + previousDenominator;
                previousDenominator = scratchValue;
                fractionNumerator   = (BigInteger)(Decimal * fractionDenominator + .5);
            }
            fractionNumerator = decimalSign * fractionNumerator;
            return(new Value(fractionNumerator, fractionDenominator, NumberType.rational));
        }
Example #9
0
        public static BigRational IntegralSinX2Row(int a)
        {
            BigRational sum = 0;
            BigRational previous;
            BigRational factorial = 1;

            var minus = 1;
            var n     = 0;

            do
            {
                var member = minus
                             * BigRational.Pow(a, 4 * n + 3)
                             / (factorial * (4 * n + 3));
                n++;
                minus     *= -1;
                factorial *= (2 * n + 1) * (2 * n);
                previous   = sum;
                sum       += member;
            } while (BigRational.Abs(sum - previous) > Error);

            return(sum);
        }
        public void big_rational_static_methods()
        {
            Assert.True(0.5 == BigRational.Abs(0.5d));
            Assert.True(0.5 == BigRational.Abs(-0.5d));
            Assert.False(0.5 == BigRational.Abs(-1));
            Assert.False(0.5 == BigRational.Abs(1));

            Assert.True(-0.5 == BigRational.Negate(0.5d));
            Assert.True(0.5 == BigRational.Negate(-0.5d));
            Assert.False(0.5 == BigRational.Negate(-1));
            Assert.False(-0.5 == BigRational.Negate(1));

            //Doesn't work great... testing with equals doubles fails
            Assert.True(new BigRational(1, 2) == BigRational.Invert(new BigRational(2, 1)));
            Assert.True(new BigRational(2, 1) == BigRational.Invert(new BigRational(1, 2)));
            Assert.True(new BigRational(-1, 2) == BigRational.Invert(new BigRational(-2, 1)));
            Assert.True(new BigRational(-2, 1) == BigRational.Invert(new BigRational(-1, 2)));

            Assert.True(new BigRational(BigInteger.One) + new BigRational(BigInteger.One) == 2);
            Assert.True(BigRational.Add(new BigRational(BigInteger.One), new BigRational(BigInteger.One)) == 2);

            //Not working for me
            //BigRational big1 = new BigRational(1.0);
            //big1++;
            //Assert.AreEqual(new BigRational(2, 1), big1);

            //Not working for me
            //BigRational big2 = new BigRational(1.0);
            //big2--;
            //Assert.True(0 == big2);

            Assert.True(new BigRational(BigInteger.One) - new BigRational(BigInteger.One) == 0);
            Assert.True(BigRational.Subtract(new BigRational(BigInteger.One), new BigRational(BigInteger.One)) == 0);

            BigRational big3 = new BigRational(1.0);

            Assert.True(-1.0 == -big3);

            BigRational big4 = new BigRational(1.0);

            Assert.True(1.0 == +big3);

            Assert.True(new BigRational(BigInteger.One) * new BigRational(BigInteger.One) == 1);
            Assert.True(BigRational.Multiply(new BigRational(BigInteger.One), new BigRational(BigInteger.One)) == 1);
            Assert.True(new BigRational(BigInteger.One) * new BigRational(2.0) == 2.0);
            Assert.True(BigRational.Multiply(new BigRational(BigInteger.One), new BigRational(2.0)) == 2.0);

            Assert.True(new BigRational(BigInteger.One) / new BigRational(BigInteger.One) == 1);
            Assert.True(BigRational.Divide(new BigRational(BigInteger.One), new BigRational(BigInteger.One)) == 1);
            Assert.AreEqual(new BigRational(1, 2), new BigRational(new BigInteger(1)) / new BigRational(new BigInteger(2)));                    //Again, mixing consructors doesn't work well.
            Assert.AreEqual(new BigRational(1, 2), BigRational.Divide(new BigRational(new BigInteger(1)), new BigRational(new BigInteger(2)))); //Again, mixing consructors doesn't work well. Testing with equals double 0.5 fails

            //Assert.True(new BigRational(BigInteger.One) < new BigRational(2.0)); //Don't mix the constructors for comparison!!!
            Assert.True(new BigRational(1.0) < new BigRational(2.0));
            Assert.False(new BigRational(2.0) < new BigRational(1.0));
            Assert.False(new BigRational(1.0) < new BigRational(1.0));
            Assert.True(new BigRational(1.0) <= new BigRational(2.0));
            Assert.True(new BigRational(1.0) <= new BigRational(1.0));
            Assert.False(new BigRational(2.0) <= new BigRational(1.0));
            Assert.True(new BigRational(1.0) == new BigRational(1.0));
            Assert.True(new BigRational(1.0) != new BigRational(2.0));

            Assert.False(new BigRational(1.0) > new BigRational(2.0));
            Assert.True(new BigRational(2.0) > new BigRational(1.0));
            Assert.False(new BigRational(BigInteger.One) > new BigRational(BigInteger.One));
            Assert.False(new BigRational(1.0) >= new BigRational(2.0));
            Assert.True(new BigRational(BigInteger.One) >= new BigRational(BigInteger.One));
            Assert.True(new BigRational(2.0) >= new BigRational(1.0));

            //Modulus doesn't seem to work well
            //Assert.AreEqual(BigRational.Zero, new BigRational(2.0) % new BigRational(1.0));
            //Assert.AreEqual(BigRational.One, new BigRational(3.0) % new BigRational(2.0));
            //Assert.True(new BigRational(1.5) % new BigRational(1.0) == new BigRational(0.5));
            //Assert.True(new BigRational(1.0) % new BigRational(2.0) == 0);
        }