Continue MathTrig Breakup - Completion! (#1985)

* Continue MathTrig Breakup - Completion!

Continuing the process of breaking MathTrip.php up into smaller classes. This round takes care of everything that was left:
- ABS
- DEGREES
- EXP
- RADIANS
- SQRT
- SQRTPI
- SUMSQ, SUMX2MY2, SUMX2PY2, SUMXMY2

The only notable logic change was that the 3 SUMX* functions had accepted arrays of unlike length; in that condition, they now return N/A, as Excel does. There had been no tests for this condition.

All the functions in MathTrig.php are now deprecated. Except for COMBIN, the test suite executes them only from MathTrig MovedFunctionsTest. COMBIN is still directly called by some Statistics Binomial functions which have not yet had the opportunity to be re-coded for the new location.


Co-authored-by: Mark Baker <mark@lange.demon.co.uk>
This commit is contained in:
oleibman 2021-04-05 07:39:03 -07:00 committed by GitHub
parent 36c3b5f5d8
commit 95b8c4d59b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
37 changed files with 634 additions and 298 deletions

View File

@ -228,7 +228,7 @@ class Calculation
private static $phpSpreadsheetFunctions = [
'ABS' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'builtinABS'],
'functionCall' => [MathTrig\Absolute::class, 'evaluate'],
'argumentCount' => '1',
],
'ACCRINT' => [
@ -835,7 +835,7 @@ class Calculation
],
'DEGREES' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'builtinDEGREES'],
'functionCall' => [MathTrig\Degrees::class, 'evaluate'],
'argumentCount' => '1',
],
'DELTA' => [
@ -975,7 +975,7 @@ class Calculation
],
'EXP' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'builtinEXP'],
'functionCall' => [MathTrig\Exp::class, 'evaluate'],
'argumentCount' => '1',
],
'EXPONDIST' => [
@ -2038,7 +2038,7 @@ class Calculation
],
'RADIANS' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'builtinRADIANS'],
'functionCall' => [MathTrig\Radians::class, 'evaluate'],
'argumentCount' => '1',
],
'RAND' => [
@ -2185,7 +2185,7 @@ class Calculation
],
'SERIESSUM' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'SERIESSUM'],
'functionCall' => [MathTrig\SeriesSum::class, 'funcSeriesSum'],
'argumentCount' => '4',
],
'SHEET' => [
@ -2205,7 +2205,7 @@ class Calculation
],
'SIN' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'builtinSIN'],
'functionCall' => [MathTrig\Sin::class, 'funcSin'],
'argumentCount' => '1',
],
'SINH' => [
@ -2250,12 +2250,12 @@ class Calculation
],
'SQRT' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'builtinSQRT'],
'functionCall' => [MathTrig\Sqrt::class, 'evaluate'],
'argumentCount' => '1',
],
'SQRTPI' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'SQRTPI'],
'functionCall' => [MathTrig\SqrtPi::class, 'evaluate'],
'argumentCount' => '1',
],
'STANDARDIZE' => [
@ -2331,22 +2331,22 @@ class Calculation
],
'SUMSQ' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'SUMSQ'],
'functionCall' => [MathTrig\SumSquares::class, 'sumSquare'],
'argumentCount' => '1+',
],
'SUMX2MY2' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'SUMX2MY2'],
'functionCall' => [MathTrig\SumSquares::class, 'sumXSquaredMinusYSquared'],
'argumentCount' => '2',
],
'SUMX2PY2' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'SUMX2PY2'],
'functionCall' => [MathTrig\SumSquares::class, 'sumXSquaredPlusYSquared'],
'argumentCount' => '2',
],
'SUMXMY2' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'SUMXMY2'],
'functionCall' => [MathTrig\SumSquares::class, 'sumXMinusYSquared'],
'argumentCount' => '2',
],
'SWITCH' => [

View File

@ -23,7 +23,7 @@ class DateTime
/**
* getDateValue.
*
* @Deprecated 2.0.0 Use the method getDateValueNoThrow in the DateTimeExcel\Helpers class instead
* @Deprecated 2.0.0 Use the method getDateValue in the DateTimeExcel\Helpers class instead
*
* @param mixed $dateValue
*
@ -31,7 +31,11 @@ class DateTime
*/
public static function getDateValue($dateValue)
{
return DateTimeExcel\Helpers::getDateValueNoThrow($dateValue);
try {
return DateTimeExcel\Helpers::getDateValue($dateValue);
} catch (Exception $e) {
return $e->getMessage();
}
}
/**

View File

@ -56,22 +56,6 @@ class Helpers
return (float) $dateValue;
}
/**
* getDateValueNoThrow.
*
* @param mixed $dateValue
*
* @return mixed Excel date/time serial value, or string if error
*/
public static function getDateValueNoThrow($dateValue)
{
try {
return self::getDateValue($dateValue);
} catch (Exception $e) {
return $e->getMessage();
}
}
/**
* getTimeValue.
*

View File

@ -640,23 +640,15 @@ class MathTrig
*
* Returns the square root of (number * pi).
*
* @Deprecated 2.0.0 Use the evaluate method in the MathTrig\SqrtPi class instead
*
* @param float $number Number
*
* @return float|string Square Root of Number * Pi, or a string containing an error
*/
public static function SQRTPI($number)
{
$number = Functions::flattenSingleValue($number);
if (is_numeric($number)) {
if ($number < 0) {
return Functions::NAN();
}
return sqrt($number * M_PI);
}
return Functions::VALUE();
return MathTrig\SqrtPi::evaluate($number);
}
/**
@ -769,107 +761,63 @@ class MathTrig
*
* SUMSQ returns the sum of the squares of the arguments
*
* @Deprecated 2.0.0 Use the sumSquare method in the MathTrig\SumSquares class instead
*
* Excel Function:
* SUMSQ(value1[,value2[, ...]])
*
* @param mixed ...$args Data values
*
* @return float
* @return float|string
*/
public static function SUMSQ(...$args)
{
$returnValue = 0;
// Loop through arguments
foreach (Functions::flattenArray($args) as $arg) {
// Is it a numeric value?
if ((is_numeric($arg)) && (!is_string($arg))) {
$returnValue += ($arg * $arg);
}
}
return $returnValue;
return MathTrig\SumSquares::sumSquare(...$args);
}
/**
* SUMX2MY2.
*
* @Deprecated 2.0.0 Use the sumXSquaredMinusYSquared method in the MathTrig\SumSquares class instead
*
* @param mixed[] $matrixData1 Matrix #1
* @param mixed[] $matrixData2 Matrix #2
*
* @return float
* @return float|string
*/
public static function SUMX2MY2($matrixData1, $matrixData2)
{
$array1 = Functions::flattenArray($matrixData1);
$array2 = Functions::flattenArray($matrixData2);
$count = min(count($array1), count($array2));
$result = 0;
for ($i = 0; $i < $count; ++$i) {
if (
((is_numeric($array1[$i])) && (!is_string($array1[$i]))) &&
((is_numeric($array2[$i])) && (!is_string($array2[$i])))
) {
$result += ($array1[$i] * $array1[$i]) - ($array2[$i] * $array2[$i]);
}
}
return $result;
return MathTrig\SumSquares::sumXSquaredMinusYSquared($matrixData1, $matrixData2);
}
/**
* SUMX2PY2.
*
* @Deprecated 2.0.0 Use the sumXSquaredPlusYSquared method in the MathTrig\SumSquares class instead
*
* @param mixed[] $matrixData1 Matrix #1
* @param mixed[] $matrixData2 Matrix #2
*
* @return float
* @return float|string
*/
public static function SUMX2PY2($matrixData1, $matrixData2)
{
$array1 = Functions::flattenArray($matrixData1);
$array2 = Functions::flattenArray($matrixData2);
$count = min(count($array1), count($array2));
$result = 0;
for ($i = 0; $i < $count; ++$i) {
if (
((is_numeric($array1[$i])) && (!is_string($array1[$i]))) &&
((is_numeric($array2[$i])) && (!is_string($array2[$i])))
) {
$result += ($array1[$i] * $array1[$i]) + ($array2[$i] * $array2[$i]);
}
}
return $result;
return MathTrig\SumSquares::sumXSquaredPlusYSquared($matrixData1, $matrixData2);
}
/**
* SUMXMY2.
*
* @Deprecated 2.0.0 Use the sumXMinusYSquared method in the MathTrig\SumSquares class instead
*
* @param mixed[] $matrixData1 Matrix #1
* @param mixed[] $matrixData2 Matrix #2
*
* @return float
* @return float|string
*/
public static function SUMXMY2($matrixData1, $matrixData2)
{
$array1 = Functions::flattenArray($matrixData1);
$array2 = Functions::flattenArray($matrixData2);
$count = min(count($array1), count($array2));
$result = 0;
for ($i = 0; $i < $count; ++$i) {
if (
((is_numeric($array1[$i])) && (!is_string($array1[$i]))) &&
((is_numeric($array2[$i])) && (!is_string($array2[$i])))
) {
$result += ($array1[$i] - $array2[$i]) * ($array1[$i] - $array2[$i]);
}
}
return $result;
return MathTrig\SumSquares::sumXMinusYSquared($matrixData1, $matrixData2);
}
/**
@ -1057,19 +1005,15 @@ class MathTrig
*
* Returns the result of builtin function abs after validating args.
*
* @Deprecated 2.0.0 Use the evaluate method in the MathTrig\Absolute class instead
*
* @param mixed $number Should be numeric
*
* @return float|int|string Rounded number
*/
public static function builtinABS($number)
{
$number = Functions::flattenSingleValue($number);
if (!is_numeric($number)) {
return Functions::VALUE();
}
return abs($number);
return MathTrig\Absolute::evaluate($number);
}
/**
@ -1205,19 +1149,15 @@ class MathTrig
*
* Returns the result of builtin function rad2deg after validating args.
*
* @Deprecated 2.0.0 Use the evaluate method in the MathTrig\Degrees class instead
*
* @param mixed $number Should be numeric
*
* @return float|string Rounded number
*/
public static function builtinDEGREES($number)
{
$number = Functions::flattenSingleValue($number);
if (!is_numeric($number)) {
return Functions::VALUE();
}
return rad2deg($number);
return MathTrig\Degrees::evaluate($number);
}
/**
@ -1225,19 +1165,15 @@ class MathTrig
*
* Returns the result of builtin function exp after validating args.
*
* @Deprecated 2.0.0 Use the evaluate method in the MathTrig\Exp class instead
*
* @param mixed $number Should be numeric
*
* @return float|string Rounded number
*/
public static function builtinEXP($number)
{
$number = Functions::flattenSingleValue($number);
if (!is_numeric($number)) {
return Functions::VALUE();
}
return exp($number);
return MathTrig\Exp::evaluate($number);
}
/**
@ -1277,19 +1213,15 @@ class MathTrig
*
* Returns the result of builtin function deg2rad after validating args.
*
* @Deprecated 2.0.0 Use the funcSin method in the MathTrig\Sin class instead
*
* @param mixed $number Should be numeric
*
* @return float|string Rounded number
*/
public static function builtinRADIANS($number)
{
$number = Functions::flattenSingleValue($number);
if (!is_numeric($number)) {
return Functions::VALUE();
}
return deg2rad($number);
return MathTrig\Radians::evaluate($number);
}
/**
@ -1329,19 +1261,15 @@ class MathTrig
*
* Returns the result of builtin function sqrt after validating args.
*
* @Deprecated 2.0.0 Use the evaluate method in the MathTrig\Sqrt class instead
*
* @param mixed $number Should be numeric
*
* @return float|string Rounded number
*/
public static function builtinSQRT($number)
{
$number = Functions::flattenSingleValue($number);
if (!is_numeric($number)) {
return Functions::VALUE();
}
return self::numberOrNan(sqrt($number));
return MathTrig\Sqrt::evaluate($number);
}
/**

View File

@ -0,0 +1,28 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use Exception;
class Absolute
{
/**
* ABS.
*
* Returns the result of builtin function abs after validating args.
*
* @param mixed $number Should be numeric
*
* @return float|int|string Rounded number
*/
public static function evaluate($number)
{
try {
$number = Helpers::validateNumericNullBool($number);
} catch (Exception $e) {
return $e->getMessage();
}
return abs($number);
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use Exception;
class Degrees
{
/**
* DEGREES.
*
* Returns the result of builtin function rad2deg after validating args.
*
* @param mixed $number Should be numeric
*
* @return float|string Rounded number
*/
public static function evaluate($number)
{
try {
$number = Helpers::validateNumericNullBool($number);
} catch (Exception $e) {
return $e->getMessage();
}
return rad2deg($number);
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use Exception;
class Exp
{
/**
* EXP.
*
* Returns the result of builtin function exp after validating args.
*
* @param mixed $number Should be numeric
*
* @return float|string Rounded number
*/
public static function evaluate($number)
{
try {
$number = Helpers::validateNumericNullBool($number);
} catch (Exception $e) {
return $e->getMessage();
}
return exp($number);
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use Exception;
class Radians
{
/**
* RADIANS.
*
* Returns the result of builtin function deg2rad after validating args.
*
* @param mixed $number Should be numeric
*
* @return float|string Rounded number
*/
public static function evaluate($number)
{
try {
$number = Helpers::validateNumericNullBool($number);
} catch (Exception $e) {
return $e->getMessage();
}
return deg2rad($number);
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use Exception;
class Sqrt
{
/**
* SQRT.
*
* Returns the result of builtin function sqrt after validating args.
*
* @param mixed $number Should be numeric
*
* @return float|string Rounded number
*/
public static function evaluate($number)
{
try {
$number = Helpers::validateNumericNullBool($number);
} catch (Exception $e) {
return $e->getMessage();
}
return Helpers::numberOrNan(sqrt($number));
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use Exception;
class SqrtPi
{
/**
* SQRTPI.
*
* Returns the square root of (number * pi).
*
* @param float $number Number
*
* @return float|string Square Root of Number * Pi, or a string containing an error
*/
public static function evaluate($number)
{
try {
$number = Helpers::validateNumericNullSubstitution($number, 0);
Helpers::validateNotNegative($number);
} catch (Exception $e) {
return $e->getMessage();
}
return sqrt($number * M_PI);
}
}

View File

@ -0,0 +1,142 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
class SumSquares
{
/**
* SUMSQ.
*
* SUMSQ returns the sum of the squares of the arguments
*
* Excel Function:
* SUMSQ(value1[,value2[, ...]])
*
* @param mixed ...$args Data values
*
* @return float|string
*/
public static function sumSquare(...$args)
{
try {
$returnValue = 0;
// Loop through arguments
foreach (Functions::flattenArray($args) as $arg) {
$arg1 = Helpers::validateNumericNullSubstitution($arg, 0);
$returnValue += ($arg1 * $arg1);
}
} catch (Exception $e) {
return $e->getMessage();
}
return $returnValue;
}
private static function getCount(array $array1, array $array2): int
{
$count = count($array1);
if ($count !== count($array2)) {
throw new Exception(Functions::NA());
}
return $count;
}
/**
* These functions accept only numeric arguments, not even strings which are numeric.
*
* @param mixed $item
*/
private static function numericNotString($item): bool
{
return is_numeric($item) && !is_string($item);
}
/**
* SUMX2MY2.
*
* @param mixed[] $matrixData1 Matrix #1
* @param mixed[] $matrixData2 Matrix #2
*
* @return float|string
*/
public static function sumXSquaredMinusYSquared($matrixData1, $matrixData2)
{
try {
$array1 = Functions::flattenArray($matrixData1);
$array2 = Functions::flattenArray($matrixData2);
$count = self::getCount($array1, $array2);
$result = 0;
for ($i = 0; $i < $count; ++$i) {
if (self::numericNotString($array1[$i]) && self::numericNotString($array2[$i])) {
$result += ($array1[$i] * $array1[$i]) - ($array2[$i] * $array2[$i]);
}
}
} catch (Exception $e) {
return $e->getMessage();
}
return $result;
}
/**
* SUMX2PY2.
*
* @param mixed[] $matrixData1 Matrix #1
* @param mixed[] $matrixData2 Matrix #2
*
* @return float|string
*/
public static function sumXSquaredPlusYSquared($matrixData1, $matrixData2)
{
try {
$array1 = Functions::flattenArray($matrixData1);
$array2 = Functions::flattenArray($matrixData2);
$count = self::getCount($array1, $array2);
$result = 0;
for ($i = 0; $i < $count; ++$i) {
if (self::numericNotString($array1[$i]) && self::numericNotString($array2[$i])) {
$result += ($array1[$i] * $array1[$i]) + ($array2[$i] * $array2[$i]);
}
}
} catch (Exception $e) {
return $e->getMessage();
}
return $result;
}
/**
* SUMXMY2.
*
* @param mixed[] $matrixData1 Matrix #1
* @param mixed[] $matrixData2 Matrix #2
*
* @return float|string
*/
public static function sumXMinusYSquared($matrixData1, $matrixData2)
{
try {
$array1 = Functions::flattenArray($matrixData1);
$array2 = Functions::flattenArray($matrixData2);
$count = self::getCount($array1, $array2);
$result = 0;
for ($i = 0; $i < $count; ++$i) {
if (self::numericNotString($array1[$i]) && self::numericNotString($array2[$i])) {
$result += ($array1[$i] - $array2[$i]) * ($array1[$i] - $array2[$i]);
}
}
} catch (Exception $e) {
return $e->getMessage();
}
return $result;
}
}

View File

@ -17,7 +17,7 @@ class FractionFormatter extends BaseFormatter
$decimalLength = strlen($decimalPart);
$decimalDivisor = 10 ** $decimalLength;
$GCD = MathTrig::GCD($decimalPart, $decimalDivisor);
$GCD = MathTrig\Gcd::evaluate($decimalPart, $decimalDivisor);
$adjustedDecimalPart = $decimalPart / $GCD;
$adjustedDecimalDivisor = $decimalDivisor / $GCD;

View File

@ -2,31 +2,26 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PHPUnit\Framework\TestCase;
class AbsTest extends TestCase
class AbsTest extends AllSetupTeardown
{
/**
* @dataProvider providerAbs
*
* @param mixed $expectedResult
* @param mixed $val
* @param mixed $number
*/
public function testRound($expectedResult, $val = null): void
public function testRound($expectedResult, $number = 'omitted'): void
{
if ($val === null) {
$this->expectException(CalcExp::class);
$formula = '=ABS()';
$sheet = $this->sheet;
$this->mightHaveException($expectedResult);
$this->setCell('A1', $number);
if ($number === 'omitted') {
$sheet->getCell('B1')->setValue('=ABS()');
} else {
$formula = "=ABS($val)";
$sheet->getCell('B1')->setValue('=ABS(A1)');
}
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getCell('A1')->setValue($formula);
$result = $sheet->getCell('A1')->getCalculatedValue();
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
$result = $sheet->getCell('B1')->getCalculatedValue();
self::assertSame($expectedResult, $result);
}
public function providerAbs()

View File

@ -4,6 +4,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcException;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PHPUnit\Framework\TestCase;
@ -49,4 +50,18 @@ class AllSetupTeardown extends TestCase
$this->expectException(CalcException::class);
}
}
/**
* @param mixed $value
*/
protected function setCell(string $cell, $value): void
{
if ($value !== null) {
if (is_string($value) && is_numeric($value)) {
$this->sheet->getCell($cell)->setValueExplicit($value, DataType::TYPE_STRING);
} else {
$this->sheet->getCell($cell)->setValue($value);
}
}
}
}

View File

@ -2,31 +2,26 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PHPUnit\Framework\TestCase;
class DegreesTest extends TestCase
class DegreesTest extends AllSetupTeardown
{
/**
* @dataProvider providerDEGREES
*
* @param mixed $expectedResult
* @param mixed $val
* @param mixed $number
*/
public function testDEGREES($expectedResult, $val = null): void
public function testDegrees($expectedResult, $number = 'omitted'): void
{
if ($val === null) {
$this->expectException(CalcExp::class);
$formula = '=DEGREES()';
$sheet = $this->sheet;
$this->mightHaveException($expectedResult);
$this->setCell('A1', $number);
if ($number === 'omitted') {
$sheet->getCell('B1')->setValue('=DEGREES()');
} else {
$formula = "=DEGREES($val)";
$sheet->getCell('B1')->setValue('=DEGREES(A1)');
}
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getCell('A1')->setValue($formula);
$result = $sheet->getCell('A1')->getCalculatedValue();
self::assertEqualsWithDelta($expectedResult, $result, 1E-6);
$result = $sheet->getCell('B1')->getCalculatedValue();
self::assertEqualsWithDelta($expectedResult, $result, 1E-8);
}
public function providerDegrees()

View File

@ -2,31 +2,28 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PHPUnit\Framework\TestCase;
class ExpTest extends TestCase
class ExpTest extends AllSetupTeardown
{
/**
* @dataProvider providerEXP
*
* @param mixed $expectedResult
* @param mixed $val
* @param mixed $number
*/
public function testEXP($expectedResult, $val = null): void
public function testEXP($expectedResult, $number = 'omitted'): void
{
if ($val === null) {
$this->expectException(CalcExp::class);
$formula = '=EXP()';
} else {
$formula = "=EXP($val)";
$this->mightHaveException($expectedResult);
$sheet = $this->sheet;
if ($number !== null) {
$sheet->getCell('A1')->setValue($number);
}
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getCell('A1')->setValue($formula);
$result = $sheet->getCell('A1')->getCalculatedValue();
self::assertEqualsWithDelta($expectedResult, $result, 1E-6);
if ($number === 'omitted') {
$sheet->getCell('B1')->setValue('=EXP()');
} else {
$sheet->getCell('B1')->setValue('=EXP(A1)');
}
$result = $sheet->getCell('B1')->getCalculatedValue();
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
}
public function providerEXP()

View File

@ -11,9 +11,9 @@ class MInverseTest extends AllSetupTeardown
*
* @param mixed $expectedResult
*/
public function testMINVERSE($expectedResult, ...$args): void
public function testMINVERSE($expectedResult, array $args): void
{
$result = MathTrig::MINVERSE(...$args);
$result = MathTrig\MatrixFunctions::funcMInverse($args);
self::assertEqualsWithDelta($expectedResult, $result, 1E-8);
}

View File

@ -13,7 +13,7 @@ class MMultTest extends AllSetupTeardown
*/
public function testMMULT($expectedResult, ...$args): void
{
$result = MathTrig::MMULT(...$args);
$result = MathTrig\MatrixFunctions::funcMMult(...$args);
self::assertEqualsWithDelta($expectedResult, $result, 1E-8);
}

View File

@ -16,6 +16,7 @@ class MovedFunctionsTest extends TestCase
{
public function testMovedFunctions(): void
{
self::assertSame(1, MathTrig::builtinABS(1));
self::assertEqualsWithDelta(0, MathTrig::builtinACOS(1), 1E-9);
self::assertEqualsWithDelta(0, MathTrig::builtinACOSH(1), 1E-9);
self::assertEqualsWithDelta(3.04192400109863, MathTrig::ACOT(-10), 1E-9);
@ -35,12 +36,15 @@ class MovedFunctionsTest extends TestCase
self::assertEquals('#DIV/0!', MathTrig::COTH(0));
self::assertEquals('#DIV/0!', MathTrig::CSC(0));
self::assertEquals('#DIV/0!', MathTrig::CSCH(0));
self::assertEquals(0, MathTrig::builtinDEGREES(0));
self::assertEquals(6, MathTrig::EVEN(4.5));
self::assertEquals(1, MathTrig::builtinEXP(0));
self::assertEquals(6, MathTrig::FACT(3));
self::assertEquals(105, MathTrig::FACTDOUBLE(7));
self::assertEquals(-6, MathTrig::FLOOR(-4.5, 2));
self::assertEquals(0.23, MathTrig::FLOORMATH(0.234, 0.01));
self::assertEquals(-4, MathTrig::FLOORPRECISE(-2.5, 2));
self::assertEquals(2, MathTrig::GCD(4, 6));
self::assertEquals(-9, MathTrig::INT(-8.3));
self::assertEquals(12, MathTrig::LCM(4, 6));
self::assertEqualswithDelta(2.302585, MathTrig::builtinLN(10), 1E-6);
@ -58,10 +62,12 @@ class MovedFunctionsTest extends TestCase
self::assertEquals(1, MathTrig::MOD(5, 2));
self::assertEquals(6, MathTrig::MROUND(7.3, 3));
self::assertEquals(1, MathTrig::MULTINOMIAL(1));
self::assertEquals(0, MathTrig::numberOrNan(0));
self::assertEquals(5, MathTrig::ODD(4.5));
self::assertEquals(8, MathTrig::POWER(2, 3));
self::assertEquals(8, MathTrig::PRODUCT(1, 2, 4));
self::assertEquals(8, MathTrig::QUOTIENT(17, 2));
self::assertEquals(0, MathTrig::builtinRADIANS(0));
self::assertGreaterThanOrEqual(0, MATHTRIG::RAND());
self::assertEquals('I', MathTrig::ROMAN(1));
self::assertEquals(3.3, MathTrig::builtinROUND(3.27, 1));
@ -73,10 +79,23 @@ class MovedFunctionsTest extends TestCase
self::assertEquals(1, MathTrig::SIGN(79.2));
self::assertEquals(0, MathTrig::builtinSIN(0));
self::assertEquals(0, MathTrig::builtinSINH(0));
self::assertEquals(0, MathTrig::builtinSQRT(0));
self::assertEqualswithDelta(3.54490770181103, MathTrig::SQRTPI(4), 1E-6);
self::assertEquals(0, MathTrig::SUBTOTAL(2, [0, 0]));
self::assertEquals(7, MathTrig::SUM(1, 2, 4));
self::assertEquals(4, MathTrig::SUMIF([[2], [4]], '>2'));
self::assertEquals(2, MathTrig::SUMIFS(
[[1], [1], [1]],
[['Y'], ['Y'], ['N']],
'=Y',
[['H'], ['H'], ['H']],
'=H'
));
self::assertEquals(17, MathTrig::SUMPRODUCT([1, 2, 3], [5, 0, 4]));
self::assertEquals(21, MathTrig::SUMSQ(1, 2, 4));
self::assertEquals(-20, MathTrig::SUMX2MY2([1, 2], [3, 4]));
self::assertEquals(30, MathTrig::SUMX2PY2([1, 2], [3, 4]));
self::assertEquals(8, MathTrig::SUMXMY2([1, 2], [3, 4]));
self::assertEquals(0, MathTrig::builtinTAN(0));
self::assertEquals(0, MathTrig::builtinTANH(0));
self::assertEquals(70, MathTrig::TRUNC(79.2, -1));

View File

@ -2,31 +2,26 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PHPUnit\Framework\TestCase;
class RadiansTest extends TestCase
class RadiansTest extends AllSetupTeardown
{
/**
* @dataProvider providerRADIANS
*
* @param mixed $expectedResult
* @param mixed $val
* @param mixed $number
*/
public function testRADIANS($expectedResult, $val = null): void
public function testRADIANS($expectedResult, $number = 'omitted'): void
{
if ($val === null) {
$this->expectException(CalcExp::class);
$formula = '=RADIANS()';
$sheet = $this->sheet;
$this->mightHaveException($expectedResult);
$this->setCell('A1', $number);
if ($number === 'omitted') {
$sheet->getCell('B1')->setValue('=RADIANS()');
} else {
$formula = "=RADIANS($val)";
$sheet->getCell('B1')->setValue('=RADIANS(A1)');
}
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getCell('A1')->setValue($formula);
$result = $sheet->getCell('A1')->getCalculatedValue();
self::assertEqualsWithDelta($expectedResult, $result, 1E-6);
$result = $sheet->getCell('B1')->getCalculatedValue();
self::assertEqualsWithDelta($expectedResult, $result, 1E-9);
}
public function providerRADIANS()

View File

@ -2,26 +2,27 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use PHPUnit\Framework\TestCase;
class SqrtPiTest extends TestCase
class SqrtPiTest extends AllSetupTeardown
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}
/**
* @dataProvider providerSQRTPI
*
* @param mixed $expectedResult
* @param mixed $value
* @param mixed $number
*/
public function testSQRTPI($expectedResult, $value): void
public function testSQRTPI($expectedResult, $number): void
{
$result = MathTrig::SQRTPI($value);
$this->mightHaveException($expectedResult);
$sheet = $this->sheet;
if ($number !== null) {
$sheet->getCell('A1')->setValue($number);
}
if ($number === 'omitted') {
$sheet->getCell('B1')->setValue('=SQRTPI()');
} else {
$sheet->getCell('B1')->setValue('=SQRTPI(A1)');
}
$result = $sheet->getCell('B1')->getCalculatedValue();
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
}

View File

@ -2,30 +2,25 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PHPUnit\Framework\TestCase;
class SqrtTest extends TestCase
class SqrtTest extends AllSetupTeardown
{
/**
* @dataProvider providerSQRT
*
* @param mixed $expectedResult
* @param mixed $val
* @param mixed $number
*/
public function testSQRT($expectedResult, $val = null): void
public function testSQRT($expectedResult, $number = 'omitted'): void
{
if ($val === null) {
$this->expectException(CalcExp::class);
$formula = '=SQRT()';
$sheet = $this->sheet;
$this->mightHaveException($expectedResult);
$this->setCell('A1', $number);
if ($number === 'omitted') {
$sheet->getCell('B1')->setValue('=SQRT()');
} else {
$formula = "=SQRT($val)";
$sheet->getCell('B1')->setValue('=SQRT(A1)');
}
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getCell('A1')->setValue($formula);
$result = $sheet->getCell('A1')->getCalculatedValue();
$result = $sheet->getCell('B1')->getCalculatedValue();
self::assertEqualsWithDelta($expectedResult, $result, 1E-6);
}

View File

@ -2,17 +2,10 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use PHPUnit\Framework\TestCase;
use PhpOffice\PhpSpreadsheet\Calculation\Statistical;
class SumIfsTest extends TestCase
class SumIfsTest extends AllSetupTeardown
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}
/**
* @dataProvider providerSUMIFS
*
@ -20,7 +13,7 @@ class SumIfsTest extends TestCase
*/
public function testSUMIFS($expectedResult, ...$args): void
{
$result = MathTrig::SUMIFS(...$args);
$result = Statistical\Conditional::SUMIFS(...$args);
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
}

View File

@ -2,17 +2,8 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use PHPUnit\Framework\TestCase;
class SumSqTest extends TestCase
class SumSqTest extends AllSetupTeardown
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}
/**
* @dataProvider providerSUMSQ
*
@ -20,7 +11,19 @@ class SumSqTest extends TestCase
*/
public function testSUMSQ($expectedResult, ...$args): void
{
$result = MathTrig::SUMSQ(...$args);
$this->mightHaveException($expectedResult);
$maxRow = 0;
$funcArg = '';
$sheet = $this->sheet;
foreach ($args as $arg) {
++$maxRow;
$funcArg = "A1:A$maxRow";
if ($arg !== null) {
$sheet->getCell("A$maxRow")->setValue($arg);
}
}
$sheet->getCell('B1')->setValue("=SUMSQ($funcArg)");
$result = $sheet->getCell('B1')->getCalculatedValue();
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
}

View File

@ -3,24 +3,34 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use PHPUnit\Framework\TestCase;
class SumX2MY2Test extends TestCase
class SumX2MY2Test extends AllSetupTeardown
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}
/**
* @dataProvider providerSUMX2MY2
*
* @param mixed $expectedResult
*/
public function testSUMX2MY2($expectedResult, ...$args): void
public function testSUMX2MY2($expectedResult, array $matrixData1, array $matrixData2): void
{
$result = MathTrig::SUMX2MY2(...$args);
$this->mightHaveException($expectedResult);
$sheet = $this->sheet;
$maxRow = 0;
$funcArg1 = '';
foreach (Functions::flattenArray($matrixData1) as $arg) {
++$maxRow;
$funcArg1 = "A1:A$maxRow";
$this->setCell("A$maxRow", $arg);
}
$maxRow = 0;
$funcArg2 = '';
foreach (Functions::flattenArray($matrixData2) as $arg) {
++$maxRow;
$funcArg2 = "C1:C$maxRow";
$this->setCell("C$maxRow", $arg);
}
$sheet->getCell('B1')->setValue("=SUMX2MY2($funcArg1, $funcArg2)");
$result = $sheet->getCell('B1')->getCalculatedValue();
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
}

View File

@ -3,24 +3,34 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use PHPUnit\Framework\TestCase;
class SumX2PY2Test extends TestCase
class SumX2PY2Test extends AllSetupTeardown
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}
/**
* @dataProvider providerSUMX2PY2
*
* @param mixed $expectedResult
*/
public function testSUMX2PY2($expectedResult, ...$args): void
public function testSUMX2PY2($expectedResult, array $matrixData1, array $matrixData2): void
{
$result = MathTrig::SUMX2PY2(...$args);
$this->mightHaveException($expectedResult);
$sheet = $this->sheet;
$maxRow = 0;
$funcArg1 = '';
foreach (Functions::flattenArray($matrixData1) as $arg) {
++$maxRow;
$funcArg1 = "A1:A$maxRow";
$this->setCell("A$maxRow", $arg);
}
$maxRow = 0;
$funcArg2 = '';
foreach (Functions::flattenArray($matrixData2) as $arg) {
++$maxRow;
$funcArg2 = "C1:C$maxRow";
$this->setCell("C$maxRow", $arg);
}
$sheet->getCell('B1')->setValue("=SUMX2PY2($funcArg1, $funcArg2)");
$result = $sheet->getCell('B1')->getCalculatedValue();
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
}

View File

@ -3,24 +3,34 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use PHPUnit\Framework\TestCase;
class SumXMY2Test extends TestCase
class SumXMY2Test extends AllSetupTeardown
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}
/**
* @dataProvider providerSUMXMY2
*
* @param mixed $expectedResult
*/
public function testSUMXMY2($expectedResult, ...$args): void
public function testSUMXMY2($expectedResult, array $matrixData1, array $matrixData2): void
{
$result = MathTrig::SUMXMY2(...$args);
$this->mightHaveException($expectedResult);
$sheet = $this->sheet;
$maxRow = 0;
$funcArg1 = '';
foreach (Functions::flattenArray($matrixData1) as $arg) {
++$maxRow;
$funcArg1 = "A1:A$maxRow";
$this->setCell("A$maxRow", $arg);
}
$maxRow = 0;
$funcArg2 = '';
foreach (Functions::flattenArray($matrixData2) as $arg) {
++$maxRow;
$funcArg2 = "C1:C$maxRow";
$this->setCell("C$maxRow", $arg);
}
$sheet->getCell('B1')->setValue("=SUMXMY2($funcArg1, $funcArg2)");
$result = $sheet->getCell('B1')->getCalculatedValue();
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
}

View File

@ -1,12 +1,18 @@
<?php
return [
['#VALUE!'], // exception - not enough args
['#VALUE!', '"ABC"'],
[35.51, '"35.51"'],
['exception'], // exception - not enough args
['#VALUE!', 'ABC'],
[35.51, '35.51'],
[35.51, '=35.51'],
[35.51, '="35.51"'],
[35.51, 35.51],
[35.51, -35.51],
[6, '"6"'],
[7, '"-7"'],
[6, '6'],
[7, '-7'],
[0, 0],
[0, null],
[0, false],
[1, true],
['#VALUE!', ''],
];

View File

@ -1,7 +1,12 @@
<?php
return [
['#VALUE!'], // exception not enough args
['#VALUE!', '"ABC"'],
['exception'], // exception not enough args
['#VALUE!', 'ABC'],
[45, M_PI / 4],
[0, 0],
[0, null],
[0, false],
['#VALUE!', ''],
[57.29577951, true],
];

View File

@ -1,7 +1,11 @@
<?php
return [
['#VALUE!'], // exception not enough args
['#VALUE!', '"ABC"'],
[2.718282, 1],
['exception'], // exception not enough args
['#VALUE!', 'ABC'],
[2.7182818284590, 1],
[2.7182818284590, true],
[1, false],
[1, null],
['#VALUE!', ''],
];

View File

@ -1,7 +1,14 @@
<?php
return [
['#VALUE!'], // exception not enough args
['#VALUE!', '"ABC"'],
['exception'], // exception not enough args
['#VALUE!', 'ABC'],
[M_PI / 4, 45],
[0, 0],
[0, null],
[0, false],
['#VALUE!', ''],
['#VALUE!', '=15+""'],
[0, '=15+"-15"'],
[0.017453293, true],
];

View File

@ -1,10 +1,16 @@
<?php
return [
['#VALUE!'], // exception not enough args
['#VALUE!', '"ABC"'],
['exception'], // exception not enough args
['#VALUE!', 'ABC'],
[0, 0],
[0, null],
[0, false],
['#VALUE!', ''],
[1, true],
[1.5, 2.25],
[1.5, '2.25'],
[1.5, '="2.25"'],
[1.772454, M_PI],
['#NUM!', -2.1],
];

View File

@ -61,4 +61,8 @@ return [
3.9633272976060101,
5,
],
['#VALUE!', true],
['#VALUE!', false],
['0', null],
['exception', 'omitted'],
];

View File

@ -44,4 +44,13 @@ return [
2,
3,
],
[14, 1, '2', 3],
[14, 1, '=2', 3],
[14, 1, '="2"', 3],
['#VALUE!', 1, 'X', 3],
['#VALUE!', 1, '', 3],
['#VALUE!', false, 2, 3],
['#VALUE!', 1, 2, true],
[5, 1, 2, null],
[10, 1, null, 3, null],
];

View File

@ -16,4 +16,14 @@ return [
[[1, 2], [3, 4]],
[[5, 6], [7, 8]],
],
[-20, [1, 2], [3, 4]],
[-20, [1, '=2'], [3, 4]],
[-8, [1, '2'], [3, 4]],
[-8, [1, '="2"'], [3, 4]],
[-8, [1, 'X'], [3, 4]],
[-8, [1, false], [3, 4]],
[-12, [1, 2], ['', 4]],
[-12, [1, 2], [null, 4]],
[-12, [1, 2], [true, 4]],
['#N/A', [1, 2], [3, 4, 5]], // different dimensions
];

View File

@ -16,4 +16,14 @@ return [
[[1, 2], [3, 4]],
[[5, 6], [7, 8]],
],
[30, [1, 2], [3, 4]],
[30, [1, '=2'], [3, 4]],
[10, [1, ''], [3, 4]],
[10, [1, '2'], [3, 4]],
[10, [1, '="2"'], [3, 4]],
[10, [1, 'X'], [3, 4]],
[10, [1, false], [3, 4]],
[20, [1, 2], [null, 4]],
[20, [1, 2], [true, 4]],
['#N/A', [1, 2], [3, 4, 5]], // different dimensions
];

View File

@ -16,4 +16,14 @@ return [
[[1, 2], [3, 4]],
[[5, 6], [7, 8]],
],
[8, [1, 2], [3, 4]],
[8, [1, '=2'], [3, 4]],
[4, [1, ''], [3, 4]],
[4, [1, '2'], [3, 4]],
[4, [1, '="2"'], [3, 4]],
[4, [1, 'X'], [3, 4]],
[4, [1, false], [3, 4]],
[4, [1, 2], [null, 4]],
[4, [1, 2], [true, 4]],
['#N/A', [1, 2], [3, 4, 5]], // different dimensions
];