From 2180ad3d66d94c8948af1e259fcc8ec10737fb1c Mon Sep 17 00:00:00 2001 From: gonzo Date: Fri, 19 May 2023 11:04:44 +0200 Subject: added mortgage loans --- Makefile | 4 ++-- loans.h | 6 ++++++ mortgage_loan.c | 31 +++++++++++++++++++++++++++++++ straight_line_loan.c | 1 + 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 mortgage_loan.c 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 +#include #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; } -- cgit v1.2.3