summaryrefslogtreecommitdiffstats
path: root/mortgage_loan.c
diff options
context:
space:
mode:
Diffstat (limited to 'mortgage_loan.c')
-rw-r--r--mortgage_loan.c20
1 files changed, 14 insertions, 6 deletions
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;
}