Skip to content

Commit

Permalink
add mirr test
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1922096 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
pjfanning committed Nov 25, 2024
1 parent edb9aea commit 5874f55
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ protected int getMaxNumOperands() {
@Override
protected double evaluate(double[] values) throws EvaluationException {

double financeRate = values[values.length-2];
double reinvestRate = values[values.length-1];
final double financeRate = values[values.length-2];
final double reinvestRate = values[values.length-1];

double[] mirrValues = Arrays.copyOf(values, values.length - 2);
final double[] mirrValues = Arrays.copyOf(values, values.length - 2);

boolean mirrValuesAreAllNegatives = true;
boolean mirrValuesAreAllPositives = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,41 @@ void testEvaluateInSheet() {
assertEquals(0.18736225093, res, 0.00000001);
}

@Test
void testMicrosoftSample() {
// https://support.microsoft.com/en-us/office/mirr-function-b020f038-7492-4fb4-93c1-35c345b53524
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Sheet1");

int row = 0;
sheet.createRow(row++).createCell(0).setCellValue("Data");
sheet.createRow(row++).createCell(0).setCellValue(-120000);
sheet.createRow(row++).createCell(0).setCellValue(39000);
sheet.createRow(row++).createCell(0).setCellValue(30000);
sheet.createRow(row++).createCell(0).setCellValue(21000);
sheet.createRow(row++).createCell(0).setCellValue(37000);
sheet.createRow(row++).createCell(0).setCellValue(46000);
sheet.createRow(row++).createCell(0).setCellValue(0.1);
sheet.createRow(row++).createCell(0).setCellValue(0.12);

HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
HSSFCell cell = sheet.createRow(row).createCell(0);
cell.setCellFormula("MIRR(A2:A7, A8, A9)");
fe.clearAllCachedResultValues();
fe.evaluateFormulaCell(cell);
assertEquals(0.126094, cell.getNumericCellValue(), 0.00000015);

cell.setCellFormula("MIRR(A2:A5, A8, A9)");
fe.clearAllCachedResultValues();
fe.evaluateFormulaCell(cell);
assertEquals(-0.048044655, cell.getNumericCellValue(), 0.00000015);

cell.setCellFormula("MIRR(A2:A7, A8, .14)");
fe.clearAllCachedResultValues();
fe.evaluateFormulaCell(cell);
assertEquals(0.134759111, cell.getNumericCellValue(), 0.00000015);
}

@Test
void testMirrFromSpreadsheet() {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("mirrTest.xls");
Expand Down

0 comments on commit 5874f55

Please sign in to comment.