summaryrefslogtreecommitdiffstats
path: root/bullet_loan.c
blob: 452885bfd7957f4dbc8ca68a376a890cbd11c5ca (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
32
33
34
35
36
37
#include "loans.h"

contract *bullet_init(int n, int d, double r, double 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;

	return c;
}

double bullet_update(contract *c) {
	if (c->k > c->n)
		return 0.0;

	c->k++;
	c->I = c->r * c->P;

	if (c->k == c->n) {
		c->D = c->P;
		c->P = 0.0;
	}

	if (c->k <= c->d)
		c->P += c->I;
	else
		c->A = c->D + c->I;

	return c->A;
}

void bullet_free(contract *c) { free(c); }