Skip to content

Instantly share code, notes, and snippets.

@tklam
Created April 25, 2017 14:28
Show Gist options
  • Save tklam/bc48389ae5dddc569e4c87889335259a to your computer and use it in GitHub Desktop.
Save tklam/bc48389ae5dddc569e4c87889335259a to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <iostream>
using namespace std;
enum MonthDay {
JANUARY = 31,
FEBRUARY = 28,
MARCH = 31,
APRIL = 30,
MAY = 31,
JUNE = 30,
JULY = 31,
AUGUST = 31,
SEPTEMBER = 30,
OCTOBER = 31,
NOVEMBER = 30,
DECEMBER = 31
};
static MonthDay monthDay[12] = {
JANUARY,
FEBRUARY,
MARCH,
APRIL,
MAY,
JUNE,
JULY,
AUGUST,
SEPTEMBER,
OCTOBER,
NOVEMBER,
DECEMBER
};
void calDebt(double paidAmount, double borrowAmount, int year,
double annualInterestRate, double cashRebateRate,
double mortageLinkAmount, double mortageLinkRate) {
double rebate = borrowAmount * cashRebateRate / 100;
double curBorrowAmount = borrowAmount;
double totalMonths = year * 12;
double effectiveInterestPerMonth = annualInterestRate / 12 / 100;
double monthlyPayAmount = borrowAmount
* effectiveInterestPerMonth
* pow((1+effectiveInterestPerMonth), totalMonths)
/ (pow((1+effectiveInterestPerMonth), totalMonths) - 1);
int monthDayIndex = 11;
double totalDebt = 0;
double totalMortageLinkInterest = 0;
double totalInterest = 0 ;
for (int i=0; i<totalMonths; ++i) {
double curMortageLinkInterest = min(mortageLinkAmount, curBorrowAmount/2) * mortageLinkRate / 100
/ 365
* monthDay[monthDayIndex];
totalMortageLinkInterest += curMortageLinkInterest;
double curMonthlyInterest = curBorrowAmount * annualInterestRate / 100
/ 365
* monthDay[monthDayIndex];
totalInterest += curMonthlyInterest;
double curMonthlyPrinciple = monthlyPayAmount - curMonthlyInterest;
if (curMonthlyPrinciple < 0) {
curMonthlyPrinciple = 0;
}
curBorrowAmount = curBorrowAmount - curMonthlyPrinciple;
if (curBorrowAmount < 0) {
curBorrowAmount = 0;
}
totalDebt += curMonthlyInterest + curMonthlyPrinciple;
printf("#%3d %2d interest: %10.3f principal: %10.3f remaining principle: %10.3f mortage interest: %10.3f\n",
i+1, monthDayIndex, curMonthlyInterest, curMonthlyPrinciple, curBorrowAmount,
curMortageLinkInterest);
monthDayIndex = (monthDayIndex + 1 ) % 12;
}
printf("\nmonthlyPayAmount: %10.3f\n", monthlyPayAmount);
printf("\nlost/fixed principal: %10.3f\n", (paidAmount + mortageLinkAmount));
printf("paid amount: %10.3f\n", paidAmount);
printf("borrow amount: %10.3f\n", borrowAmount);
printf("total debt: %10.3f\n", totalDebt);
printf("cash rebate: %10.3f\n", rebate);
printf("total interest: %10.3f\n", totalInterest);
printf("totalMortageLinkInterest: %10.3f\n", totalMortageLinkInterest);
printf("total debt - cash rebate: %10.3f\n", (totalDebt - rebate));
printf("total debt - cash rebate - totalMortageLinkInterest: %10.3f\n",
(totalDebt - rebate - totalMortageLinkInterest));
printf("effective interest: %10.3f\n", (totalInterest - totalMortageLinkInterest));
printf("effective interest/borrow amount: %10.3f\n", (totalInterest - totalMortageLinkInterest) / borrowAmount);
}
int main(int argc, char** argv) {
if (argc != 8) {
printf("\
usage: %s flatPrice \
borrowPercentage \
years \
borrowRate \
cashRebateRate \
mortageLinkAmount \
mortageLinkRate\n",
argv[0]);
exit(1);
}
double flatPrice = atof(argv[1]);
double borrowPercentage= atof(argv[2]);
double year = atoi(argv[3]);
double borrowRate = atof(argv[4]);
double cashRebate = atof(argv[5]);
double mortageLinkAmount = atof(argv[6]);
double mortageLinkRate = atof(argv[7]);
calDebt(flatPrice * (1-borrowPercentage/100),
flatPrice * (borrowPercentage/100),
year, borrowRate,
cashRebate,
mortageLinkAmount,
mortageLinkRate);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment