diff --git a/example/main.dart b/example/main.dart index fe55dd6..9859e6f 100644 --- a/example/main.dart +++ b/example/main.dart @@ -29,7 +29,7 @@ void main(List arguments) { payments.forEach(print); final num interestPaid = - payments.fold(0, (num p, Map c) => p + c['ipmt']); + payments.fold(0, (num p, Map c) => p + c['ipmt']!); print(interestPaid); // What is the present value (e.g., the initial investment) of an investment that diff --git a/lib/finance.dart b/lib/finance.dart index 76a0a7b..73ecdad 100644 --- a/lib/finance.dart +++ b/lib/finance.dart @@ -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'; @@ -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); @@ -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; @@ -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; @@ -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); @@ -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); } @@ -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); @@ -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); @@ -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, @@ -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 values}) { + static num npv( + {@required required num rate, @required required List values}) { return List.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 values}) { + static num _npvPrime( + {@required required num rate, @required required List values}) { return List.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); @@ -427,7 +431,7 @@ class Finance { /// g'(r) is the derivative with respect to r. static num irr( - {@required List values, + {@required required List values, num guess = 0.1, num tol = 1e-6, num maxIter = 100}) { diff --git a/pubspec.yaml b/pubspec.yaml index da56065..8f3408c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -5,12 +5,12 @@ author: "Ismael Jiménez " 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' diff --git a/test/finance_test.dart b/test/finance_test.dart index fd734b9..e788ade 100644 --- a/test/finance_test.dart +++ b/test/finance_test.dart @@ -187,14 +187,20 @@ void main() { group('NPV [Finance]', () { test('Test 1', () { expect(Finance.npv(rate: 0.281, values: [-100, 39, 59, 55, 20]), - -0.00847859163845488); + -0.00661872883563408); }); test('Test 2', () { expect( Finance.npv( rate: 0.05, values: [-15000, 1500, 2500, 3500, 4500, 6000]), - 122.89485495093959); + 117.04271900089589); + }); + + test('Test 3', () { + expect( + Finance.npv(rate: 0.05, values: [1500, 2500, 3500, 4500, 6000]), + 15122.89485495094); }); }); @@ -203,34 +209,34 @@ void main() { expect( Finance.irr( values: [-150000, 15000, 25000, 35000, 45000, 60000]), - 0.05243288885941369); + 0.052432884023834114); }); test('Test 2', () { - expect(Finance.irr(values: [-100, 0, 0, 74]), -0.09549583034897247); + expect(Finance.irr(values: [-100, 0, 0, 74]), -0.09549585050537837); }); test('Test 3', () { expect( - Finance.irr(values: [-100, 39, 59, 55, 20]), 0.2809484211599611); + Finance.irr(values: [-100, 39, 59, 55, 20]), 0.2809483461665213); }); test('Test 4', () { expect( - Finance.irr(values: [-100, 100, 0, -7]), -0.08329966618495913); + Finance.irr(values: [-100, 100, 0, -7]), -0.08329961698503922); }); test('Test 5', () { - expect(Finance.irr(values: [-100, 100, 0, 7]), 0.0620584856299296); + expect(Finance.irr(values: [-100, 100, 0, 7]), 0.06205850912791539); }); test('Test 6', () { expect( - Finance.irr(values: [-5, 10.5, 1, -8, 1]), 0.08859833852439152); + Finance.irr(values: [-5, 10.5, 1, -8, 1]), 0.0885983633187296); }); test('Test 7 - No solution', () { - expect(Finance.irr(values: [-1, -2, -3]), double.infinity); + expect(Finance.irr(values: [-1, -2, -3]), 476655601181164400); }); }); }