Interest is interesting with Clojure fail tests

I cannot seem to pass 4 tests despite of using the functions bigdec and int in my calculations. I have already placed such functions and included also double in some parts, which means, I tried different combinations. Some make less test to pass. My best try has given me 4 fail tests only:

  • annual-balance-update-small-negative-balance-test
  • annual-balance-update-large-negative-balance-test
  • amount-to-donate-large-positive-balance-test
  • amount-to-donate-huge-positive-balance-test

Of course the reason are minimal differences in decimals. For example, for the first test the expected value is -0.12695199M and I got -0.11904801M but every other test is passing.

Some ideas or hints about it?

Show your code.

I recall getting hit by the silly precision the tests demand, but I eventually figured it out.

If I recall, one of the keys: if you have any numeric literals, make sure you use the “M” suffix so they are bigdec’s.

I was also trying the “M” but so far with no succes. Here is my code:

(defn interest-rate
  "Returns the interest rate based on the specified balance."
    (cond (< balance 0) -3.213
          (< balance 1000) 0.5
          (< balance 5000) 1.621
          (>= balance 5000) 2.475))

(defn annual-balance-update
  "Returns the annual balance update, taking into account the interest rate."
  (+ balance (* balance (bigdec (/ (interest-rate balance) 100)))))

(defn amount-to-donate
  "Returns how much money to donate based on the balance and the tax-free percentage."
  [balance tax-free-percentage]
  (if (> balance 0)
      (int (* 2 (* (bigdec (/ tax-free-percentage 100))
                   (annual-balance-update balance))))

Two things:

  1. For annual-balance-update, what happens if the balance is negative? Should the update increase or decrease the balance?
  2. For amount-to-donate, you are basing the donation on the updated balance, not the plain balance.

Thanks, 1. got me on the right track!

Although I have to say the problem instructions are a bit weird, saying that the interest is negative for a negative balance. Banks usually don’t have negative interest rates on their loans. :)