summaryrefslogtreecommitdiffstats
path: root/mortgage_loan.c
diff options
context:
space:
mode:
authorgonzo <gonzo@toniatuh.com>2023-05-19 11:04:44 +0200
committergonzo <gonzo@toniatuh.com>2023-05-19 11:04:44 +0200
commit2180ad3d66d94c8948af1e259fcc8ec10737fb1c (patch)
treedb1c6ef7ca7d6de91271a4c059f24921ecf32268 /mortgage_loan.c
parentad2a003a77dc234a6eb5628b298630d54b337604 (diff)
downloadloan_calc-2180ad3d66d94c8948af1e259fcc8ec10737fb1c.tar.gz
added mortgage loans
Diffstat (limited to 'mortgage_loan.c')
-rw-r--r--mortgage_loan.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/mortgage_loan.c b/mortgage_loan.c
new file mode 100644
index 0000000..386828a
--- /dev/null
+++ b/mortgage_loan.c
@@ -0,0 +1,31 @@
+#include "loans.h"
+
+contract *mort_init(int n, int d, float r, float P) {
+ if ((n < 0)) return NULL;
+ if ((d > n)||(d < 0)) return NULL;
+ contract *c = (contract *)calloc(1, sizeof(contract));
+ c->n = (unsigned int)n;
+ c->d = (unsigned int)d;
+ c->r = r;
+ c->P = P;
+ c->A = - (P * r * powf(1.0f + r, (float)n));
+ c->A = c->A / (1.0f - powf(1.0f + r, (float)n));
+
+ return c;
+}
+
+float mort_update(contract *c) {
+ if (c->k > c->n)
+ return 0.0f;
+ c->k++;
+ c->I = c->r * c->P;
+ c->D = c->A - c->I;
+ c->P = c->P - c->D;
+ /* c->A is fixed ! */
+
+ return c->A;
+}
+
+void mort_free(contract *c) {
+ free(c);
+}