Final phase of aggregating the MathTrig function implementations into commonly-themed groups

This commit is contained in:
MarkBaker 2021-05-13 21:40:16 +02:00 committed by Mark Baker
parent ac5b96d0f1
commit 71bc7e6f89
10 changed files with 100 additions and 124 deletions

View File

@ -968,7 +968,7 @@ class Calculation
], ],
'EVEN' => [ 'EVEN' => [
'category' => Category::CATEGORY_MATH_AND_TRIG, 'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig\Even::class, 'evaluate'], 'functionCall' => [MathTrig\Round::class, 'even'],
'argumentCount' => '1', 'argumentCount' => '1',
], ],
'EXACT' => [ 'EXACT' => [
@ -1859,7 +1859,7 @@ class Calculation
], ],
'ODD' => [ 'ODD' => [
'category' => Category::CATEGORY_MATH_AND_TRIG, 'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig\Odd::class, 'evaluate'], 'functionCall' => [MathTrig\Round::class, 'odd'],
'argumentCount' => '1', 'argumentCount' => '1',
], ],
'ODDFPRICE' => [ 'ODDFPRICE' => [
@ -2253,12 +2253,12 @@ class Calculation
], ],
'SQRT' => [ 'SQRT' => [
'category' => Category::CATEGORY_MATH_AND_TRIG, 'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig\Sqrt::class, 'evaluate'], 'functionCall' => [MathTrig\Sqrt::class, 'sqrt'],
'argumentCount' => '1', 'argumentCount' => '1',
], ],
'SQRTPI' => [ 'SQRTPI' => [
'category' => Category::CATEGORY_MATH_AND_TRIG, 'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig\SqrtPi::class, 'evaluate'], 'functionCall' => [MathTrig\Sqrt::class, 'pi'],
'argumentCount' => '1', 'argumentCount' => '1',
], ],
'STANDARDIZE' => [ 'STANDARDIZE' => [

View File

@ -148,8 +148,8 @@ class MathTrig
* *
* @Deprecated 1.18.0 * @Deprecated 1.18.0
* *
* @see MathTrig\Even::evaluate() * @see MathTrig\Round::even()
* Use the evaluate() method in the MathTrig\Even class instead * Use the even() method in the MathTrig\Round class instead
* *
* @param float $number Number to round * @param float $number Number to round
* *
@ -157,7 +157,7 @@ class MathTrig
*/ */
public static function EVEN($number) public static function EVEN($number)
{ {
return MathTrig\Even::evaluate($number); return MathTrig\Round::even($number);
} }
/** /**
@ -508,8 +508,8 @@ class MathTrig
* *
* @Deprecated 1.18.0 * @Deprecated 1.18.0
* *
* @See MathTrig\Odd::evaluate() * @See MathTrig\Round::odd()
* Use the evaluate method in the MathTrig\Odd class instead * Use the odd method in the MathTrig\Round class instead
* *
* @param float $number Number to round * @param float $number Number to round
* *
@ -517,7 +517,7 @@ class MathTrig
*/ */
public static function ODD($number) public static function ODD($number)
{ {
return MathTrig\Odd::evaluate($number); return MathTrig\Round::odd($number);
} }
/** /**
@ -726,8 +726,8 @@ class MathTrig
* *
* @Deprecated 1.18.0 * @Deprecated 1.18.0
* *
* @See MathTrig\SqrtPi::evaluate() * @See MathTrig\Sqrt::sqrt()
* Use the evaluate method in the MathTrig\SqrtPi class instead * Use the pi method in the MathTrig\Sqrt class instead
* *
* @param float $number Number * @param float $number Number
* *
@ -735,7 +735,7 @@ class MathTrig
*/ */
public static function SQRTPI($number) public static function SQRTPI($number)
{ {
return MathTrig\SqrtPi::evaluate($number); return MathTrig\Sqrt::pi($number);
} }
/** /**
@ -1448,8 +1448,8 @@ class MathTrig
* *
* @Deprecated 1.18.0 * @Deprecated 1.18.0
* *
* @See MathTrig\Sqrt::evaluate() * @See MathTrig\Sqrt::sqrt()
* Use the evaluate method in the MathTrig\Sqrt class instead * Use the sqrt method in the MathTrig\Sqrt class instead
* *
* @param mixed $number Should be numeric * @param mixed $number Should be numeric
* *
@ -1457,7 +1457,7 @@ class MathTrig
*/ */
public static function builtinSQRT($number) public static function builtinSQRT($number)
{ {
return MathTrig\Sqrt::evaluate($number); return MathTrig\Sqrt::sqrt($number);
} }
/** /**

View File

@ -2,7 +2,7 @@
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig; namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use Exception; use PhpOffice\PhpSpreadsheet\Calculation\Exception;
class Absolute class Absolute
{ {

View File

@ -1,35 +0,0 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use Exception;
class Even
{
/**
* EVEN.
*
* Returns number rounded up to the nearest even integer.
* You can use this function for processing items that come in twos. For example,
* a packing crate accepts rows of one or two items. The crate is full when
* the number of items, rounded up to the nearest two, matches the crate's
* capacity.
*
* Excel Function:
* EVEN(number)
*
* @param float $number Number to round
*
* @return float|string Rounded Number, or a string containing an error
*/
public static function evaluate($number)
{
try {
$number = Helpers::validateNumericNullBool($number);
} catch (Exception $e) {
return $e->getMessage();
}
return Helpers::getEven($number);
}
}

View File

@ -2,7 +2,7 @@
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig; namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use Exception; use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Calculation\Functions;
class Gcd class Gcd

View File

@ -2,7 +2,7 @@
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig; namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use Exception; use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Calculation\Functions;
class Lcm class Lcm

View File

@ -1,38 +0,0 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use Exception;
class Odd
{
/**
* ODD.
*
* Returns number rounded up to the nearest odd integer.
*
* @param float $number Number to round
*
* @return float|string Rounded Number, or a string containing an error
*/
public static function evaluate($number)
{
try {
$number = Helpers::validateNumericNullBool($number);
} catch (Exception $e) {
return $e->getMessage();
}
$significance = Helpers::returnSign($number);
if ($significance == 0) {
return 1;
}
$result = ceil($number / $significance) * $significance;
if ($result == Helpers::getEven($result)) {
$result += $significance;
}
return $result;
}
}

View File

@ -119,4 +119,61 @@ class Round
return Functions::NAN(); return Functions::NAN();
} }
/**
* EVEN.
*
* Returns number rounded up to the nearest even integer.
* You can use this function for processing items that come in twos. For example,
* a packing crate accepts rows of one or two items. The crate is full when
* the number of items, rounded up to the nearest two, matches the crate's
* capacity.
*
* Excel Function:
* EVEN(number)
*
* @param float $number Number to round
*
* @return float|string Rounded Number, or a string containing an error
*/
public static function even($number)
{
try {
$number = Helpers::validateNumericNullBool($number);
} catch (Exception $e) {
return $e->getMessage();
}
return Helpers::getEven($number);
}
/**
* ODD.
*
* Returns number rounded up to the nearest odd integer.
*
* @param float $number Number to round
*
* @return float|string Rounded Number, or a string containing an error
*/
public static function odd($number)
{
try {
$number = Helpers::validateNumericNullBool($number);
} catch (Exception $e) {
return $e->getMessage();
}
$significance = Helpers::returnSign($number);
if ($significance == 0) {
return 1;
}
$result = ceil($number / $significance) * $significance;
if ($result == Helpers::getEven($result)) {
$result += $significance;
}
return $result;
}
} }

View File

@ -2,7 +2,7 @@
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig; namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use Exception; use PhpOffice\PhpSpreadsheet\Calculation\Exception;
class Sqrt class Sqrt
{ {
@ -13,9 +13,9 @@ class Sqrt
* *
* @param mixed $number Should be numeric * @param mixed $number Should be numeric
* *
* @return float|string Rounded number * @return float|string square roor
*/ */
public static function evaluate($number) public static function sqrt($number)
{ {
try { try {
$number = Helpers::validateNumericNullBool($number); $number = Helpers::validateNumericNullBool($number);
@ -25,4 +25,25 @@ class Sqrt
return Helpers::numberOrNan(sqrt($number)); return Helpers::numberOrNan(sqrt($number));
} }
/**
* 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 pi($number)
{
try {
$number = Helpers::validateNumericNullSubstitution($number, 0);
Helpers::validateNotNegative($number);
} catch (Exception $e) {
return $e->getMessage();
}
return sqrt($number * M_PI);
}
} }

View File

@ -1,29 +0,0 @@
<?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);
}
}