Skip to content

Enable array-readiness for more Math/Trig functions #2584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/PhpSpreadsheet/Calculation/MathTrig.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static function ATAN2($xCoordinate = null, $yCoordinate = null)
* @param float $radix
* @param int $minLength
*
* @return string the text representation with the given radix (base)
* @return array|string the text representation with the given radix (base)
*/
public static function BASE($number, $radix, $minLength = null)
{
Expand All @@ -100,7 +100,7 @@ public static function BASE($number, $radix, $minLength = null)
* @param float $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
* @return array|float|string Rounded Number, or a string containing an error
*
* @see MathTrig\Ceiling::ceiling()
* Use the ceiling() method in the MathTrig\Ceiling class instead
Expand Down Expand Up @@ -231,7 +231,7 @@ public static function FACTDOUBLE($factVal)
* @param float $number Number to round
* @param float $significance Significance
*
* @return float|string Rounded Number, or a string containing an error
* @return array|float|string Rounded Number, or a string containing an error
*
*@see MathTrig\Floor::floor()
* Use the floor() method in the MathTrig\Floor class instead
Expand All @@ -255,7 +255,7 @@ public static function FLOOR($number, $significance = null)
* @param float $significance Significance
* @param int $mode direction to round negative numbers
*
* @return float|string Rounded Number, or a string containing an error
* @return array|float|string Rounded Number, or a string containing an error
*
*@see MathTrig\Floor::math()
* Use the math() method in the MathTrig\Floor class instead
Expand All @@ -278,7 +278,7 @@ public static function FLOORMATH($number, $significance = null, $mode = 0)
* @param float $number Number to round
* @param float $significance Significance
*
* @return float|string Rounded Number, or a string containing an error
* @return array|float|string Rounded Number, or a string containing an error
*
*@see MathTrig\Floor::precise()
* Use the precise() method in the MathTrig\Floor class instead
Expand Down Expand Up @@ -375,7 +375,7 @@ public static function LCM(...$args)
* @param float $number The positive real number for which you want the logarithm
* @param float $base The base of the logarithm. If base is omitted, it is assumed to be 10.
*
* @return float|string The result, or a string containing an error
* @return array|float|string The result, or a string containing an error
*/
public static function logBase($number, $base = 10)
{
Expand Down Expand Up @@ -941,7 +941,7 @@ public static function SUMXMY2($matrixData1, $matrixData2)
* @param float $value
* @param int $digits
*
* @return float|string Truncated value, or a string containing an error
* @return array|float|string Truncated value, or a string containing an error
*/
public static function TRUNC($value = 0, $digits = 0)
{
Expand Down Expand Up @@ -1358,7 +1358,7 @@ public static function builtinEXP($number)
*
* @param mixed $number Should be numeric
*
* @return float|string Rounded number
* @return array|float|string Rounded number
*/
public static function builtinLN($number)
{
Expand All @@ -1377,7 +1377,7 @@ public static function builtinLN($number)
*
* @param mixed $number Should be numeric
*
* @return float|string Rounded number
* @return array|float|string Rounded number
*/
public static function builtinLOG10($number)
{
Expand Down
22 changes: 21 additions & 1 deletion src/PhpSpreadsheet/Calculation/MathTrig/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;

use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;

class Base
{
use ArrayEnabled;

/**
* BASE.
*
Expand All @@ -16,13 +19,22 @@ class Base
* BASE(Number, Radix [Min_length])
*
* @param mixed $number expect float
* Or can be an array of values
* @param mixed $radix expect float
* Or can be an array of values
* @param mixed $minLength expect int or null
* Or can be an array of values
*
* @return string the text representation with the given radix (base)
* @return array|string the text representation with the given radix (base)
* If an array of numbers is passed as an argument, then the returned result will also be an array
* with the same dimensions
*/
public static function evaluate($number, $radix, $minLength = null)
{
if (is_array($number) || is_array($radix) || is_array($minLength)) {
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $radix, $minLength);
}

try {
$number = (float) floor(Helpers::validateNumericNullBool($number));
$radix = (int) Helpers::validateNumericNullBool($radix);
Expand All @@ -31,6 +43,14 @@ public static function evaluate($number, $radix, $minLength = null)
}
$minLength = Functions::flattenSingleValue($minLength);

return self::calculate($number, $radix, $minLength);
}

/**
* @param mixed $minLength
*/
private static function calculate(float $number, int $radix, $minLength): string
{
if ($minLength === null || is_numeric($minLength)) {
if ($number < 0 || $number >= 2 ** 53 || $radix < 2 || $radix > 36) {
return Functions::NAN(); // Numeric range constraints
Expand Down
42 changes: 35 additions & 7 deletions src/PhpSpreadsheet/Calculation/MathTrig/Ceiling.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;

use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;

class Ceiling
{
use ArrayEnabled;

/**
* CEILING.
*
Expand All @@ -18,13 +21,21 @@ class Ceiling
* Excel Function:
* CEILING(number[,significance])
*
* @param float $number the number you want the ceiling
* @param float $significance the multiple to which you want to round
* @param array|float $number the number you want the ceiling
* Or can be an array of values
* @param array|float $significance the multiple to which you want to round
* Or can be an array of values
*
* @return float|string Rounded Number, or a string containing an error
* @return array|float|string Rounded Number, or a string containing an error
* If an array of numbers is passed as an argument, then the returned result will also be an array
* with the same dimensions
*/
public static function ceiling($number, $significance = null)
{
if (is_array($number) || is_array($significance)) {
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance);
}

if ($significance === null) {
self::floorCheck1Arg();
}
Expand All @@ -48,13 +59,22 @@ public static function ceiling($number, $significance = null)
* CEILING.MATH(number[,significance[,mode]])
*
* @param mixed $number Number to round
* Or can be an array of values
* @param mixed $significance Significance
* @param int $mode direction to round negative numbers
* Or can be an array of values
* @param array|int $mode direction to round negative numbers
* Or can be an array of values
*
* @return float|string Rounded Number, or a string containing an error
* @return array|float|string Rounded Number, or a string containing an error
* If an array of numbers is passed as an argument, then the returned result will also be an array
* with the same dimensions
*/
public static function math($number, $significance = null, $mode = 0)
{
if (is_array($number) || is_array($significance) || is_array($mode)) {
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance, $mode);
}

try {
$number = Helpers::validateNumericNullBool($number);
$significance = Helpers::validateNumericNullSubstitution($significance, ($number < 0) ? -1 : 1);
Expand Down Expand Up @@ -82,12 +102,20 @@ public static function math($number, $significance = null, $mode = 0)
* CEILING.PRECISE(number[,significance])
*
* @param mixed $number the number you want to round
* @param float $significance the multiple to which you want to round
* Or can be an array of values
* @param array|float $significance the multiple to which you want to round
* Or can be an array of values
*
* @return float|string Rounded Number, or a string containing an error
* @return array|float|string Rounded Number, or a string containing an error
* If an array of numbers is passed as an argument, then the returned result will also be an array
* with the same dimensions
*/
public static function precise($number, $significance = 1)
{
if (is_array($number) || is_array($significance)) {
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance);
}

try {
$number = Helpers::validateNumericNullBool($number);
$significance = Helpers::validateNumericNullSubstitution($significance, null);
Expand Down
38 changes: 33 additions & 5 deletions src/PhpSpreadsheet/Calculation/MathTrig/Floor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;

use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;

class Floor
{
use ArrayEnabled;

private static function floorCheck1Arg(): void
{
$compatibility = Functions::getCompatibilityMode();
Expand All @@ -24,12 +27,20 @@ private static function floorCheck1Arg(): void
* FLOOR(number[,significance])
*
* @param mixed $number Expect float. Number to round
* Or can be an array of values
* @param mixed $significance Expect float. Significance
* Or can be an array of values
*
* @return float|string Rounded Number, or a string containing an error
* @return array|float|string Rounded Number, or a string containing an error
* If an array of numbers is passed as an argument, then the returned result will also be an array
* with the same dimensions
*/
public static function floor($number, $significance = null)
{
if (is_array($number) || is_array($significance)) {
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance);
}

if ($significance === null) {
self::floorCheck1Arg();
}
Expand All @@ -53,13 +64,22 @@ public static function floor($number, $significance = null)
* FLOOR.MATH(number[,significance[,mode]])
*
* @param mixed $number Number to round
* Or can be an array of values
* @param mixed $significance Significance
* Or can be an array of values
* @param mixed $mode direction to round negative numbers
* Or can be an array of values
*
* @return float|string Rounded Number, or a string containing an error
* @return array|float|string Rounded Number, or a string containing an error
* If an array of numbers is passed as an argument, then the returned result will also be an array
* with the same dimensions
*/
public static function math($number, $significance = null, $mode = 0)
{
if (is_array($number) || is_array($significance) || is_array($mode)) {
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance, $mode);
}

try {
$number = Helpers::validateNumericNullBool($number);
$significance = Helpers::validateNumericNullSubstitution($significance, ($number < 0) ? -1 : 1);
Expand All @@ -79,13 +99,21 @@ public static function math($number, $significance = null, $mode = 0)
* Excel Function:
* FLOOR.PRECISE(number[,significance])
*
* @param float $number Number to round
* @param float $significance Significance
* @param array|float $number Number to round
* Or can be an array of values
* @param array|float $significance Significance
* Or can be an array of values
*
* @return float|string Rounded Number, or a string containing an error
* @return array|float|string Rounded Number, or a string containing an error
* If an array of numbers is passed as an argument, then the returned result will also be an array
* with the same dimensions
*/
public static function precise($number, $significance = 1)
{
if (is_array($number) || is_array($significance)) {
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance);
}

try {
$number = Helpers::validateNumericNullBool($number);
$significance = Helpers::validateNumericNullSubstitution($significance, null);
Expand Down
31 changes: 28 additions & 3 deletions src/PhpSpreadsheet/Calculation/MathTrig/Logarithms.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;

use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
use PhpOffice\PhpSpreadsheet\Calculation\Exception;

class Logarithms
{
use ArrayEnabled;

/**
* LOG_BASE.
*
Expand All @@ -15,12 +18,20 @@ class Logarithms
* LOG(number[,base])
*
* @param mixed $number The positive real number for which you want the logarithm
* Or can be an array of values
* @param mixed $base The base of the logarithm. If base is omitted, it is assumed to be 10.
* Or can be an array of values
*
* @return float|string The result, or a string containing an error
* @return array|float|string The result, or a string containing an error
* If an array of numbers is passed as an argument, then the returned result will also be an array
* with the same dimensions
*/
public static function withBase($number, $base = 10)
{
if (is_array($number) || is_array($base)) {
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $base);
}

try {
$number = Helpers::validateNumericNullBool($number);
Helpers::validatePositive($number);
Expand All @@ -39,11 +50,18 @@ public static function withBase($number, $base = 10)
* Returns the result of builtin function log after validating args.
*
* @param mixed $number Should be numeric
* Or can be an array of values
*
* @return float|string Rounded number
* @return array|float|string Rounded number
* If an array of numbers is passed as an argument, then the returned result will also be an array
* with the same dimensions
*/
public static function base10($number)
{
if (is_array($number)) {
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $number);
}

try {
$number = Helpers::validateNumericNullBool($number);
Helpers::validatePositive($number);
Expand All @@ -60,11 +78,18 @@ public static function base10($number)
* Returns the result of builtin function log after validating args.
*
* @param mixed $number Should be numeric
* Or can be an array of values
*
* @return float|string Rounded number
* @return array|float|string Rounded number
* If an array of numbers is passed as an argument, then the returned result will also be an array
* with the same dimensions
*/
public static function natural($number)
{
if (is_array($number)) {
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $number);
}

try {
$number = Helpers::validateNumericNullBool($number);
Helpers::validatePositive($number);
Expand Down
Loading