summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--bullet_loan.c8
-rw-r--r--contract.h13
-rw-r--r--loans.c4
-rw-r--r--loans.h16
-rw-r--r--mortgage_loan.c10
-rw-r--r--straight_line_loan.c8
6 files changed, 31 insertions, 28 deletions
diff --git a/bullet_loan.c b/bullet_loan.c
index 2fc73d6..8ec4be6 100644
--- a/bullet_loan.c
+++ b/bullet_loan.c
@@ -1,6 +1,6 @@
#include "loans.h"
-contract *bullet_init(int n, int d, float r, float P) {
+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));
@@ -12,16 +12,16 @@ contract *bullet_init(int n, int d, float r, float P) {
return c;
}
-float bullet_update(contract *c) {
+double bullet_update(contract *c) {
if (c->k > c->n)
- return 0.0f;
+ return 0.0;
c->k++;
c->I = c->r * c->P;
if (c->k == c->n) {
c->D = c->P;
- c->P = 0.0f;
+ c->P = 0.0;
}
if (c->k <= c->d) c->P += c->I;
diff --git a/contract.h b/contract.h
index 4c74cb0..bcaef15 100644
--- a/contract.h
+++ b/contract.h
@@ -1,12 +1,15 @@
#pragma once
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wpadded"
typedef struct contract {
unsigned int n; /* duration of the loan */
unsigned int k; /* current year */
unsigned int d; /* delay period of the loan */
- float r; /* interest rate */
- float P; /* part of the pricipal left at year k */
- float I; /* the interest for year k */
- float D; /* part of the capital reinbursed on year k */
- float A; /* annuity for year k */
+ double r; /* interest rate */
+ double P; /* part of the pricipal left at year k */
+ double I; /* the interest for year k */
+ double D; /* part of the capital reinbursed on year k */
+ double A; /* annuity for year k */
} contract;
+#pragma clang diagnostic pop
diff --git a/loans.c b/loans.c
index f812aaf..46a5100 100644
--- a/loans.c
+++ b/loans.c
@@ -1,7 +1,7 @@
#include "loans.h"
#include "stdio.h"
-loan loan_init(loan_type type, int n, int d, float r, float P) {
+loan loan_init(loan_type type, int n, int d, double r, double P) {
loan ret;
ret.type = type;
@@ -20,7 +20,7 @@ loan loan_init(loan_type type, int n, int d, float r, float P) {
return ret;
}
-float loan_update(loan l) {
+double loan_update(loan l) {
switch (l.type) {
case BULLET:
return bullet_update(l.c);
diff --git a/loans.h b/loans.h
index 88bbb41..aed283e 100644
--- a/loans.h
+++ b/loans.h
@@ -18,21 +18,21 @@ typedef struct loan {
#pragma clang diagnostic pop
/**** BULLET LOAN ****/
-contract *bullet_init(int n, int d, float r, float P);
-float bullet_update(contract *);
+contract *bullet_init(int n, int d, double r, double P);
+double bullet_update(contract *);
void bullet_free(contract *c);
/**** STRAIGHT LINE ****/
-contract *sl_init(int n, int d, float r, float P);
-float sl_update(contract *);
+contract *sl_init(int n, int d, double r, double P);
+double sl_update(contract *);
void sl_free(contract *);
/**** MORTGAGE ****/
-contract *mort_init(int n, int d, float r, float P);
-float mort_update(contract *);
+contract *mort_init(int n, int d, double r, double P);
+double mort_update(contract *);
void mort_free(contract *);
/**** GENERAL LOAN STUFF ****/
-loan loan_init(loan_type type, int n, int d, float r, float P);
-float loan_update(loan);
+loan loan_init(loan_type type, int n, int d, double r, double P);
+double loan_update(loan);
void loan_free(loan);
diff --git a/mortgage_loan.c b/mortgage_loan.c
index 2959214..a9ab28b 100644
--- a/mortgage_loan.c
+++ b/mortgage_loan.c
@@ -1,6 +1,6 @@
#include "loans.h"
-contract *mort_init(int n, int d, float r, float P) {
+contract *mort_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));
@@ -12,13 +12,13 @@ contract *mort_init(int n, int d, float r, float P) {
return c;
}
-float mort_update(contract *c) {
+double mort_update(contract *c) {
if (c->k > c->n)
- return 0.0f;
+ return 0.0;
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->A = - (c->P * c->r * pow(1.0 + c->r, (double)(c->n - c->k)));
+ c->A = c->A / (1.0 - pow(1.0 + c->r, (double)(c->n - c->k)));
}
c->k++;
c->I = c->r * c->P;
diff --git a/straight_line_loan.c b/straight_line_loan.c
index df86dcc..024fd49 100644
--- a/straight_line_loan.c
+++ b/straight_line_loan.c
@@ -1,6 +1,6 @@
#include "loans.h"
-contract *sl_init(int n, int d, float r, float P) {
+contract *sl_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));
@@ -12,11 +12,11 @@ contract *sl_init(int n, int d, float r, float P) {
return c;
}
-float sl_update(contract *c) {
+double sl_update(contract *c) {
if (c->k > c->n)
- return 0.0f;
+ return 0.0;
- if (c->k == c->d) c->D = c->P/(float)(c->n - c->k);
+ if (c->k == c->d) c->D = c->P/(double)(c->n - c->k);
c->k++;
c->I = c->r * c->P;