summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorgonzo <gonzo@toniatuh.com>2023-05-19 17:18:57 +0200
committergonzo <gonzo@toniatuh.com>2023-05-19 17:18:57 +0200
commitd01fc15a98e5ede02e3f9aff04e3d4014e6e484c (patch)
tree6b276d8fdf6c987c7eb65b3a21fda494dc2638b7
parent2180ad3d66d94c8948af1e259fcc8ec10737fb1c (diff)
downloadloan_calc-d01fc15a98e5ede02e3f9aff04e3d4014e6e484c.tar.gz
added delays to loans
-rw-r--r--bullet_loan.c8
-rw-r--r--mortgage_loan.c20
-rw-r--r--straight_line_loan.c16
3 files changed, 31 insertions, 13 deletions
diff --git a/bullet_loan.c b/bullet_loan.c
index 189ca05..2fc73d6 100644
--- a/bullet_loan.c
+++ b/bullet_loan.c
@@ -2,7 +2,7 @@
contract *bullet_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;
@@ -15,13 +15,17 @@ contract *bullet_init(int n, int d, float r, float P) {
float bullet_update(contract *c) {
if (c->k > c->n)
return 0.0f;
+
c->k++;
c->I = c->r * c->P;
+
if (c->k == c->n) {
c->D = c->P;
c->P = 0.0f;
}
- c->A = c->D + c->I;
+
+ if (c->k <= c->d) c->P += c->I;
+ else c->A = c->D + c->I;
return c->A;
}
diff --git a/mortgage_loan.c b/mortgage_loan.c
index 386828a..2959214 100644
--- a/mortgage_loan.c
+++ b/mortgage_loan.c
@@ -2,14 +2,12 @@
contract *mort_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->A = - (P * r * powf(1.0f + r, (float)n));
- c->A = c->A / (1.0f - powf(1.0f + r, (float)n));
return c;
}
@@ -17,11 +15,21 @@ contract *mort_init(int n, int d, float r, float P) {
float mort_update(contract *c) {
if (c->k > c->n)
return 0.0f;
+
+ if (c->k == c->d) {
+ c->A = - (c->P * c->r * powf(1.0f + c->r, (float)(c->n - c->k)));
+ c->A = c->A / (1.0f - powf(1.0f + c->r, (float)(c->n - c->k)));
+ }
c->k++;
c->I = c->r * c->P;
- c->D = c->A - c->I;
- c->P = c->P - c->D;
- /* c->A is fixed ! */
+
+ if (c->k <= c->d)
+ c->P += c->I;
+ else {
+ c->D = c->A - c->I;
+ c->P = c->P - c->D;
+ /* c->A is fixed ! */
+ }
return c->A;
}
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;
}