summaryrefslogtreecommitdiffstats
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
parentad2a003a77dc234a6eb5628b298630d54b337604 (diff)
downloadloan_calc-2180ad3d66d94c8948af1e259fcc8ec10737fb1c.tar.gz
added mortgage loans
-rw-r--r--Makefile4
-rw-r--r--loans.h6
-rw-r--r--mortgage_loan.c31
-rw-r--r--straight_line_loan.c1
4 files changed, 40 insertions, 2 deletions
diff --git a/Makefile b/Makefile
index 3446aa7..d3a30d0 100644
--- a/Makefile
+++ b/Makefile
@@ -8,7 +8,7 @@ CC=clang --std=c99
AR=ar ruvcs
CFLAGS=-Wall -Wextra -Werror -Weverything -Wpadded -pedantic
-LIBS=-lncurses
+LIBS=-lncurses -lm
BLDFLAGS=-g
RELFLAGS=-O2
@@ -18,7 +18,7 @@ HEAD=
OBJ=${DEPS:.c=.o}
LIB=las.a
-LDEPS=bullet_loan.c straight_line_loan.c
+LDEPS=bullet_loan.c straight_line_loan.c mortgage_loan.c
LHEAD=contract.h loans.h
LOBJ=${LDEPS:.c=.o}
diff --git a/loans.h b/loans.h
index 8613028..fd276ae 100644
--- a/loans.h
+++ b/loans.h
@@ -1,5 +1,6 @@
#pragma once
#include <stdlib.h>
+#include <math.h>
#include "contract.h"
/**** BULLET LOAN ****/
@@ -11,3 +12,8 @@ void bullet_free(contract *c);
contract *sl_init(int n, int d, float r, float P);
float sl_update(contract *);
void sl_free(contract *);
+
+/**** MORTGAGE ****/
+contract *mort_init(int n, int d, float r, float P);
+float mort_update(contract *);
+void mort_free(contract *);
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);
+}
diff --git a/straight_line_loan.c b/straight_line_loan.c
index 0d25247..73ba859 100644
--- a/straight_line_loan.c
+++ b/straight_line_loan.c
@@ -20,6 +20,7 @@ float sl_update(contract *c) {
c->I = c->r * c->P;
c->P = c->P - c->D;
c->A = c->D + c->I;
+ /* c->D is fixed ! */
return c->A;
}