Permalink
Browse files

Four4Sample - Handle overflow of numerator in multiply

  • Loading branch information...
bobbymcr committed Mar 17, 2018
1 parent 96e84f6 commit 7229bdb9c215efba309a54b87fb3e9252c081b38
Showing with 17 additions and 2 deletions.
  1. +10 −0 projects/Four4Sample/Four4.Test/EvalTest.cs
  2. +7 −2 projects/Four4Sample/Four4/Number.cs
@@ -156,6 +156,16 @@ public void Exponent(string input, string result)
Test(input, result);
}
[Theory]
[InlineData("3 ! !", "720")]
[InlineData("3 ! ! 3 ^", "373248000")]
[InlineData("3 ! ! 3 ^ .3 /", "NaN")]
[InlineData("3 ! ! 3 ^ .3 / 3 ^", "NaN")]
public void ExpressionsWith3(string input, string result)
{
Test(input, result);
}
private static void Test(string input, string result)
{
Expression.Eval(input).ToString().Should().Be(result, "input was {0}", input);
@@ -61,9 +61,14 @@ public Number(int num, int denom)
public static Number operator *(Number left, Number right)
{
int n = left.num * right.num;
double nr = (double)left.num * right.num;
if (nr > int.MaxValue)
{
return NaN;
}
int d = left.denom * right.denom;
return new Number(n, d);
return new Number((int)nr, d);
}
public static Number operator /(Number left, Number right)

0 comments on commit 7229bdb

Please sign in to comment.