Skip to content

Commit 9ae521c

Browse files
authored
Fix RATE, PRICE, XIRR, and XNPV Functions (#1456)
There were about 20 skipped tests for RATE and PRICE marked "This test should be fixed". This change does that by fixing the code for those functions, validating the existing tests, and adding new ones. XIRR and XNPV are also substantially changed. As part of this change, the following functions also have minor changes: - isValidFrequency - COUPDAYBS - COUPNUM (additional tests) - DB - DDB PhpUnit reports 100% coverage for all the changed functions. Since I was dealing with skipped tests, I also fixed tests/PhpSpreadsheetTests/Writer/Xlsx/LocaleFloatsTest, which was being skipped in Windows. I also delete the temporary file which it creates. There is now only one remaining test which is skipped - ODS Reader is not complete enough to run some tests against it. Unfortunately, that test is too complicated for me to deal with now. In researching this change, I found several places in the code where special code was added for Gnumeric claiming: - Gnumeric does not handle free-format string dates - Gnumeric adds extra options, not available in Excel, for the frequency parameter for functions such as YIELD - Gnumeric rounds the results for DB and DDB to 2 decimal places None of these claims is true, at least not on a recent version of Gnumeric, and the code which supports these differences is removed. There did not appear to be any tests targeted for these supposed properties of Gnumeric. The PRICE function needed relatively minor changes - mostly additional tests for invalid input. The main problem with the PRICE tests is that Excel appears to have a bug. The algorithm is published: https://support.office.com/en-us/article/price-function-3ea9deac-8dfa-436f-a7c8-17ea02c21b0a The results that Excel returns for basis codes 2 and 3 appear to be incorrect in many cases. I have segregated these tests into a new test PRICE3. The results of these tests agree with the published algorithm, and to the results for LibreOffice and Gnumeric. The results returned by Excel do not agree with them. The tests which remain in the test PRICE all use basis codes other than 2 or 3, and all agree with Excel, LibreOffice, and Gnumeric. For the RATE function, there appears to be a problem with how the secant method was implemented. I studied the implementation of RATE in Python numpy, and adapted its implementation of secant method. The results now agree with numpy, and, more important, with Excel. XIRR, which calls XNPV, permits its dates to be earlier than the start date, whereas XNPV does not. I dealt with this by renaming the existing XNPV function to xnpvOrdered, adding a parameter to indicate whether start date has to be earliest. XNPV calls the new function with that parameter set to TRUE, and XIRR calls it with the parameter set to FALSE. Some additional error checking was added to xnpvOrdered, and also to XIRR. XIRR tests benefited from increasing the value of FINANCIAL_MAX_ITERATIONS. Finally, since this change is very test-related: samples/Basic/13_CalculationCyclicFormulae PhpUnit started reporting an error like "too much regression". The test deals with an infinite cyclic formula, and allowed the calculation engine to run for 100 cycles. The actual number of cycles seems irrelevant for the purpose of this test. I changed it to 15, and PhpUnit no longer complains.
1 parent 8eaceb0 commit 9ae521c

File tree

10 files changed

+718
-197
lines changed

10 files changed

+718
-197
lines changed

src/PhpSpreadsheet/Calculation/DateTime.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,13 @@ private static function dateDiff360($startDay, $startMonth, $startYear, $endDay,
5959
/**
6060
* getDateValue.
6161
*
62-
* @param string $dateValue
62+
* @param mixed $dateValue
6363
*
6464
* @return mixed Excel date/time serial value, or string if error
6565
*/
6666
public static function getDateValue($dateValue)
6767
{
6868
if (!is_numeric($dateValue)) {
69-
if ((is_string($dateValue)) &&
70-
(Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC)) {
71-
return Functions::VALUE();
72-
}
7369
if ((is_object($dateValue)) && ($dateValue instanceof \DateTimeInterface)) {
7470
$dateValue = Date::PHPToExcel($dateValue);
7571
} else {

src/PhpSpreadsheet/Calculation/Financial.php

+211-149
Large diffs are not rendered by default.

tests/PhpSpreadsheetTests/Calculation/FinancialTest.php

+54-6
Original file line numberDiff line numberDiff line change
@@ -436,17 +436,34 @@ public function providerNPV()
436436
*/
437437
public function testPRICE($expectedResult, ...$args)
438438
{
439-
$this->markTestIncomplete('TODO: This test should be fixed');
440-
441439
$result = Financial::PRICE(...$args);
442-
self::assertEqualsWithDelta($expectedResult, $result, 1E-8);
440+
self::assertEqualsWithDelta($expectedResult, $result, 1E-7);
443441
}
444442

445443
public function providerPRICE()
446444
{
447445
return require 'tests/data/Calculation/Financial/PRICE.php';
448446
}
449447

448+
/**
449+
* @dataProvider providerPRICE3
450+
*
451+
* @param mixed $expectedResult
452+
*/
453+
public function testPRICE3($expectedResult, ...$args)
454+
{
455+
// These results (PRICE function with basis codes 2 and 3)
456+
// agree with published algorithm, LibreOffice, and Gnumeric.
457+
// They do not agree with Excel.
458+
$result = Financial::PRICE(...$args);
459+
self::assertEqualsWithDelta($expectedResult, $result, 1E-7);
460+
}
461+
462+
public function providerPRICE3()
463+
{
464+
return require 'data/Calculation/Financial/PRICE3.php';
465+
}
466+
450467
/**
451468
* @dataProvider providerPRICEDISC
452469
*
@@ -486,8 +503,6 @@ public function providerPV()
486503
*/
487504
public function testRATE($expectedResult, ...$args)
488505
{
489-
$this->markTestIncomplete('TODO: This test should be fixed');
490-
491506
$result = Financial::RATE(...$args);
492507
self::assertEqualsWithDelta($expectedResult, $result, 1E-8);
493508
}
@@ -506,14 +521,47 @@ public function providerRATE()
506521
public function testXIRR($expectedResult, $message, ...$args)
507522
{
508523
$result = Financial::XIRR(...$args);
509-
self::assertEqualsWithDelta($expectedResult, $result, Financial::FINANCIAL_PRECISION, $message);
524+
if (is_numeric($result) && is_numeric($expectedResult)) {
525+
if ($expectedResult != 0) {
526+
$frac = $result / $expectedResult;
527+
if ($frac > 0.999999 && $frac < 1.000001) {
528+
$result = $expectedResult;
529+
}
530+
}
531+
}
532+
self::assertEquals($expectedResult, $result, $message);
510533
}
511534

512535
public function providerXIRR()
513536
{
514537
return require 'tests/data/Calculation/Financial/XIRR.php';
515538
}
516539

540+
/**
541+
* @dataProvider providerXNPV
542+
*
543+
* @param mixed $expectedResult
544+
* @param mixed $message
545+
*/
546+
public function testXNPV($expectedResult, $message, ...$args)
547+
{
548+
$result = Financial::XNPV(...$args);
549+
if (is_numeric($result) && is_numeric($expectedResult)) {
550+
if ($expectedResult != 0) {
551+
$frac = $result / $expectedResult;
552+
if ($frac > 0.999999 && $frac < 1.000001) {
553+
$result = $expectedResult;
554+
}
555+
}
556+
}
557+
self::assertEquals($expectedResult, $result, $message);
558+
}
559+
560+
public function providerXNPV()
561+
{
562+
return require 'data/Calculation/Financial/XNPV.php';
563+
}
564+
517565
/**
518566
* @dataProvider providerPDURATION
519567
*

tests/PhpSpreadsheetTests/Writer/Xlsx/LocaleFloatsTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ protected function setUp(): void
1414
{
1515
$this->currentLocale = setlocale(LC_ALL, '0');
1616

17-
if (!setlocale(LC_ALL, 'fr_FR.UTF-8')) {
17+
if (!setlocale(LC_ALL, 'fr_FR.UTF-8', 'fra_fra')) {
1818
$this->localeAdjusted = false;
1919

2020
return;
@@ -45,6 +45,7 @@ public function testLocaleFloatsCorrectlyConvertedByWriter()
4545

4646
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
4747
$spreadsheet = $reader->load($filename);
48+
unlink($filename);
4849

4950
$result = $spreadsheet->getActiveSheet()->getCell('A1')->getValue();
5051

tests/data/Calculation/Financial/COUPNUM.php

+35
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,39 @@
7373
4,
7474
0,
7575
],
76+
[
77+
16,
78+
'1-Apr-2012',
79+
'31-Mar-2020',
80+
2,
81+
0,
82+
],
83+
[
84+
16,
85+
'1-Apr-2012',
86+
'31-Mar-2020',
87+
2,
88+
1,
89+
],
90+
[
91+
16,
92+
'1-Apr-2012',
93+
'31-Mar-2020',
94+
2,
95+
2,
96+
],
97+
[
98+
16,
99+
'1-Apr-2012',
100+
'31-Mar-2020',
101+
2,
102+
3,
103+
],
104+
[
105+
16,
106+
'1-Apr-2012',
107+
'31-Mar-2020',
108+
2,
109+
4,
110+
],
76111
];

0 commit comments

Comments
 (0)