diff options
| author | gonzo <gonzo@toniatuh.com> | 2023-05-19 22:28:10 +0200 |
|---|---|---|
| committer | gonzo <gonzo@toniatuh.com> | 2023-05-19 22:28:10 +0200 |
| commit | 2c380f8df666fcebd979511f33e7d72291b389cd (patch) | |
| tree | 0238209fa73cfa7e4c0a8a58b3ffd8426c20db60 | |
| parent | 0751b55f611b889f63dfc2bc2705e7537703cb43 (diff) | |
| download | loan_calc-2c380f8df666fcebd979511f33e7d72291b389cd.tar.gz | |
moved from floats to doubles for added precision
| -rw-r--r-- | bullet_loan.c | 8 | ||||
| -rw-r--r-- | contract.h | 13 | ||||
| -rw-r--r-- | loans.c | 4 | ||||
| -rw-r--r-- | loans.h | 16 | ||||
| -rw-r--r-- | mortgage_loan.c | 10 | ||||
| -rw-r--r-- | straight_line_loan.c | 8 |
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; @@ -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 @@ -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); @@ -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; |
