Some initial grouping of the refactored MathTrig functions into small group classes, and renaming of methods

This commit is contained in:
MarkBaker 2021-05-12 18:01:48 +02:00 committed by Mark Baker
parent 4b82920120
commit 22468a7965
8 changed files with 193 additions and 227 deletions

View File

@ -466,17 +466,17 @@ class Calculation
], ],
'CEILING' => [ 'CEILING' => [
'category' => Category::CATEGORY_MATH_AND_TRIG, 'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig\Ceiling::class, 'evaluate'], 'functionCall' => [MathTrig\Ceiling::class, 'ceiling'],
'argumentCount' => '1-2', // 2 for Excel, 1-2 for Ods/Gnumeric 'argumentCount' => '1-2', // 2 for Excel, 1-2 for Ods/Gnumeric
], ],
'CEILING.MATH' => [ 'CEILING.MATH' => [
'category' => Category::CATEGORY_MATH_AND_TRIG, 'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig\CeilingMath::class, 'evaluate'], 'functionCall' => [MathTrig\Ceiling::class, 'math'],
'argumentCount' => '1-3', 'argumentCount' => '1-3',
], ],
'CEILING.PRECISE' => [ 'CEILING.PRECISE' => [
'category' => Category::CATEGORY_MATH_AND_TRIG, 'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig\CeilingPrecise::class, 'evaluate'], 'functionCall' => [MathTrig\Ceiling::class, 'precise'],
'argumentCount' => '1,2', 'argumentCount' => '1,2',
], ],
'CELL' => [ 'CELL' => [
@ -1073,17 +1073,17 @@ class Calculation
], ],
'FLOOR' => [ 'FLOOR' => [
'category' => Category::CATEGORY_MATH_AND_TRIG, 'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig\Floor::class, 'evaluate'], 'functionCall' => [MathTrig\Floor::class, 'floor'],
'argumentCount' => '1-2', // Excel requries 2, Ods/Gnumeric 1-2 'argumentCount' => '1-2', // Excel requries 2, Ods/Gnumeric 1-2
], ],
'FLOOR.MATH' => [ 'FLOOR.MATH' => [
'category' => Category::CATEGORY_MATH_AND_TRIG, 'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig\FloorMath::class, 'evaluate'], 'functionCall' => [MathTrig\Floor::class, 'math'],
'argumentCount' => '1-3', 'argumentCount' => '1-3',
], ],
'FLOOR.PRECISE' => [ 'FLOOR.PRECISE' => [
'category' => Category::CATEGORY_MATH_AND_TRIG, 'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig\FloorPrecise::class, 'evaluate'], 'functionCall' => [MathTrig\Floor::class, 'precise'],
'argumentCount' => '1-2', 'argumentCount' => '1-2',
], ],
'FORECAST' => [ 'FORECAST' => [

View File

@ -97,17 +97,17 @@ class MathTrig
* *
* @Deprecated 1.17.0 * @Deprecated 1.17.0
* *
* @see MathTrig\Ceiling::evaluate()
* Use the evaluate() method in the MathTrig\Ceiling class instead
*
* @param float $number the number you want to round * @param float $number the number you want to round
* @param float $significance the multiple to which you want to round * @param float $significance the multiple to which you want to round
* *
* @return float|string Rounded Number, or a string containing an error * @return float|string Rounded Number, or a string containing an error
*
* @see MathTrig\Ceiling::ceiling()
* Use the ceiling() method in the MathTrig\Ceiling class instead
*/ */
public static function CEILING($number, $significance = null) public static function CEILING($number, $significance = null)
{ {
return MathTrig\Ceiling::evaluate($number, $significance); return MathTrig\Ceiling::ceiling($number, $significance);
} }
/** /**
@ -228,17 +228,17 @@ class MathTrig
* *
* @Deprecated 1.17.0 * @Deprecated 1.17.0
* *
* @see MathTrig\Floor::evaluate()
* Use the evaluate() method in the MathTrig\Floor class instead
*
* @param float $number Number to round * @param float $number Number to round
* @param float $significance Significance * @param float $significance Significance
* *
* @return float|string Rounded Number, or a string containing an error * @return float|string Rounded Number, or a string containing an error
*
*@see MathTrig\Floor::floor()
* Use the evaluate() method in the MathTrig\Floor class instead
*/ */
public static function FLOOR($number, $significance = null) public static function FLOOR($number, $significance = null)
{ {
return MathTrig\Floor::evaluate($number, $significance); return MathTrig\Floor::floor($number, $significance);
} }
/** /**
@ -251,18 +251,18 @@ class MathTrig
* *
* @Deprecated 1.17.0 * @Deprecated 1.17.0
* *
* @see MathTrig\FloorMath::evaluate()
* Use the evaluate() method in the MathTrig\FloorMath class instead
*
* @param float $number Number to round * @param float $number Number to round
* @param float $significance Significance * @param float $significance Significance
* @param int $mode direction to round negative numbers * @param int $mode direction to round negative numbers
* *
* @return float|string Rounded Number, or a string containing an error * @return float|string Rounded Number, or a string containing an error
*
*@see MathTrig\Floor::math()
* Use the math() method in the MathTrig\Floor class instead
*/ */
public static function FLOORMATH($number, $significance = null, $mode = 0) public static function FLOORMATH($number, $significance = null, $mode = 0)
{ {
return MathTrig\FloorMath::evaluate($number, $significance, $mode); return MathTrig\Floor::math($number, $significance, $mode);
} }
/** /**
@ -275,17 +275,17 @@ class MathTrig
* *
* @Deprecated 1.17.0 * @Deprecated 1.17.0
* *
* @see MathTrig\FloorPrecise::evaluate()
* Use the evaluate() method in the MathTrig\FloorPrecise class instead
*
* @param float $number Number to round * @param float $number Number to round
* @param float $significance Significance * @param float $significance Significance
* *
* @return float|string Rounded Number, or a string containing an error * @return float|string Rounded Number, or a string containing an error
*
*@see MathTrig\Floor::precise()
* Use the precise() method in the MathTrig\Floor class instead
*/ */
public static function FLOORPRECISE($number, $significance = 1) public static function FLOORPRECISE($number, $significance = 1)
{ {
return MathTrig\FloorPrecise::evaluate($number, $significance); return MathTrig\Floor::precise($number, $significance);
} }
/** /**

View File

@ -23,7 +23,7 @@ class Ceiling
* *
* @return float|string Rounded Number, or a string containing an error * @return float|string Rounded Number, or a string containing an error
*/ */
public static function evaluate($number, $significance = null) public static function ceiling($number, $significance = null)
{ {
if ($significance === null) { if ($significance === null) {
self::floorCheck1Arg(); self::floorCheck1Arg();
@ -39,6 +39,78 @@ class Ceiling
return self::argumentsOk((float) $number, (float) $significance); return self::argumentsOk((float) $number, (float) $significance);
} }
/**
* CEILING.MATH.
*
* Round a number down to the nearest integer or to the nearest multiple of significance.
*
* Excel Function:
* CEILING.MATH(number[,significance[,mode]])
*
* @param mixed $number Number to round
* @param mixed $significance Significance
* @param int $mode direction to round negative numbers
*
* @return float|string Rounded Number, or a string containing an error
*/
public static function math($number, $significance = null, $mode = 0)
{
try {
$number = Helpers::validateNumericNullBool($number);
$significance = Helpers::validateNumericNullSubstitution($significance, ($number < 0) ? -1 : 1);
$mode = Helpers::validateNumericNullSubstitution($mode, null);
} catch (Exception $e) {
return $e->getMessage();
}
if (empty($significance * $number)) {
return 0.0;
}
if (self::ceilingMathTest((float) $significance, (float) $number, (int) $mode)) {
return floor($number / $significance) * $significance;
}
return ceil($number / $significance) * $significance;
}
/**
* CEILING.PRECISE.
*
* Rounds number up, away from zero, to the nearest multiple of significance.
*
* Excel Function:
* CEILING.PRECISE(number[,significance])
*
* @param mixed $number the number you want to round
* @param float $significance the multiple to which you want to round
*
* @return float|string Rounded Number, or a string containing an error
*/
public static function precise($number, $significance = 1)
{
try {
$number = Helpers::validateNumericNullBool($number);
$significance = Helpers::validateNumericNullSubstitution($significance, null);
} catch (Exception $e) {
return $e->getMessage();
}
if (!$significance) {
return 0.0;
}
$result = $number / abs($significance);
return ceil($result) * $significance * (($significance < 0) ? -1 : 1);
}
/**
* Let CEILINGMATH complexity pass Scrutinizer.
*/
private static function ceilingMathTest(float $significance, float $number, int $mode): bool
{
return ((float) $significance < 0) || ((float) $number < 0 && !empty($mode));
}
/** /**
* Avoid Scrutinizer problems concerning complexity. * Avoid Scrutinizer problems concerning complexity.
* *

View File

@ -1,50 +0,0 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use Exception;
class CeilingMath
{
/**
* CEILING.MATH.
*
* Round a number down to the nearest integer or to the nearest multiple of significance.
*
* Excel Function:
* CEILING.MATH(number[,significance[,mode]])
*
* @param mixed $number Number to round
* @param mixed $significance Significance
* @param int $mode direction to round negative numbers
*
* @return float|string Rounded Number, or a string containing an error
*/
public static function evaluate($number, $significance = null, $mode = 0)
{
try {
$number = Helpers::validateNumericNullBool($number);
$significance = Helpers::validateNumericNullSubstitution($significance, ($number < 0) ? -1 : 1);
$mode = Helpers::validateNumericNullSubstitution($mode, null);
} catch (Exception $e) {
return $e->getMessage();
}
if (empty($significance * $number)) {
return 0.0;
}
if (self::ceilingMathTest((float) $significance, (float) $number, (int) $mode)) {
return floor($number / $significance) * $significance;
}
return ceil($number / $significance) * $significance;
}
/**
* Let CEILINGMATH complexity pass Scrutinizer.
*/
private static function ceilingMathTest(float $significance, float $number, int $mode): bool
{
return ((float) $significance < 0) || ((float) $number < 0 && !empty($mode));
}
}

View File

@ -1,38 +0,0 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use Exception;
class CeilingPrecise
{
/**
* CEILING.PRECISE.
*
* Rounds number up, away from zero, to the nearest multiple of significance.
*
* Excel Function:
* CEILING.PRECISE(number[,significance])
*
* @param mixed $number the number you want to round
* @param float $significance the multiple to which you want to round
*
* @return float|string Rounded Number, or a string containing an error
*/
public static function evaluate($number, $significance = 1)
{
try {
$number = Helpers::validateNumericNullBool($number);
$significance = Helpers::validateNumericNullSubstitution($significance, null);
} catch (Exception $e) {
return $e->getMessage();
}
if (!$significance) {
return 0.0;
}
$result = $number / abs($significance);
return ceil($result) * $significance * (($significance < 0) ? -1 : 1);
}
}

View File

@ -28,7 +28,7 @@ class Floor
* *
* @return float|string Rounded Number, or a string containing an error * @return float|string Rounded Number, or a string containing an error
*/ */
public static function evaluate($number, $significance = null) public static function floor($number, $significance = null)
{ {
if ($significance === null) { if ($significance === null) {
self::floorCheck1Arg(); self::floorCheck1Arg();
@ -44,6 +44,103 @@ class Floor
return self::argumentsOk((float) $number, (float) $significance); return self::argumentsOk((float) $number, (float) $significance);
} }
/**
* FLOOR.MATH.
*
* Round a number down to the nearest integer or to the nearest multiple of significance.
*
* Excel Function:
* FLOOR.MATH(number[,significance[,mode]])
*
* @param mixed $number Number to round
* @param mixed $significance Significance
* @param mixed $mode direction to round negative numbers
*
* @return float|string Rounded Number, or a string containing an error
*/
public static function math($number, $significance = null, $mode = 0)
{
try {
$number = Helpers::validateNumericNullBool($number);
$significance = Helpers::validateNumericNullSubstitution($significance, ($number < 0) ? -1 : 1);
$mode = Helpers::validateNumericNullSubstitution($mode, null);
} catch (Exception $e) {
return $e->getMessage();
}
return self::argsOk((float) $number, (float) $significance, (int) $mode);
}
/**
* FLOOR.PRECISE.
*
* Rounds number down, toward zero, to the nearest multiple of significance.
*
* Excel Function:
* FLOOR.PRECISE(number[,significance])
*
* @param float $number Number to round
* @param float $significance Significance
*
* @return float|string Rounded Number, or a string containing an error
*/
public static function precise($number, $significance = 1)
{
try {
$number = Helpers::validateNumericNullBool($number);
$significance = Helpers::validateNumericNullSubstitution($significance, null);
} catch (Exception $e) {
return $e->getMessage();
}
return self::argumentsOkPrecise((float) $number, (float) $significance);
}
/**
* Avoid Scrutinizer problems concerning complexity.
*
* @return float|string
*/
private static function argumentsOkPrecise(float $number, float $significance)
{
if ($significance == 0.0) {
return Functions::DIV0();
}
if ($number == 0.0) {
return 0.0;
}
return floor($number / abs($significance)) * abs($significance);
}
/**
* Avoid Scrutinizer complexity problems.
*
* @return float|string Rounded Number, or a string containing an error
*/
private static function argsOk(float $number, float $significance, int $mode)
{
if (!$significance) {
return Functions::DIV0();
}
if (!$number) {
return 0.0;
}
if (self::floorMathTest($number, $significance, $mode)) {
return ceil($number / $significance) * $significance;
}
return floor($number / $significance) * $significance;
}
/**
* Let FLOORMATH complexity pass Scrutinizer.
*/
private static function floorMathTest(float $number, float $significance, int $mode): bool
{
return Helpers::returnSign($significance) == -1 || (Helpers::returnSign($number) == -1 && !empty($mode));
}
/** /**
* Avoid Scrutinizer problems concerning complexity. * Avoid Scrutinizer problems concerning complexity.
* *

View File

@ -1,64 +0,0 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
class FloorMath
{
/**
* FLOOR.MATH.
*
* Round a number down to the nearest integer or to the nearest multiple of significance.
*
* Excel Function:
* FLOOR.MATH(number[,significance[,mode]])
*
* @param mixed $number Number to round
* @param mixed $significance Significance
* @param mixed $mode direction to round negative numbers
*
* @return float|string Rounded Number, or a string containing an error
*/
public static function evaluate($number, $significance = null, $mode = 0)
{
try {
$number = Helpers::validateNumericNullBool($number);
$significance = Helpers::validateNumericNullSubstitution($significance, ($number < 0) ? -1 : 1);
$mode = Helpers::validateNumericNullSubstitution($mode, null);
} catch (Exception $e) {
return $e->getMessage();
}
return self::argsOk((float) $number, (float) $significance, (int) $mode);
}
/**
* Avoid Scrutinizer complexity problems.
*
* @return float|string Rounded Number, or a string containing an error
*/
private static function argsOk(float $number, float $significance, int $mode)
{
if (!$significance) {
return Functions::DIV0();
}
if (!$number) {
return 0.0;
}
if (self::floorMathTest($number, $significance, $mode)) {
return ceil($number / $significance) * $significance;
}
return floor($number / $significance) * $significance;
}
/**
* Let FLOORMATH complexity pass Scrutinizer.
*/
private static function floorMathTest(float $number, float $significance, int $mode): bool
{
return Helpers::returnSign($significance) == -1 || (Helpers::returnSign($number) == -1 && !empty($mode));
}
}

View File

@ -1,51 +0,0 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
class FloorPrecise
{
/**
* FLOOR.PRECISE.
*
* Rounds number down, toward zero, to the nearest multiple of significance.
*
* Excel Function:
* FLOOR.PRECISE(number[,significance])
*
* @param float $number Number to round
* @param float $significance Significance
*
* @return float|string Rounded Number, or a string containing an error
*/
public static function evaluate($number, $significance = 1)
{
try {
$number = Helpers::validateNumericNullBool($number);
$significance = Helpers::validateNumericNullSubstitution($significance, null);
} catch (Exception $e) {
return $e->getMessage();
}
return self::argumentsOk((float) $number, (float) $significance);
}
/**
* Avoid Scrutinizer problems concerning complexity.
*
* @return float|string
*/
private static function argumentsOk(float $number, float $significance)
{
if ($significance == 0.0) {
return Functions::DIV0();
}
if ($number == 0.0) {
return 0.0;
}
return floor($number / abs($significance)) * abs($significance);
}
}