public void CompareToTest() { Rational target = new Rational(1, 3); Assert.AreEqual(target.CompareTo(new Rational(1, 2)), -1); Assert.AreEqual(target.CompareTo(new Rational(1, 3)), 0); Assert.AreEqual(target.CompareTo(new Rational(1, 4)), 1); }
static List <Rational> Egyptian(Rational r) { List <Rational> result = new List <Rational>(); if (r.CompareTo(1) >= 0) { if (r.Den == 1) { result.Add(r); result.Add(new Rational(0)); return(result); } result.Add(new Rational(r.Num / r.Den)); r = r.Sub(result[0]); } BigInteger modFunc(BigInteger m, BigInteger n) { return(((m % n) + n) % n); } while (r.Num != 1) { var q = (r.Den + r.Num - 1) / r.Num; result.Add(new Rational(1, q)); r = new Rational(modFunc(-r.Den, r.Num), r.Den * q); } result.Add(r); return(result); }
static void Main(string[] args) { int a = 2; int b = 7; int c = 5; int d = 10; Rational x = new Rational(a, b); Rational y = new Rational(c, d); Rational z = c; Console.WriteLine("x=" + x); Console.WriteLine("y=" + y); Console.WriteLine("z=" + z); Console.WriteLine("x is equals z?" + x.Equals(z)); Console.WriteLine("x is equals x?" + x.Equals(x)); Console.WriteLine("x compared to z?" + x.CompareTo(z)); Console.WriteLine("x+y=" + (x + y)); Console.WriteLine("y-x=" + (y - x)); Console.WriteLine("x*y=" + (x * y)); Console.WriteLine("x/y=" + (x / y)); Console.WriteLine("-z=" + -z); Console.ReadKey(); }
public void Rational_x_is_less_than_y(Rational x, Rational y) { Assert.True(x.CompareTo(y) < 0); Assert.True(x < y); Assert.True(x <= y); Assert.False(x > y); Assert.False(x >= y); }
public void If_not_equal_then_x_to_y_is_opposite_than_y_to_x(Rational x, Rational y) { Assert.Equal(x.CompareTo(y), y.CompareTo(x) * -1); Assert.True(x < y != y < x); Assert.True(x <= y != y <= x); Assert.True(x > y != y > x); Assert.True(x >= y != y >= x); }
public void If_x_lte_y_and_y_lte_z_then_x_lte_z(Rational x, Rational y, Rational z) { if (x <= y && y <= z) { Assert.True(x <= z); Assert.True(x.CompareTo(z) <= 0); } }
public void Rational_are_not_equal_if_different_values(Rational x, Rational y) { Assert.True(x != y); Assert.False(x == y); Assert.False(Equals(x, y)); Assert.False(x.Equals(y)); Assert.False(x.Equals((object)y)); Assert.NotEqual(0, x.CompareTo(y)); }
public void Rational_are_equivalent_if_represent_the_same_value(Rational x, Rational y) { Assert.True(Equals(x, y)); Assert.True(x == y); Assert.True(x >= y); Assert.True(x <= y); Assert.True(x.Equals(y)); Assert.True(x.Equals((object)y)); Assert.Equal(0, x.CompareTo(y)); Assert.Equal(x.GetHashCode(), y.GetHashCode()); }
public void If_both_are_lte_then_they_are_equal(Rational x, Rational y) { if (x <= y && y <= x) { Assert.True(x.Equals(y)); Assert.True(x.Equals((object)y)); Assert.True(x == y); Assert.False(x != y); Assert.Equal(0, x.CompareTo(y)); } }
public void Rational_is_equal_to_itself(Rational x) { var y = x; Assert.True(Equals(x, y)); Assert.True(x == y); Assert.True(x >= y); Assert.True(x <= y); Assert.True(x.Equals(y)); Assert.True(x.Equals((object)y)); Assert.Equal(0, x.CompareTo(y)); }
public void RationalNumber_CompareTo_Equals() { //Arrange var smaller = new Rational(1, 2); var bigger = new Rational(1, 2); int expectedResult = 0; //Act int result = smaller.CompareTo(bigger); //Assert Assert.AreEqual(expectedResult, result); }
public void Case1() { Rational rationalValue = Rational.Parse("3221123045552"); byte byteValue = 16; sbyte sbyteValue = -16; short shortValue = 1233; ushort ushortValue = 1233; int intValue = -12233; uint uintValue = 12233; long longValue = 12382222; ulong ulongValue = 1238222; Console.WriteLine("Comparing {0} with {1}: {2}", rationalValue, byteValue, rationalValue.CompareTo(byteValue)); Console.WriteLine("Comparing {0} with {1}: {2}", rationalValue, sbyteValue, rationalValue.CompareTo(sbyteValue)); Console.WriteLine("Comparing {0} with {1}: {2}", rationalValue, shortValue, rationalValue.CompareTo(shortValue)); Console.WriteLine("Comparing {0} with {1}: {2}", rationalValue, ushortValue, rationalValue.CompareTo(ushortValue)); Console.WriteLine("Comparing {0} with {1}: {2}", rationalValue, intValue, rationalValue.CompareTo(intValue)); Console.WriteLine("Comparing {0} with {1}: {2}", rationalValue, uintValue, rationalValue.CompareTo(uintValue)); Console.WriteLine("Comparing {0} with {1}: {2}", rationalValue, longValue, rationalValue.CompareTo(longValue)); Console.WriteLine("Comparing {0} with {1}: {2}", rationalValue, ulongValue, rationalValue.CompareTo(ulongValue)); // The example displays the following output: // Comparing 3221123045552 with 16: 1 // Comparing 3221123045552 with -16: 1 // Comparing 3221123045552 with 1233: 1 // Comparing 3221123045552 with 1233: 1 // Comparing 3221123045552 with -12233: 1 // Comparing 3221123045552 with 12233: 1 // Comparing 3221123045552 with 12382222: 1 // Comparing 3221123045552 with 1238222: 1 }
internal Range(Span span, Rational end1, Rational end2) : base(span) { Contract.Requires(end1.IsInteger && end2.IsInteger); if (end1.CompareTo(end2) <= 0) { Lower = end1; Upper = end2; } else { Lower = end2; Upper = end1; } cachedHashCode = GetDetailedNodeKindHash(); }
public void ComparisonsNegativeDenominator() { // arrange var a = new Rational(3); var b = new Rational(-4, -1); // assert Assert.Equal(-1, a.CompareTo(b)); Assert.False(a > b); Assert.False(a >= b); Assert.True(a < b); Assert.True(a <= b); Assert.Equal(+1, b.CompareTo(a)); Assert.True(b > a); Assert.True(b >= a); Assert.False(b < a); Assert.False(b <= a); }
public void Case2() { object[] values = { Math.Pow(Int64.MaxValue, 10), null, 12.534, Int64.MaxValue, Rational.One }; Rational number = UInt64.MaxValue; foreach (object value in values) { try { Console.WriteLine("Comparing {0} with '{1}': {2}", number, value, number.CompareTo(value)); } catch (ArgumentException) { Console.WriteLine("Unable to compare the {0} value {1} with a Rational.", value.GetType().Name, value); } } // The example displays the following output: // Comparing 18446744073709551615 with '4.4555084156466750133735972424E+189': -1 // Comparing 18446744073709551615 with '': 1 // Unable to compare the Double value 12.534 with a Rational. // Unable to compare the Int64 value 9223372036854775807 with a Rational. // Comparing 18446744073709551615 with '1': 1 //TODO:指数表記で出力できる例に変更したい }
public void ComparisonsNaNCompareTo(Rational a, Rational b, int cmp) => Assert.Equal(cmp, a.CompareTo(b));
public void At_least_one_rational_must_be_lt_or_eq_the_other(Rational x, Rational y) { Assert.True(x.CompareTo(y) <= 0 || y.CompareTo(x) <= 0); Assert.True(x <= y || y <= x); Assert.True(x >= y || y >= x); }
public int CompareTo(ComplexPart other) { return(_value.CompareTo(other._value)); }
public static BigDecimal MultiplyRound(BigDecimal x, Rational f) { if (f.CompareTo(BigInteger.Zero) == 0) return BigDecimal.Zero; /* Convert the rational value with two digits of extra precision */ var mc = new MathContext(2 + x.Precision); BigDecimal fbd = f.ToBigDecimal(mc); /* and the precision of the product is then dominated by the precision in x */ return MultiplyRound(x, fbd); }
public static BigDecimal Log(Rational r, MathContext mc) { /* the value is undefined if x is negative. */ if (r.CompareTo(Rational.Zero) <= 0) throw new ArithmeticException("Cannot take log of negative " + r); if (r.CompareTo(Rational.One) == 0) return BigDecimal.Zero; /* log(r+epsr) = log(r)+epsr/r. Convert the precision to an absolute error in the result. * eps contains the required absolute error of the result, epsr/r. */ double eps = PrecisionToError(System.Math.Log(r.ToDouble()), mc.Precision); /* Convert this further into a requirement of the relative precision in r, given that * epsr/r is also the relative precision of r. Add one safety digit. */ var mcloc = new MathContext(1 + ErrorToPrecision(eps)); BigDecimal resul = Log(r.ToBigDecimal(mcloc)); return resul.Round(mc); }
public static BigDecimal PowRound(BigDecimal x, Rational q) { /** Special cases: x^1=x and x^0 = 1 */ if (q.CompareTo(BigInteger.One) == 0) return x; if (q.Sign == 0) return BigDecimal.One; if (q.IsInteger) { /* We are sure that the denominator is positive here, because normalize() has been * called during constrution etc. */ return PowRound(x, q.Numerator); } /* Refuse to operate on the general negative basis. The integer q have already been handled above. */ if (x.CompareTo(BigDecimal.Zero) < 0) throw new ArithmeticException("Cannot power negative " + x); if (q.IsIntegerFraction) { /* Newton method with first estimate in double precision. * The disadvantage of this first line here is that the result must fit in the * standard range of double precision numbers exponents. */ double estim = System.Math.Pow(x.ToDouble(), q.ToDouble()); var res = new BigDecimal(estim); /* The error in x^q is q*x^(q-1)*Delta(x). * The relative error is q*Delta(x)/x, q times the relative error of x. */ var reserr = new BigDecimal(0.5*q.Abs().ToDouble() *x.Ulp().Divide(x.Abs(), MathContext.Decimal64).ToDouble()); /* The main point in branching the cases above is that this conversion * will succeed for numerator and denominator of q. */ int qa = q.Numerator.ToInt32(); int qb = q.Denominator.ToInt32(); /* Newton iterations. */ BigDecimal xpowa = PowRound(x, qa); for (;;) { /* numerator and denominator of the Newton term. The major * disadvantage of this implementation is that the updates of the powers * of the new estimate are done in full precision calling BigDecimal.pow(), * which becomes slow if the denominator of q is large. */ BigDecimal nu = res.Pow(qb).Subtract(xpowa); BigDecimal de = MultiplyRound(res.Pow(qb - 1), q.Denominator); /* estimated correction */ BigDecimal eps = nu.Divide(de, MathContext.Decimal64); BigDecimal err = res.Multiply(reserr, MathContext.Decimal64); int precDiv = 2 + ErrorToPrecision(eps, err); if (precDiv <= 0) { /* The case when the precision is already reached and any precision * will do. */ eps = nu.Divide(de, MathContext.Decimal32); } else { eps = nu.Divide(de, new MathContext(precDiv)); } res = SubtractRound(res, eps); /* reached final precision if the relative error fell below reserr, * |eps/res| < reserr */ if (eps.Divide(res, MathContext.Decimal64).Abs().CompareTo(reserr) < 0) { /* delete the bits of extra precision kept in this * working copy. */ return res.Round(new MathContext(ErrorToPrecision(reserr.ToDouble()))); } } } /* The error in x^q is q*x^(q-1)*Delta(x) + Delta(q)*x^q*log(x). * The relative error is q/x*Delta(x) + Delta(q)*log(x). Convert q to a floating point * number such that its relative error becomes negligible: Delta(q)/q << Delta(x)/x/log(x) . */ int precq = 3 + ErrorToPrecision((x.Ulp().Divide(x, MathContext.Decimal64)).ToDouble() /System.Math.Log(x.ToDouble())); /* Perform the actual calculation as exponentiation of two floating point numbers. */ return Pow(x, q.ToBigDecimal(new MathContext(precq))); }
public void RationalComparisonTest() { Rational rational = new Rational(1, 2); Rational equal = new Rational(2, 4); Rational less = new Rational(1, 3); Rational greater = new Rational(2, 3); // equality with rational rational.CompareTo(rational).ShouldBe(0); rational.CompareTo(equal).ShouldBe(0); rational.CompareTo(less).ShouldBe(1); rational.CompareTo(greater).ShouldBe(-1); (rational < rational).ShouldBeFalse(); (rational > rational).ShouldBeFalse(); (rational < less).ShouldBeFalse(); (rational > less).ShouldBeTrue(); (rational < greater).ShouldBeTrue(); (rational > greater).ShouldBeFalse(); (rational <= rational).ShouldBeTrue(); (rational >= rational).ShouldBeTrue(); (rational <= less).ShouldBeFalse(); (rational >= less).ShouldBeTrue(); (rational <= greater).ShouldBeTrue(); (rational >= greater).ShouldBeFalse(); // equality with integer (rational <= 1).ShouldBeTrue(); (rational >= 0).ShouldBeTrue(); (rational <= 0).ShouldBeFalse(); (rational >= 1).ShouldBeFalse(); (rational < 1).ShouldBeTrue(); (rational > 0).ShouldBeTrue(); (rational < 0).ShouldBeFalse(); (rational > 1).ShouldBeFalse(); (0 <= rational).ShouldBeTrue(); (1 >= rational).ShouldBeTrue(); (1 <= rational).ShouldBeFalse(); (0 >= rational).ShouldBeFalse(); (0 < rational).ShouldBeTrue(); (1 > rational).ShouldBeTrue(); (1 < rational).ShouldBeFalse(); (0 > rational).ShouldBeFalse(); rational.CompareTo(0).ShouldBe(1); rational.CompareTo(1).ShouldBe(-1); rational.CompareTo(Int64.MinValue).ShouldBe(1); rational.CompareTo(Int64.MaxValue).ShouldBe(-1); Rational.Zero.CompareTo(0).ShouldBe(0); Rational.MinValue.CompareTo(Int64.MinValue).ShouldBe(0); Rational.MaxValue.CompareTo(Int64.MaxValue).ShouldBe(0); new Rational(9223372036854775801, Int64.MaxValue - 1).CompareTo(new Rational(9223372036854775797, Int64.MaxValue)).ShouldBe(1); new Rational(9223372036854775797, Int64.MaxValue - 1).CompareTo(new Rational(9223372036854775801, Int64.MaxValue)).ShouldBe(-1); // equality with object rational.CompareTo((Object)equal).ShouldBe(0); rational.CompareTo((Object)less).ShouldBe(1); rational.CompareTo((Object)greater).ShouldBe(-1); // extrema rational.CompareTo(Rational.PositiveInfinity).ShouldBe(-1); rational.CompareTo(Rational.NegativeInfinity).ShouldBe(1); rational.CompareTo(Rational.NaN).ShouldBe(1); rational.CompareTo(null).ShouldBe(1); Rational.PositiveInfinity.CompareTo(Rational.PositiveInfinity).ShouldBe(0); Rational.NegativeInfinity.CompareTo(Rational.NegativeInfinity).ShouldBe(0); Rational.NaN.CompareTo(rational).ShouldBe(-1); Rational.PositiveInfinity.CompareTo(Rational.NaN).ShouldBe(1); Rational.NegativeInfinity.CompareTo(Rational.NaN).ShouldBe(1); Rational.PositiveInfinity.CompareTo(new Rational(1, 1)).ShouldBe(1); Rational.NegativeInfinity.CompareTo(new Rational(1, 1)).ShouldBe(-1); Rational.PositiveInfinity.CompareTo(Rational.PositiveInfinity).ShouldBe(0); Rational.NegativeInfinity.CompareTo(Rational.NegativeInfinity).ShouldBe(0); Rational.NaN.CompareTo(Rational.NaN).ShouldBe(0); Rational.NaN.CompareTo(Rational.PositiveInfinity).ShouldBe(-1); Rational.NaN.CompareTo(Rational.NegativeInfinity).ShouldBe(-1); Rational.NaN.CompareTo(Rational.NaN).ShouldBe(0); new Rational(Int64.MaxValue - 1, 1).CompareTo(Rational.MaxValue).ShouldBe(-1); new Rational(Int64.MinValue + 1, 1).CompareTo(Rational.MinValue).ShouldBe(1); new Rational(Int64.MaxValue / 2, 1).CompareTo(Rational.MaxValue).ShouldBe(-1); new Rational(Int64.MinValue / 2, 1).CompareTo(Rational.MinValue).ShouldBe(1); Rational.MaxValue.CompareTo(new Rational(Int64.MaxValue - 1, 1)).ShouldBe(1); Rational.MinValue.CompareTo(new Rational(Int64.MinValue + 1, 1)).ShouldBe(-1); Rational.MaxValue.CompareTo(new Rational(Int64.MaxValue / 2, 1)).ShouldBe(1); Rational.MinValue.CompareTo(new Rational(Int64.MinValue / 2, 1)).ShouldBe(-1); // exceptions Should.Throw <ArgumentException>(() => rational.CompareTo(new Object())); }