You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ce...@apache.org on 2021/02/18 10:00:13 UTC
svn commit: r1886658 - in /poi/trunk/src:
java/org/apache/poi/ss/formula/functions/FinanceLib.java
testcases/org/apache/poi/ss/formula/functions/TestFinanceLib.java
Author: centic
Date: Thu Feb 18 10:00:13 2021
New Revision: 1886658
URL: http://svn.apache.org/viewvc?rev=1886658&view=rev
Log:
FinanceLib: Simplify code and add a few more tests
Modified:
poi/trunk/src/java/org/apache/poi/ss/formula/functions/FinanceLib.java
poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestFinanceLib.java
Modified: poi/trunk/src/java/org/apache/poi/ss/formula/functions/FinanceLib.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/functions/FinanceLib.java?rev=1886658&r1=1886657&r2=1886658&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/functions/FinanceLib.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/functions/FinanceLib.java Thu Feb 18 10:00:13 2021
@@ -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<iSize; i++) {
- npv += cfs[i] / trate;
+ for (double cf : cfs) {
+ npv += cf / trate;
trate *= r1;
}
return npv;
@@ -141,17 +135,14 @@ public final class FinanceLib {
* @param t type (true=pmt at beginning of period, false=pmt at end of period)
*/
public static double pmt(double r, double n, double p, double f, boolean t) {
- double retval = 0;
if (r == 0) {
- retval = -1*(f+p)/n;
- }
- else {
- double r1 = r + 1;
- retval = ( f + p * Math.pow(r1, n) ) * r
+ return -1*(f+p)/n;
+ } else {
+ double r1 = r + 1;
+ return ( f + p * Math.pow(r1, n) ) * r
/
((t ? r1 : 1) * (1 - Math.pow(r1, n)));
}
- return retval;
}
/**
@@ -163,9 +154,8 @@ public final class FinanceLib {
* @param t type (true=pmt at beginning of period, false=pmt at end of period)
*/
public static double nper(double r, double y, double p, double f, boolean t) {
- double retval = 0;
if (r == 0) {
- retval = -1 * (f + p) / y;
+ return -1 * (f + p) / y;
} else {
double r1 = r + 1;
double ryr = (t ? r1 : 1) * y / r;
@@ -176,10 +166,7 @@ public final class FinanceLib {
? Math.log(-p - ryr)
: Math.log(p + ryr);
double a3 = Math.log(r1);
- retval = (a1 - a2) / a3;
+ return (a1 - a2) / a3;
}
- return retval;
}
-
-
}
Modified: poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestFinanceLib.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestFinanceLib.java?rev=1886658&r1=1886657&r2=1886658&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestFinanceLib.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestFinanceLib.java Thu Feb 18 10:00:13 2021
@@ -33,7 +33,22 @@ class TestFinanceLib extends BaseTestNum
void testFv() {
double f, r, y, p, x;
int n;
- boolean t = false;
+ boolean t;
+
+ r = 0; n = 1; y = 1; p = 1; t = true;
+ f = FinanceLib.fv(r, n, y, p, t);
+ x = -2;
+ assertDouble("fv ", x, f);
+
+ r = 0.12/12; n = 12; y = -1000; p = 0; t = false;
+ f = FinanceLib.fv(r, n, y, p, t);
+ x = 12682.50301319;
+ assertDouble("fv ", x, f);
+
+ r = 0.06/12; n = 10; y = -200; p = -500; t = true;
+ f = FinanceLib.fv(r, n, y, p, t);
+ x = 2581.4033740;
+ assertDouble("fv ", x, f);
r = 0; n = 3; y = 2; p = 7; t = true;
f = FinanceLib.fv(r, n, y, p, t);
@@ -105,7 +120,7 @@ class TestFinanceLib extends BaseTestNum
void testPmt() {
double f, r, y, p, x;
int n;
- boolean t = false;
+ boolean t;
r = 0; n = 3; p = 2; f = 7; t = true;
y = FinanceLib.pmt(r, n, p, f, t);
@@ -139,7 +154,7 @@ class TestFinanceLib extends BaseTestNum
void testPv() {
double f, r, y, p, x;
int n;
- boolean t = false;
+ boolean t;
r = 0; n = 3; y = 2; f = 7; t = true;
f = FinanceLib.pv(r, n, y, f, t);
@@ -182,7 +197,7 @@ class TestFinanceLib extends BaseTestNum
@Test
void testNper() {
double f, r, y, p, x, n;
- boolean t = false;
+ boolean t;
r = 0; y = 7; p = 2; f = 3; t = false;
n = FinanceLib.nper(r, y, p, f, t);
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org