Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this unit test fail when comparing two doubles?

Tags:

c#

vb.net

nunit

I have the following code in vb.net that calculates the amount before tax was applied:

Public Shared Function CalculateRateBeforeTax(ByVal rate As Decimal, ByVal tax As Decimal) As Decimal
    Dim base As Decimal = rate / (1 + (tax / 100.0))
    Return Math.Round(base,2)
End Function

Some scenarios I setup were:

Rate = 107, Tax = 7%, Base = 100

Rate = 325, Tax = 6.5%, Base = 305.16

Rate = 215, Tax = 125%, Base = 95.55

I put the above scenarios into some unit tests using c# and using the nunit testing framework. The first scenario passes, but the other fails and I am not sure how I can get it to pass. Here is are my tests:

[TestFixture]
class TaxTests
{
    [Test]
    public void CalculateRateBeforeTax_ShouldReturn100_WhenRateIs107AndTaxIs7Percent()
    {
        decimal expected = 100.0m;
        decimal actual = TaxUtil.CalculateRateBeforeTax(107.0m, 7.0m);

        Assert.AreEqual(expected,actual);
    }

    [Test]
    public void CalculateRateBeforeTax_ShouldReturn305point16_WhenRateIs325AndTaxIs6point5Percent()
    {
        decimal expected = 305.16m;
        decimal actual = TaxUtil.CalculateRateBeforeTax(325.0m, 6.5m);

        Assert.AreEqual(expected, actual);
    }

    [Test]
    public void CalculateRateBeforeTax_ShouldReturn95point55_WhenRateIs215AndTaxIs125Percent()
    {
        decimal expected = 95.55m;
        decimal actual = TaxUtil.CalculateRateBeforeTax(215.0m, 125.0m);

        Assert.AreEqual(expected, actual);
    }

}

As I said before, the first test passes, but the results of the other tests are:

Second Test expected 305.1600000000000003d But was: 305.1643192488263d

Third Test expected 95.54999999999997 But was: 95.55555555555555557d

like image 548
Xaisoft Avatar asked Jan 21 '23 03:01

Xaisoft


2 Answers

Just take out a calculator and enter the following: 325 / (1 + (6.5 / 100.0))

The result is 305.164319...

Then you're asking if 305.164319... is equal to 305.16. The test obviously fails, they are not the same numbers.

Now if you're wondering why you have slightly different numbers than this, like 305.1600000000000003 instead of 305.16, this is because there is some loss of precision with Double type. You can use the Decimal type for more precision.

But the most important problem is that the value returned by CalculateRateBeforeTax is not truncated correctly to have precision up to the cent. You just have to truncate two decimals like this:

Dim rounded As Decimal = Math.Floor(base * 100) / 100

Now by changing Double type by Decimal type your Assert should work.

like image 187
Meta-Knight Avatar answered Jan 31 '23 19:01

Meta-Knight


Congratulations. Your Unit Tests have actually done what they are supposed to do and found a bug with the code you are testing.

You have rounding errors. Unfortunately this is being caused by the VB.NET code you are trying to Unit Test rather than the code in your actual tests.

You need to use a data type with more precision. I would suggest replacing your use of Double with Decimal.

like image 43
Justin Niessner Avatar answered Jan 31 '23 19:01

Justin Niessner