Skip to content

Commit

Permalink
ROUND Accepts null, false, and true as First Parameter (#1837)
Browse files Browse the repository at this point in the history
* ROUND Accepts null, false, and true as First Parameter

Issue #1789 was addressed by PR #1799. In a follow-up discussion,
it came to light that ROUND was not handling the unexpected case where the
first parameter is an empty cell in the same manner that Excel does.
Subsequent investigation showed that a boolean first parameter is permitted.
I broadened my investigation to include the following related functions.
- ROUNDUP
- ROUNDDOWN
- MROUND
- TRUNC
- INT
- FLOOR
- FLOOR.MATH
- FLOOR.PRECISE
- CEILING
- CEILING.MATH
- CEILING.PRECISE

All of these allow a NULL first parameter, and all except MROUND allow boolean.
For completeness, I will note that all treat null string as invalid.
I suspect there are other functions which permit
similarly unexpected parameters, but I consider them out of scope for this PR.

CEILING.MATH and CEILING.PRECISE were unimplemented, and are now supported
as part of this PR.

The tests for each of these functions have been re-coded, though all the original
test data is still included in the test cases, plus several new cases for each.
The new tests now take place as a user would invoke the functions,
through a spreadsheet cell rather than a
direct call to the appropriate function within Calculation/MathTrig.
Aside from being more realistic, the new tests are also more complete.
For example, FLOOR.MATH can take from 1-3 arguments, and the existing tests
confirmed that the function in Calculation could handle a single argument.
However, the function list in Calculation.php erroneously set the number of
arguments for FLOOR.MATH to exactly 3, so, if a user tried to get the calculated
result of a cell containing FLOOR.MATH(1.2), the result would be an Exception.

Aside from the parameter support, there are a few minor code changes.
Ods, as well as Gnumeric, allows the omission of the second parameter for
FLOAT and CEILING; Excel does not. A potential divide-by-zero error is
avoided in CEILING, FLOOR, and FLOORMATH.

I will note that it would probably be beneficial in terms of maintainability
to break MathTrig up into many individual modules. The same would hold for the
other Calculation modules. I would be willing to look into this if you agree
that it would be worthwhile.
  • Loading branch information
oleibman committed Feb 13, 2021
1 parent c54e3e9 commit cabcfaa
Show file tree
Hide file tree
Showing 42 changed files with 1,352 additions and 1,032 deletions.
36 changes: 18 additions & 18 deletions src/PhpSpreadsheet/Calculation/Calculation.php
Expand Up @@ -463,18 +463,18 @@ class Calculation
],
'CEILING' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'CEILING'],
'argumentCount' => '2',
'functionCall' => [MathTrig\Ceiling::class, 'funcCeiling'],
'argumentCount' => '1-2', // 2 for Excel, 1-2 for Ods/Gnumeric
],
'CEILING.MATH' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [Functions::class, 'DUMMY'],
'argumentCount' => '3',
'functionCall' => [MathTrig\CeilingMath::class, 'funcCeilingMath'],
'argumentCount' => '1-3',
],
'CEILING.PRECISE' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [Functions::class, 'DUMMY'],
'argumentCount' => '2',
'functionCall' => [MathTrig\CeilingPrecise::class, 'funcCeilingPrecise'],
'argumentCount' => '1,2',
],
'CELL' => [
'category' => Category::CATEGORY_INFORMATION,
Expand Down Expand Up @@ -1069,18 +1069,18 @@ class Calculation
],
'FLOOR' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'FLOOR'],
'argumentCount' => '2',
'functionCall' => [MathTrig\Floor::class, 'funcFloor'],
'argumentCount' => '1-2', // Excel requries 2, Ods/Gnumeric 1-2
],
'FLOOR.MATH' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'FLOORMATH'],
'argumentCount' => '3',
'functionCall' => [MathTrig\FloorMath::class, 'funcFloorMath'],
'argumentCount' => '1-3',
],
'FLOOR.PRECISE' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'FLOORPRECISE'],
'argumentCount' => '2',
'functionCall' => [MathTrig\FloorPrecise::class, 'funcFloorPrecise'],
'argumentCount' => '1-2',
],
'FORECAST' => [
'category' => Category::CATEGORY_STATISTICAL,
Expand Down Expand Up @@ -1418,7 +1418,7 @@ class Calculation
],
'INT' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'INT'],
'functionCall' => [MathTrig\IntClass::class, 'funcInt'],
'argumentCount' => '1',
],
'INTERCEPT' => [
Expand Down Expand Up @@ -1725,7 +1725,7 @@ class Calculation
],
'MROUND' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'MROUND'],
'functionCall' => [MathTrig\Mround::class, 'funcMround'],
'argumentCount' => '2',
],
'MULTINOMIAL' => [
Expand Down Expand Up @@ -2112,17 +2112,17 @@ class Calculation
],
'ROUND' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'builtinROUND'],
'functionCall' => [MathTrig\Round::class, 'builtinROUND'],
'argumentCount' => '2',
],
'ROUNDDOWN' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'ROUNDDOWN'],
'functionCall' => [MathTrig\RoundDown::class, 'funcRoundDown'],
'argumentCount' => '2',
],
'ROUNDUP' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'ROUNDUP'],
'functionCall' => [MathTrig\RoundUp::class, 'funcRoundUp'],
'argumentCount' => '2',
],
'ROW' => [
Expand Down Expand Up @@ -2474,7 +2474,7 @@ class Calculation
],
'TRUNC' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'TRUNC'],
'functionCall' => [MathTrig\Trunc::class, 'funcTrunc'],
'argumentCount' => '1,2',
],
'TTEST' => [
Expand Down

0 comments on commit cabcfaa

Please sign in to comment.