From d01fc15a98e5ede02e3f9aff04e3d4014e6e484c Mon Sep 17 00:00:00 2001 From: gonzo Date: Fri, 19 May 2023 17:18:57 +0200 Subject: added delays to loans --- straight_line_loan.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'straight_line_loan.c') diff --git a/straight_line_loan.c b/straight_line_loan.c index 73ba859..df86dcc 100644 --- a/straight_line_loan.c +++ b/straight_line_loan.c @@ -2,13 +2,12 @@ contract *sl_init(int n, int d, float r, float P) { if ((n < 0)) return NULL; - if ((d > n)||(d < 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->D = P/(float)n; return c; } @@ -16,11 +15,18 @@ contract *sl_init(int n, int d, float r, float P) { float sl_update(contract *c) { if (c->k > c->n) return 0.0f; + + if (c->k == c->d) c->D = c->P/(float)(c->n - c->k); c->k++; c->I = c->r * c->P; - c->P = c->P - c->D; - c->A = c->D + c->I; - /* c->D is fixed ! */ + + if (c->k <= c->d) + c->P += c->I; + else { + c->P = c->P - c->D; + c->A = c->D + c->I; + /* c->D is fixed ! */ + } return c->A; } -- cgit v1.2.3