Permalink
Browse files

Four4Sample - NaN propagation for number + string eval

  • Loading branch information...
bobbymcr committed Mar 11, 2018
1 parent d64418b commit 5be66b947f930ccd2fb88f6d20b81d5b290f17c5
Showing with 15 additions and 7 deletions.
  1. +1 −0 projects/Four4Sample/Four4.Test/CalculatorTest.cs
  2. +14 −7 projects/Four4Sample/Four4/Number.cs
@@ -137,6 +137,7 @@ public void SquareRoot(string input, string result)
[InlineData(1, 2, "4 +", "9/2")]
[InlineData(3, 4, "4 -", "-13/4")]
[InlineData(0, 1, "!", "1")]
[InlineData(0, 0, "!", "NaN")]
public void EvalNumberAndString(int num, int denom, string input, string result)
{
TestEval(num, denom, input, result);
@@ -15,14 +15,21 @@ public struct Number
public Number(int num, int denom)
{
int gcd = Gcd(num, denom);
this.num = num / gcd;
this.denom = denom / gcd;
if (this.denom < 0)
if (denom == 0)
{
this = NaN;
}
else
{
this.num *= -1;
this.denom *= -1;
int gcd = Gcd(num, denom);
this.num = num / gcd;
this.denom = denom / gcd;
if (this.denom < 0)
{
this.num *= -1;
this.denom *= -1;
}
}
}

0 comments on commit 5be66b9

Please sign in to comment.