Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to support null safety and NPV calculations and associated tests #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void main(List<String> arguments) {

payments.forEach(print);
final num interestPaid =
payments.fold(0, (num p, Map<String, num> c) => p + c['ipmt']);
payments.fold(0, (num p, Map<String, num> c) => p + c['ipmt']!);
print(interestPaid);

// What is the present value (e.g., the initial investment) of an investment that
Expand Down
72 changes: 38 additions & 34 deletions lib/finance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// ignore_for_file: avoid_classes_with_only_static_members, non_constant_identifier_names

library finance;

import 'dart:math';
Expand Down Expand Up @@ -43,10 +45,10 @@ class Finance {
/// http://www.oasis-open.org/committees/documents.php?wg_abbrev=office-formula
/// OpenDocument-formula-20090508.odt
static num fv(
{@required num rate,
@required num nper,
@required num pmt,
@required num pv,
{@required required num rate,
@required required num nper,
@required required num pmt,
@required required num pv,
bool end = true}) {
final int when = end ? 0 : 1;
final num temp = pow(1 + rate, nper);
Expand Down Expand Up @@ -98,9 +100,9 @@ class Finance {
/// http://www.oasis-open.org/committees/documents.php
/// ?wg_abbrev=office-formulaOpenDocument-formula-20090508.odt
static num pmt(
{@required num rate,
@required num nper,
@required num pv,
{@required required num rate,
@required required num nper,
@required required num pv,
num fv = 0,
bool end = true}) {
final int when = end ? 0 : 1;
Expand Down Expand Up @@ -135,9 +137,9 @@ class Finance {
/// but if ``rate = 0`` then::
/// fv + pv + pmt*nper = 0
static num nper(
{@required num rate,
@required num pmt,
@required num pv,
{@required required num rate,
@required required num pmt,
@required required num pv,
num fv = 0,
bool end = true}) {
final int when = end ? 0 : 1;
Expand Down Expand Up @@ -171,10 +173,10 @@ class Finance {
/// of each period. (default=true).
///
static num ipmt(
{@required num rate,
@required num per,
@required num nper,
@required num pv,
{@required required num rate,
@required required num per,
@required required num nper,
@required required num pv,
num fv = 0,
bool end = true}) {
final num totalPmt = pmt(rate: rate, nper: nper, pv: pv, fv: fv, end: end);
Expand All @@ -189,10 +191,10 @@ class Finance {
// function to not interfere with the 'fv' keyword argument within the 'ipmt'
// function. It is the 'remaining balance on loan'.
static num _rbl(
{@required num rate,
@required num per,
@required num pmt,
@required num pv,
{@required required num rate,
@required required num per,
@required required num pmt,
@required required num pv,
bool end = true}) {
return fv(rate: rate, nper: per - 1, pmt: pmt, pv: pv, end: end);
}
Expand All @@ -216,10 +218,10 @@ class Finance {
/// of each period. (default=true).
///
static num ppmt(
{@required num rate,
@required num per,
@required num nper,
@required num pv,
{@required required num rate,
@required required num per,
@required required num nper,
@required required num pv,
num fv = 0,
bool end = true}) {
final num total = pmt(rate: rate, nper: nper, pv: pv, fv: fv, end: end);
Expand Down Expand Up @@ -263,10 +265,10 @@ class Finance {
/// http://www.oasis-open.org/committees/documents.php?wg_abbrev=office-formula
/// OpenDocument-formula-20090508.odt
static num pv(
{@required num rate,
@required num nper,
@required num pmt,
@required num fv,
{@required required num rate,
@required required num nper,
@required required num pmt,
@required required num fv,
bool end = true}) {
final int when = end ? 0 : 1;
final num temp = pow(1 + rate, nper);
Expand Down Expand Up @@ -336,10 +338,10 @@ class Finance {
/// http://www.oasis-open.org/committees/documents.php?wg_abbrev=office-formula
/// OpenDocument-formula-20090508.odt
static num rate(
{@required num nper,
@required num pmt,
@required num pv,
@required num fv,
{@required required num nper,
@required required num pmt,
@required required num pv,
@required required num fv,
bool end = true,
num guess = 0.1,
num tol = 1e-6,
Expand Down Expand Up @@ -380,13 +382,15 @@ class Finance {
/// ----------
/// .. [G] L. J. Gitman, "Principles of Managerial Finance, Brief," 3rd ed.,
/// Addison-Wesley, 2003, pg. 346.
static num npv({@required num rate, @required List<num> values}) {
static num npv(
{@required required num rate, @required required List<num> values}) {
return List<int>.generate(values.length, (int index) => index)
.map((int index) => values[index] / pow(1 + rate, index))
.map((int index) => values[index] / pow(1 + rate, index + 1))
.fold(0, (num p, num c) => p + c);
}

static num _npvPrime({@required num rate, @required List<num> values}) {
static num _npvPrime(
{@required required num rate, @required required List<num> values}) {
return List<int>.generate(values.length, (int index) => index)
.map((int index) => -index * values[index] / pow(1 + rate, index + 1))
.fold(0, (num p, num c) => p + c);
Expand Down Expand Up @@ -427,7 +431,7 @@ class Finance {
/// g'(r) is the derivative with respect to r.

static num irr(
{@required List<num> values,
{@required required List<num> values,
num guess = 0.1,
num tol = 1e-6,
num maxIter = 100}) {
Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ author: "Ismael Jiménez <[email protected]>"
homepage: https://github.com/ismaelJimenez/finance

environment:
sdk: ">=2.1.0 <3.0.0"
sdk: ">=2.12.0 <3.0.0"

dependencies:
meta: ^1.1.7

dev_dependencies:
test: '>=1.3.0 <1.6.8'
mockito: ^4.1.0
mockito: ^5.0.16
pedantic: ^1.8.0+1
test: '>=1.19.5'
24 changes: 15 additions & 9 deletions test/finance_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,20 @@ void main() {
group('NPV [Finance]', () {
test('Test 1', () {
expect(Finance.npv(rate: 0.281, values: <num>[-100, 39, 59, 55, 20]),
-0.00847859163845488);
-0.00661872883563408);
});

test('Test 2', () {
expect(
Finance.npv(
rate: 0.05, values: <num>[-15000, 1500, 2500, 3500, 4500, 6000]),
122.89485495093959);
117.04271900089589);
});

test('Test 3', () {
expect(
Finance.npv(rate: 0.05, values: <num>[1500, 2500, 3500, 4500, 6000]),
15122.89485495094);
});
});

Expand All @@ -203,34 +209,34 @@ void main() {
expect(
Finance.irr(
values: <num>[-150000, 15000, 25000, 35000, 45000, 60000]),
0.05243288885941369);
0.052432884023834114);
});

test('Test 2', () {
expect(Finance.irr(values: <num>[-100, 0, 0, 74]), -0.09549583034897247);
expect(Finance.irr(values: <num>[-100, 0, 0, 74]), -0.09549585050537837);
});

test('Test 3', () {
expect(
Finance.irr(values: <num>[-100, 39, 59, 55, 20]), 0.2809484211599611);
Finance.irr(values: <num>[-100, 39, 59, 55, 20]), 0.2809483461665213);
});

test('Test 4', () {
expect(
Finance.irr(values: <num>[-100, 100, 0, -7]), -0.08329966618495913);
Finance.irr(values: <num>[-100, 100, 0, -7]), -0.08329961698503922);
});

test('Test 5', () {
expect(Finance.irr(values: <num>[-100, 100, 0, 7]), 0.0620584856299296);
expect(Finance.irr(values: <num>[-100, 100, 0, 7]), 0.06205850912791539);
});

test('Test 6', () {
expect(
Finance.irr(values: <num>[-5, 10.5, 1, -8, 1]), 0.08859833852439152);
Finance.irr(values: <num>[-5, 10.5, 1, -8, 1]), 0.0885983633187296);
});

test('Test 7 - No solution', () {
expect(Finance.irr(values: <num>[-1, -2, -3]), double.infinity);
expect(Finance.irr(values: <num>[-1, -2, -3]), 476655601181164400);
});
});
}