Skip to content

Commit

Permalink
Four4Sample - NaN propagation for number + string eval
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbymcr committed Mar 11, 2018
1 parent d64418b commit 5be66b9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions projects/Four4Sample/Four4.Test/CalculatorTest.cs
Expand Up @@ -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);
Expand Down
21 changes: 14 additions & 7 deletions projects/Four4Sample/Four4/Number.cs
Expand Up @@ -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;
}
}
}

Expand Down

0 comments on commit 5be66b9

Please sign in to comment.