From 297c16f1f017da4521d054b1fb942a7233098dec Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Thu, 18 Feb 2021 10:00:13 +0000 Subject: FinanceLib: Simplify code and add a few more tests git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1886658 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/ss/formula/functions/FinanceLib.java | 41 ++++++++-------------- .../poi/ss/formula/functions/TestFinanceLib.java | 23 +++++++++--- 2 files changed, 33 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/java/org/apache/poi/ss/formula/functions/FinanceLib.java b/src/java/org/apache/poi/ss/formula/functions/FinanceLib.java index 19a7510c68..dc4917f5e2 100644 --- a/src/java/org/apache/poi/ss/formula/functions/FinanceLib.java +++ b/src/java/org/apache/poi/ss/formula/functions/FinanceLib.java @@ -74,17 +74,14 @@ public final class FinanceLib { * @param t type (true=pmt at beginning of period, false=pmt at end of period) */ public static double fv(double r, double n, double y, double p, boolean t) { - double retval = 0; if (r == 0) { - retval = -1*(p+(n*y)); - } - else { + return -1*(p+(n*y)); + } else { double r1 = r + 1; - retval =((1-Math.pow(r1, n)) * (t ? r1 : 1) * y ) / r + return ((1-Math.pow(r1, n)) * (t ? r1 : 1) * y ) / r - p*Math.pow(r1, n); } - return retval; } /** @@ -99,17 +96,14 @@ public final class FinanceLib { * @param t type (true=pmt at beginning of period, false=pmt at end of period) */ public static double pv(double r, double n, double y, double f, boolean t) { - double retval = 0; if (r == 0) { - retval = -1*((n*y)+f); - } - else { + return -1*((n*y)+f); + } else { double r1 = r + 1; - retval =(( ( 1 - Math.pow(r1, n) ) / r ) * (t ? r1 : 1) * y - f) + return (( ( 1 - Math.pow(r1, n) ) / r ) * (t ? r1 : 1) * y - f) / Math.pow(r1, n); } - return retval; } /** @@ -125,8 +119,8 @@ public final class FinanceLib { double npv = 0; double r1 = r + 1; double trate = r1; - for (int i=0, iSize=cfs.length; i