public void IntegerFractionTest()
		{
			var f1 = new IntegerFraction(1, 2);
			var f2 = new IntegerFraction(3, 5);
			var f3 = new IntegerFraction(4, 8);
			var f4 = new IntegerFraction(4, 2);
			var f5 = new IntegerFraction(-2, 3);
			var f6 = new IntegerFraction(-1, 6);

			Assert.AreEqual("1/2", f1.ToString());
			Assert.AreEqual("3/5", f2.ToString());
			Assert.AreEqual("1/2", f3.ToString());
			Assert.AreEqual("2", f4.ToString());
			Assert.AreEqual("⁻2/3", f5.ToString());

			Assert.AreEqual("11/10", (f1 + f2).ToString());
			Assert.AreEqual("1", (f1 + f3).ToString());
			Assert.AreEqual("5/2", (f1 + f4).ToString());
			Assert.AreEqual("⁻1/6", (f1 + f5).ToString());

			Assert.AreEqual("11/10", (f2 + f3).ToString());
			Assert.AreEqual("13/5", (f2 + f4).ToString());
			Assert.AreEqual("⁻1/15", (f2 + f5).ToString());

			Assert.AreEqual("⁻1/10", (f1 - f2).ToString());
			Assert.AreEqual("0", (f1 - f3).ToString());
			Assert.AreEqual("⁻3/2", (f1 - f4).ToString());
			Assert.AreEqual("7/6", (f1 - f5).ToString());

			Assert.AreEqual("3/10", (f1 * f2).ToString());
			Assert.AreEqual("1/4", (f1 * f3).ToString());
			Assert.AreEqual("1", (f1 * f4).ToString());
			Assert.AreEqual("⁻1/3", (f1 * f5).ToString());
			Assert.AreEqual("1/9", (f5 * f6).ToString());

			Assert.AreEqual("5/6", (f1 / f2).ToString());
			Assert.AreEqual("1", (f1 / f1).ToString());
			Assert.AreEqual("1", (f1 / f3).ToString());
			Assert.AreEqual("1/4", (f1 / f4).ToString());
			Assert.AreEqual("⁻3/4", (f1 / f5).ToString());
		}
Exemple #2
0
		internal static bool IsIntegerFraction(Evaluatable evaluatable, out IntegerFraction fraction)
		{
			var component = Componentizer.ToComponent(evaluatable);
			if (component is IntegerFraction)
			{
				fraction = (IntegerFraction)component;
				return true;
			}

			var factor = Factorizer.ToFactor(evaluatable);
			if (factor is NumericFactor)
			{
				var number = ((NumericFactor)factor).Number;
				if (number is Integer)
				{
					fraction = new IntegerFraction((Integer)number);
					return true;
				}
			}

			fraction = null;
			return false;
		}
		private static Expression Evaluate(IntegerFraction leftFraction, IntegerFraction rightFraction, bool leftIsNumerator, bool rightIsNumerator)
		{
			if (leftIsNumerator && rightIsNumerator)
			{
				return Expressionizer.ToExpression(leftFraction * rightFraction);
			}

			if (leftIsNumerator)
			{
				return NumberToExpression(leftFraction / rightFraction);
			}

			if (rightIsNumerator)
			{
				return NumberToExpression(rightFraction / leftFraction);
			}

			return NumberToExpression(leftFraction.Inverse * rightFraction.Inverse);
		}