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:
Ra开发者_如何学Pythonte = 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
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.
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
.
You cannot guarantee that floating point numbers are the same as each other, even when they appear so.
It depends on a number of factors such as processor, architecture, etc. As Justin has said, use decimal if precision is required.
Have a look at Jon Skeets excellent blog post: http://csharpindepth.com/Articles/General/FloatingPoint.aspx
While the other posters are right stating that you should use Decimal
instead of Double
that isn't the cause of your observed problem.
The observed problem is caused by logical mistakes in your rounding code. You need to look up how to correctly round such values. This is a question of laws, and not math.
Another strange thing is that your CalculateRateBeforeTax
rounds its return value to an integral value, but the values you posted seem to be calculated without that rounding.
精彩评论