Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does this unit test fail when comparing two doubles?
    primarykey
    data
    text
    <p>I have the following code in vb.net that calculates the amount before tax was applied:</p> <pre><code>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 </code></pre> <p>Some scenarios I setup were:</p> <p>Rate = 107, Tax = 7%, Base = 100</p> <p>Rate = 325, Tax = 6.5%, Base = 305.16</p> <p>Rate = 215, Tax = 125%, Base = 95.55</p> <p>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:</p> <pre><code>[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); } } </code></pre> <p>As I said before, the first test passes, but the results of the other tests are:</p> <p>Second Test expected 305.1600000000000003d But was: 305.1643192488263d</p> <p>Third Test expected 95.54999999999997 But was: 95.55555555555555557d</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload