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.
2020-05-17 10:50:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// result, message, rate, values, dates
|
|
|
|
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'#VALUE!',
|
|
|
|
'If rate is not numeric, returns the #VALUE! error value',
|
|
|
|
'xyz',
|
|
|
|
[0, 120000, 120000, 120000, 120000, 120000, 120000, 120000, 120000, 120000, 120000],
|
|
|
|
['2018-06-30', '2018-12-31', '2019-12-31', '2020-12-31', '2021-12-31', '2022-12-31', '2023-12-31', '2024-12-31', '2025-12-31', '2026-12-31', '2027-12-31'],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
1000.0,
|
|
|
|
'Okay to specify values and dates as non-array',
|
|
|
|
0.10,
|
|
|
|
1000.0,
|
|
|
|
'2018-06-30',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'#NUM!',
|
|
|
|
'If different number of elements in values and dates, return NUM',
|
|
|
|
0.10,
|
|
|
|
[1000.0, 1000.1],
|
|
|
|
'2018-06-30',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'#NUM!',
|
|
|
|
'If minimum value > 0, return NUM',
|
|
|
|
0.10,
|
|
|
|
[1000.0, 1000.1],
|
|
|
|
['2018-06-30', '2018-07-30'],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'#NUM!',
|
|
|
|
'If maximum value < 0, return NUM',
|
|
|
|
0.10,
|
|
|
|
[-1000.0, -1000.1],
|
|
|
|
['2018-06-30', '2018-07-30'],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'#VALUE!',
|
|
|
|
'If any value is non-numeric, return VALUE',
|
|
|
|
0.10,
|
2020-05-18 04:49:57 +00:00
|
|
|
[-1000.0, 1000.1, 'x'],
|
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.
2020-05-17 10:50:01 +00:00
|
|
|
['2018-06-30', '2018-07-30', '2018-08-30'],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'#VALUE!',
|
|
|
|
'If first date is non-numeric, return VALUE',
|
|
|
|
0.10,
|
|
|
|
[-1000.0, 1000.1, 1000.2],
|
|
|
|
['2018-06x30', '2018-07-30', '2018-08-30'],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'#VALUE!',
|
|
|
|
'If any other date is non-numeric, return VALUE',
|
|
|
|
0.10,
|
|
|
|
[-1000.0, 1000.1, 1000.2],
|
|
|
|
['2018-06-30', '2018-07-30', '2018-08z30'],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'#NUM!',
|
|
|
|
'If any date is before first date, return NUM',
|
|
|
|
0.10,
|
|
|
|
[-1000.0, 1000.1, 1000.2],
|
|
|
|
['2018-06-30', '2018-07-30', '2018-05-30'],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
772830.734,
|
|
|
|
'XNPV calculation #1 is incorrect',
|
|
|
|
0.10,
|
|
|
|
[0, 120000, 120000, 120000, 120000, 120000, 120000, 120000, 120000, 120000, 120000],
|
|
|
|
['2018-06-30', '2018-12-31', '2019-12-31', '2020-12-31', '2021-12-31', '2022-12-31', '2023-12-31', '2024-12-31', '2025-12-31', '2026-12-31', '2027-12-31'],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
22.257507852701,
|
|
|
|
'Gnumeric gets this right, Excel returns #NUM, Libre returns incorrect result',
|
|
|
|
-0.10,
|
|
|
|
[-100.0, 110.0],
|
|
|
|
['2019-12-31', '2020-12-31'],
|
|
|
|
],
|
|
|
|
];
|