Wednesday, September 2, 2015

when one int gets divided by another in C# and there should not be a clean integer result...

...there is nonetheless. Watch out for this! You won't have this problem if you divide a double or a decimal by an int, but int by int division is crazy. The following test passes even though forty-two divided by thirteen really should give an irrational number closer to pi than three.

[TestMethod]
public void BadTest()
{
   int fortyTwo = 42;
   int thirteen = 13;
   Assert.AreEqual(fortyTwo/thirteen,3);
}

No comments:

Post a Comment