Fix: test case for interest_is_interesting_test.cpp

I opened a pull request (PR) here regarding a bug related to the C++ track that was promptly closed by the github-actions bot. It turns out that, because the bug is related to a test case, then I will need to open a discussion here first before the PR can be reviewed.

I have pasted the body of the PR below, since it explains the basic premise. I would appreciate it if someone could give me some feedback, and possibly re-open the PR if it looks OK.


The following test case contains an incorrect want value.

Currently, the test case has the following parameters:

  • \texttt{balance} = 2345.67
  • \texttt{target_balance} = 12345.6789
  • \texttt{want} = 85

However, the value of want is incorrect. Since \texttt{balance} = 2345.67, then, as given in the instructions, the interest rate is \% 1.621. If we assume that \texttt{want} = 85 as given above, then the final balance after \texttt{want} years is

\begin{align} \texttt{balance} \times \left(1 + \frac{\verb|interest_rate|}{100}\right)^\texttt{want} &= 2345.67 \times \left(1 + \frac{1.621}{100}\right)^{85} \\ &\approx \$ 9201.558 \end{align}

Similarly, the final balance after \texttt{want} - 1 years is approximately \$ 9054.78. If the want value was correct, then the target balance would have been between the final balance after want - 1 years and the final balance after want years. However, this is not the case.

This PR fixes the want value by setting it to 104 years. We can see that this is indeed correct since

\begin{align} 2345.67 \times \left(1 + \frac{1.621}{100}\right)^{103} &\approx \$ 12,290.334 \\ &< \verb|target_balance| = 12345.6789 \\ &< 2345.67 \times \left(1 + \frac{1.621}{100}\right)^{104} \approx \$ 12,489.561 \end{align}

Hey there,

From first glance, that seems correct. Apart from the test case, that would also touch the exemplar solution in the .meta directory. There are a ton of other tracks that just take those test cases and reuse them, so your point might be valid for other tracks.

(Full disclosure: I implemented that exercise in the first place and wrote the test cases, e.g. modified them from the source code of some other track. Probably go)

Thank you for sharing the exemplar solution, Christian, and thanks for re-opening my PR. I have a follow-up question for you: given that the PR is now re-opened, are there any changes that you would suggest I add to make it merge-able?

By the way, the exemplar solution is actually quite different from my solution, which was

#include <cmath>

// years_until_desired_balance calculates the minimum number of years required
// to reach the desired balance.
int years_until_desired_balance(double balance, double target_balance) {
    // TODO: Implement the years_until_desired_balance function
    return int(ceil(log(target_balance / balance) / log(1 + interest_rate(balance) / 100)));
}

Of course, my solution isn’t really appropriate for learning about for loops, because it uses none, and it includes cmath, which may not be appropriate, but I wanted to mention it anyway (FYI, I obtained this solution by re-arranging the following equation to solve for x and then applying ceil() followed by int():

\begin{align} \verb|balance| \times \left(1 + \frac{\verb|interest_rate|}{100}\right)^x &= \verb|target_balance| \\ \implies x &= \frac{\log\left(\frac{\verb|target_balance|}{\verb|balance|}\right)}{\log\left(1 + \frac{\verb|interest_rate|}{100}\right)} \\ \end{align}

).

Are you able to transform your solution into the concepts, that are meant to be taught?

I also wonder, where the exemplar goes off the rails and how your result is different. I did not have time for a deep-dive yet.

Are you able to transform your solution into the concepts, that are meant to be taught?

Yes. After seeing exemplar solution here, it became clear that the exercise was asking me to make use of the previous functions that I had written.

However, it would have been helpful if there was a note in the instructions explaining that I should be making use of the previous functions I had written, as I hadn’t immediately realized this.