Need confirmation that Mortgage calculator checker is correct

Back to General discussions forum

qwerty     2023-11-11 05:48:47

Input data:

2600000 2 159

My calculations:

Month     P        P * (R/12)%      -M       new P
  1    $2600000      $4334       -$18628   $2585706
  2    $2585706      $4310       -$18628   $2571387
  3    $2571387      $4286       -$18628   $2557045
 ...
 12    $2441442      $4070       -$18628   $2426883
 ...
 24    $2265125      $3776       -$18628   $2250272
 ...  
 159     $18596        $31       -$18627         $0

My program's answer:

18628

Checker program's answer:

18629

Can anyone provide calculations which help to understand why the expected answer was 18629?

gardengnome     2023-11-11 09:03:28
User avatar

Hi, the answer should be indeed 18628 (18627.994227...) - calculated via two completely different means. Rodion, there might be a rounding issue somewhere in the checker.

Rodion (admin)     2023-11-11 11:52:50
User avatar

Hi Friends!

Truly, it is error. Not exactly about rounding - but I found (completely forgot this) the checking code, when prepares data, does the same work which user is supposed to do - i.e. after generating three random values, tries to find answer by binary search. This binary search starts with bounds a = p / l; b = p and continues while (b - a > 0.1).

Obviously the condition is not always enough given the requirement that result is rounded up - it stopped with

a = 18627.974 b = 18628.051
m = ceil((a + b)/2)

Now condition is simply narrowed to 0.01 (or even 0.001) and it seemingly fixes the situation... Or rather I understand there is now smaller chance of misfire. Sorry for this mistake - seemingly this rounding in the end is inherently slightly wrong.

gardengnome     2023-11-11 12:01:40
User avatar

Hi, to eliminate the error completely you could use while ceil(a) < b.

Rodion (admin)     2023-11-11 12:17:51
User avatar

Mathias, thanks for hint! This weird moment when I don't completely understand why it works, though intuitively it seems reasonable :)))

The suggested change is made.

gardengnome     2023-11-11 12:38:10
User avatar

With a <= b, while ceil(a) < b is equivalent here to while ceil(a) < ceil(b) or while not (ceil(a) == ceil(b)). This ensures that the binary search runs until both the lower bound a and the upper bound b round up to the same integer, and thus the results is determined. This avoids edge cases where a is just below an integer and b just above, and we can't be sure which way the result falls.

qwerty     2023-11-11 15:32:30

Understood now.

I did not use binary search in my solution. My search was just:

...
monthly_payment = 1
while simulate(loan, multiplier, monthly_payment, months) > 0:
    monthly_payment += 1
print(monthly_payment)

Worked fast enough.

For learning purposes I will try to solve this task with binary search now.

qwerty     2023-11-12 07:46:41

Finally solved this task using binary search. But this was not easy to get it right. So to conclude: always test a program with binary search carefully!

Please login and solve 5 problems to be able to post at forum