summaryrefslogtreecommitdiffstats
path: root/mortgage_loan.c
blob: 386828afa12f74f96508c2ed6aa59ca0633bedb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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);
}