You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestFinanceLib.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /*
  18. * Created on May 23, 2005
  19. *
  20. */
  21. package org.apache.poi.ss.formula.functions;
  22. import org.apache.poi.hssf.usermodel.HSSFCell;
  23. import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
  24. import org.apache.poi.hssf.usermodel.HSSFSheet;
  25. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  26. import org.apache.poi.ss.util.Utils;
  27. import org.junit.jupiter.api.Test;
  28. import java.io.IOException;
  29. import static org.apache.poi.ss.util.Utils.addRow;
  30. class TestFinanceLib extends BaseTestNumeric {
  31. @Test
  32. void testFv() {
  33. double f, r, y, p, x;
  34. int n;
  35. boolean t;
  36. r = 0; n = 1; y = 1; p = 1; t = true;
  37. f = FinanceLib.fv(r, n, y, p, t);
  38. x = -2;
  39. assertDouble("fv ", x, f);
  40. r = 0.12/12; n = 12; y = -1000; p = 0; t = false;
  41. f = FinanceLib.fv(r, n, y, p, t);
  42. x = 12682.50301319;
  43. assertDouble("fv ", x, f);
  44. r = 0.06/12; n = 10; y = -200; p = -500; t = true;
  45. f = FinanceLib.fv(r, n, y, p, t);
  46. x = 2581.4033740;
  47. assertDouble("fv ", x, f);
  48. r = 0; n = 3; y = 2; p = 7; t = true;
  49. f = FinanceLib.fv(r, n, y, p, t);
  50. x = -13;
  51. assertDouble("fv ", x, f);
  52. r = 1; n = 10; y = 100; p = 10000; t = false;
  53. f = FinanceLib.fv(r, n, y, p, t);
  54. x = -10342300;
  55. assertDouble("fv ", x, f);
  56. r = 1; n = 10; y = 100; p = 10000; t = true;
  57. f = FinanceLib.fv(r, n, y, p, t);
  58. x = -10444600;
  59. assertDouble("fv ", x, f);
  60. r = 2; n = 12; y = 120; p = 12000; t = false;
  61. f = FinanceLib.fv(r, n, y, p, t);
  62. x = -6409178400d;
  63. assertDouble("fv ", x, f);
  64. r = 2; n = 12; y = 120; p = 12000; t = true;
  65. f = FinanceLib.fv(r, n, y, p, t);
  66. x = -6472951200d;
  67. assertDouble("fv ", x, f);
  68. // cross tests with pv
  69. r = 2.95; n = 13; y = 13000; p = -4406.78544294496; t = false;
  70. f = FinanceLib.fv(r, n, y, p, t);
  71. x = 333891.230010986; // as returned by excel
  72. assertDouble("fv ", x, f);
  73. r = 2.95; n = 13; y = 13000; p = -17406.7852148156; t = true;
  74. f = FinanceLib.fv(r, n, y, p, t);
  75. x = 333891.230102539; // as returned by excel
  76. assertDouble("fv ", x, f);
  77. }
  78. @Test
  79. void testNpv() {
  80. double r;
  81. double[] v;
  82. double npv;
  83. double x;
  84. r = 1; v = new double[]{100, 200, 300, 400};
  85. npv = FinanceLib.npv(r, v);
  86. x = 162.5;
  87. assertDouble("npv ", x, npv);
  88. r = 2.5; v = new double[]{1000, 666.66666, 333.33, 12.2768416};
  89. npv = FinanceLib.npv(r, v);
  90. x = 347.99232604144827;
  91. assertDouble("npv ", x, npv);
  92. r = 12.33333; v = new double[]{1000, 0, -900, -7777.5765};
  93. npv = FinanceLib.npv(r, v);
  94. x = 74.3742433377061;
  95. assertDouble("npv ", x, npv);
  96. r = 0.05; v = new double[]{200000, 300000.55, 400000, 1000000, 6000000, 7000000, -300000};
  97. npv = FinanceLib.npv(r, v);
  98. x = 11342283.4233124;
  99. assertDouble("npv ", x, npv);
  100. r = 0.08; v = new double[]{-1000, 500, 300, 800};
  101. npv = FinanceLib.npv(r, v);
  102. x = 328.9170387681793;
  103. assertDouble("npv ", x, npv);
  104. //https://support.microsoft.com/en-us/office/npv-function-8672cb67-2576-4d07-b67b-ac28acf2a568
  105. r = 0.1; v = new double[]{-10000, 3000, 4200, 6800};
  106. npv = FinanceLib.npv(r, v);
  107. x = 1188.4434123352207;
  108. assertDouble("npv ", x, npv);
  109. r = 0.08; v = new double[]{-40000, 8000, 9200, 10000, 12000, 14500};
  110. npv = FinanceLib.npv(r, v);
  111. x = 1779.6866249373716;
  112. assertDouble("npv ", x, npv);
  113. }
  114. @Test
  115. void testMicrosoftNpvExample2() throws IOException {
  116. //https://support.microsoft.com/en-us/office/npv-function-8672cb67-2576-4d07-b67b-ac28acf2a568
  117. try (HSSFWorkbook wb = new HSSFWorkbook()) {
  118. HSSFSheet sheet = wb.createSheet();
  119. addRow(sheet, 0, "Data");
  120. addRow(sheet, 1, 0.08);
  121. addRow(sheet, 2, -40000);
  122. addRow(sheet, 3, 8000);
  123. addRow(sheet, 4, 9200);
  124. addRow(sheet, 5, 10000);
  125. addRow(sheet, 6, 12000);
  126. addRow(sheet, 7, 14500);
  127. HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
  128. HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(100);
  129. Utils.assertDouble(fe, cell, "NPV(A2, A4:A8)+A3", 1922.061554932363, 0.00000000001);
  130. Utils.assertDouble(fe, cell, "NPV(A2, A4:A8, -9000)+A3", -3749.4650870155747, 0.00000000001);
  131. }
  132. }
  133. @Test
  134. void testPmt() {
  135. double f, r, y, p, x;
  136. int n;
  137. boolean t;
  138. r = 0; n = 3; p = 2; f = 7; t = true;
  139. y = FinanceLib.pmt(r, n, p, f, t);
  140. x = -3;
  141. assertDouble("pmt ", x, y);
  142. // cross check with pv
  143. r = 1; n = 10; p = -109.66796875; f = 10000; t = false;
  144. y = FinanceLib.pmt(r, n, p, f, t);
  145. x = 100;
  146. assertDouble("pmt ", x, y);
  147. r = 1; n = 10; p = -209.5703125; f = 10000; t = true;
  148. y = FinanceLib.pmt(r, n, p, f, t);
  149. x = 100;
  150. assertDouble("pmt ", x, y);
  151. // cross check with fv
  152. r = 2; n = 12; f = -6409178400d; p = 12000; t = false;
  153. y = FinanceLib.pmt(r, n, p, f, t);
  154. x = 120;
  155. assertDouble("pmt ", x, y);
  156. r = 2; n = 12; f = -6472951200d; p = 12000; t = true;
  157. y = FinanceLib.pmt(r, n, p, f, t);
  158. x = 120;
  159. assertDouble("pmt ", x, y);
  160. }
  161. @Test
  162. void testPv() {
  163. double f, r, y, p, x;
  164. int n;
  165. boolean t;
  166. r = 0; n = 3; y = 2; f = 7; t = true;
  167. f = FinanceLib.pv(r, n, y, f, t);
  168. x = -13;
  169. assertDouble("pv ", x, f);
  170. r = 1; n = 10; y = 100; f = 10000; t = false;
  171. p = FinanceLib.pv(r, n, y, f, t);
  172. x = -109.66796875;
  173. assertDouble("pv ", x, p);
  174. r = 1; n = 10; y = 100; f = 10000; t = true;
  175. p = FinanceLib.pv(r, n, y, f, t);
  176. x = -209.5703125;
  177. assertDouble("pv ", x, p);
  178. r = 2.95; n = 13; y = 13000; f = 333891.23; t = false;
  179. p = FinanceLib.pv(r, n, y, f, t);
  180. x = -4406.78544294496;
  181. assertDouble("pv ", x, p);
  182. r = 2.95; n = 13; y = 13000; f = 333891.23; t = true;
  183. p = FinanceLib.pv(r, n, y, f, t);
  184. x = -17406.7852148156;
  185. assertDouble("pv ", x, p);
  186. // cross tests with fv
  187. r = 2; n = 12; y = 120; f = -6409178400d; t = false;
  188. p = FinanceLib.pv(r, n, y, f, t);
  189. x = 12000;
  190. assertDouble("pv ", x, p);
  191. r = 2; n = 12; y = 120; f = -6472951200d; t = true;
  192. p = FinanceLib.pv(r, n, y, f, t);
  193. x = 12000;
  194. assertDouble("pv ", x, p);
  195. }
  196. @Test
  197. void testNper() {
  198. double f, r, y, p, x, n;
  199. boolean t;
  200. r = 0; y = 7; p = 2; f = 3; t = false;
  201. n = FinanceLib.nper(r, y, p, f, t);
  202. x = -0.71428571429; // can you believe it? excel returns nper as a fraction!??
  203. assertDouble("nper ", x, n);
  204. // cross check with pv
  205. r = 1; y = 100; p = -109.66796875; f = 10000; t = false;
  206. n = FinanceLib.nper(r, y, p, f, t);
  207. x = 10;
  208. assertDouble("nper ", x, n);
  209. r = 1; y = 100; p = -209.5703125; f = 10000; t = true;
  210. n = FinanceLib.nper(r, y, p, f, t);
  211. x = 10;
  212. assertDouble("nper ", x, n);
  213. // cross check with fv
  214. r = 2; y = 120; f = -6409178400d; p = 12000; t = false;
  215. n = FinanceLib.nper(r, y, p, f, t);
  216. x = 12;
  217. assertDouble("nper ", x, n);
  218. r = 2; y = 120; f = -6472951200d; p = 12000; t = true;
  219. n = FinanceLib.nper(r, y, p, f, t);
  220. x = 12;
  221. assertDouble("nper ", x, n);
  222. }
  223. }