Continue MathTrig Breakup - Penultimate? (#1973)

* Continue MathTrig Breakup - Penultimate?

Continuing the process of breaking MathTrip.php up into smaller classes. This round takes care of about half of what is left, so perhaps one round after this one will finish the job:
- ARABIC
- COMBIN; also implemented COMBINA
- FACTDOUBLE
- GCD (which accepts and ignores empty cells as arguments, but returns VALUE if all the arguments are that way; LCM does the same)
- LOG_BASE, LOG10, LN
- implemented MUNIT
- MOD
- POWER
- RAND, RANDBETWEEN (RANDARRAY is too complicated to implement with this ticket)

As you can see from the description, there are some functions which were combined in a single class. When not combined, I adopted PowerKiki's suggestion of using "execute" as the function name.

Co-authored-by: Mark Baker <mark@lange.demon.co.uk>
This commit is contained in:
oleibman 2021-04-02 05:35:34 -07:00 committed by GitHub
parent 0c403efe60
commit a4982fd9fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
41 changed files with 1089 additions and 372 deletions

View File

@ -288,7 +288,7 @@ class Calculation
],
'ARABIC' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'ARABIC'],
'functionCall' => [MathTrig\Arabic::class, 'evaluate'],
'argumentCount' => '1',
],
'AREAS' => [
@ -555,12 +555,12 @@ class Calculation
],
'COMBIN' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'COMBIN'],
'functionCall' => [MathTrig\Combinations::class, 'withoutRepetition'],
'argumentCount' => '2',
],
'COMBINA' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [Functions::class, 'DUMMY'],
'functionCall' => [MathTrig\Combinations::class, 'withRepetition'],
'argumentCount' => '2',
],
'COMPLEX' => [
@ -995,7 +995,7 @@ class Calculation
],
'FACTDOUBLE' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'FACTDOUBLE'],
'functionCall' => [MathTrig\FactDouble::class, 'evaluate'],
'argumentCount' => '1',
],
'FALSE' => [
@ -1187,7 +1187,7 @@ class Calculation
],
'GCD' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'GCD'],
'functionCall' => [MathTrig\Gcd::class, 'evaluate'],
'argumentCount' => '1+',
],
'GEOMEAN' => [
@ -1566,17 +1566,17 @@ class Calculation
],
'LN' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'builtinLN'],
'functionCall' => [MathTrig\Logarithms::class, 'natural'],
'argumentCount' => '1',
],
'LOG' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'logBase'],
'functionCall' => [MathTrig\Logarithms::class, 'withBase'],
'argumentCount' => '1,2',
],
'LOG10' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'builtinLOG10'],
'functionCall' => [MathTrig\Logarithms::class, 'base10'],
'argumentCount' => '1',
],
'LOGEST' => [
@ -1701,7 +1701,7 @@ class Calculation
],
'MOD' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'MOD'],
'functionCall' => [MathTrig\Mod::class, 'evaluate'],
'argumentCount' => '2',
],
'MODE' => [
@ -1736,7 +1736,7 @@ class Calculation
],
'MUNIT' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [Functions::class, 'DUMMY'],
'functionCall' => [MathTrig\MatrixFunctions::class, 'funcMUnit'],
'argumentCount' => '1',
],
'N' => [
@ -1973,7 +1973,7 @@ class Calculation
],
'POWER' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'POWER'],
'functionCall' => [MathTrig\Power::class, 'evaluate'],
'argumentCount' => '2',
],
'PPMT' => [
@ -2043,7 +2043,7 @@ class Calculation
],
'RAND' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'RAND'],
'functionCall' => [MathTrig\Random::class, 'randNoArgs'],
'argumentCount' => '0',
],
'RANDARRAY' => [
@ -2053,7 +2053,7 @@ class Calculation
],
'RANDBETWEEN' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'RAND'],
'functionCall' => [MathTrig\Random::class, 'randBetween'],
'argumentCount' => '2',
],
'RANK' => [

View File

@ -2,22 +2,15 @@
namespace PhpOffice\PhpSpreadsheet\Calculation;
use Exception;
class MathTrig
{
private static function strSplit(string $roman): array
{
$rslt = str_split($roman);
return is_array($rslt) ? $rslt : [];
}
/**
* ARABIC.
*
* Converts a Roman numeral to an Arabic numeral.
*
* @Deprecated 2.0.0 Use the evaluate method in the MathTrig\Arabic class instead
*
* Excel Function:
* ARABIC(text)
*
@ -27,69 +20,7 @@ class MathTrig
*/
public static function ARABIC($roman)
{
// An empty string should return 0
$roman = substr(trim(strtoupper((string) Functions::flattenSingleValue($roman))), 0, 255);
if ($roman === '') {
return 0;
}
// Convert the roman numeral to an arabic number
$negativeNumber = $roman[0] === '-';
if ($negativeNumber) {
$roman = substr($roman, 1);
}
try {
$arabic = self::calculateArabic(self::strSplit($roman));
} catch (Exception $e) {
return Functions::VALUE(); // Invalid character detected
}
if ($negativeNumber) {
$arabic *= -1; // The number should be negative
}
return $arabic;
}
/**
* Recursively calculate the arabic value of a roman numeral.
*
* @param int $sum
* @param int $subtract
*
* @return int
*/
protected static function calculateArabic(array $roman, &$sum = 0, $subtract = 0)
{
$lookup = [
'M' => 1000,
'D' => 500,
'C' => 100,
'L' => 50,
'X' => 10,
'V' => 5,
'I' => 1,
];
$numeral = array_shift($roman);
if (!isset($lookup[$numeral])) {
throw new Exception('Invalid character detected');
}
$arabic = $lookup[$numeral];
if (count($roman) > 0 && isset($lookup[$roman[0]]) && $arabic < $lookup[$roman[0]]) {
$subtract += $arabic;
} else {
$sum += ($arabic - $subtract);
$subtract = 0;
}
if (count($roman) > 0) {
self::calculateArabic($roman, $sum, $subtract);
}
return $sum;
return MathTrig\Arabic::evaluate($roman);
}
/**
@ -172,6 +103,8 @@ class MathTrig
* Returns the number of combinations for a given number of items. Use COMBIN to
* determine the total possible number of groups for a given number of items.
*
* @Deprecated 2.0.0 Use the without method in the MathTrig\Combinations class instead
*
* Excel Function:
* COMBIN(numObjs,numInSet)
*
@ -182,20 +115,7 @@ class MathTrig
*/
public static function COMBIN($numObjs, $numInSet)
{
$numObjs = Functions::flattenSingleValue($numObjs);
$numInSet = Functions::flattenSingleValue($numInSet);
if ((is_numeric($numObjs)) && (is_numeric($numInSet))) {
if ($numObjs < $numInSet) {
return Functions::NAN();
} elseif ($numInSet < 0) {
return Functions::NAN();
}
return round(MathTrig\Fact::funcFact($numObjs) / MathTrig\Fact::funcFact($numObjs - $numInSet)) / MathTrig\Fact::funcFact($numInSet);
}
return Functions::VALUE();
return MathTrig\Combinations::withoutRepetition($numObjs, $numInSet);
}
/**
@ -256,6 +176,8 @@ class MathTrig
*
* Returns the double factorial of a number.
*
* @Deprecated 2.0.0 Use the evaluate method in the MathTrig\FactDouble class instead
*
* Excel Function:
* FACTDOUBLE(factVal)
*
@ -265,23 +187,7 @@ class MathTrig
*/
public static function FACTDOUBLE($factVal)
{
$factLoop = Functions::flattenSingleValue($factVal);
if (is_numeric($factLoop)) {
$factLoop = floor($factLoop);
if ($factVal < 0) {
return Functions::NAN();
}
$factorial = 1;
while ($factLoop > 1) {
$factorial *= $factLoop--;
--$factLoop;
}
return $factorial;
}
return Functions::VALUE();
return MathTrig\FactDouble::evaluate($factVal);
}
/**
@ -351,11 +257,6 @@ class MathTrig
return MathTrig\FloorPrecise::funcFloorPrecise($number, $significance);
}
private static function evaluateGCD($a, $b)
{
return $b ? self::evaluateGCD($b, $a % $b) : $a;
}
/**
* INT.
*
@ -384,6 +285,8 @@ class MathTrig
* The greatest common divisor is the largest integer that divides both
* number1 and number2 without a remainder.
*
* @Deprecated 2.0.0 Use the evaluate method in the MathTrig\Gcd class instead
*
* Excel Function:
* GCD(number1[,number2[, ...]])
*
@ -393,22 +296,7 @@ class MathTrig
*/
public static function GCD(...$args)
{
$args = Functions::flattenArray($args);
// Loop through arguments
foreach (Functions::flattenArray($args) as $value) {
if (!is_numeric($value)) {
return Functions::VALUE();
} elseif ($value < 0) {
return Functions::NAN();
}
}
$gcd = (int) array_pop($args);
do {
$gcd = self::evaluateGCD($gcd, (int) array_pop($args));
} while (!empty($args));
return $gcd;
return MathTrig\Gcd::evaluate(...$args);
}
/**
@ -438,6 +326,8 @@ class MathTrig
*
* Returns the logarithm of a number to a specified base. The default base is 10.
*
* @Deprecated 2.0.0 Use the withBase method in the MathTrig\Logarithms class instead
*
* Excel Function:
* LOG(number[,base])
*
@ -446,19 +336,9 @@ class MathTrig
*
* @return float|string The result, or a string containing an error
*/
public static function logBase($number = null, $base = 10)
public static function logBase($number, $base = 10)
{
$number = Functions::flattenSingleValue($number);
$base = ($base === null) ? 10 : (float) Functions::flattenSingleValue($base);
if ((!is_numeric($base)) || (!is_numeric($number))) {
return Functions::VALUE();
}
if (($base <= 0) || ($number <= 0)) {
return Functions::NAN();
}
return log($number, $base);
return MathTrig\Logarithms::withBase($number, $base);
}
/**
@ -517,6 +397,8 @@ class MathTrig
/**
* MOD.
*
* @Deprecated 2.0.0 Use the evaluate method in the MathTrig\Mod class instead
*
* @param int $a Dividend
* @param int $b Divisor
*
@ -524,18 +406,7 @@ class MathTrig
*/
public static function MOD($a = 1, $b = 1)
{
$a = (float) Functions::flattenSingleValue($a);
$b = (float) Functions::flattenSingleValue($b);
if ($b == 0.0) {
return Functions::DIV0();
} elseif (($a < 0.0) && ($b > 0.0)) {
return $b - fmod(abs($a), $b);
} elseif (($a > 0.0) && ($b < 0.0)) {
return $b + fmod($a, abs($b));
}
return fmod($a, $b);
return MathTrig\Mod::evaluate($a, $b);
}
/**
@ -562,6 +433,8 @@ class MathTrig
*
* Returns the ratio of the factorial of a sum of values to the product of factorials.
*
* @Deprecated 2.0.0 Use the funcMultinomial method in the MathTrig\Multinomial class instead
*
* @param mixed[] $args An array of mixed values for the Data Series
*
* @return float|string The result, or a string containing an error
@ -592,27 +465,16 @@ class MathTrig
*
* Computes x raised to the power y.
*
* @Deprecated 2.0.0 Use the evaluate method in the MathTrig\Power class instead
*
* @param float $x
* @param float $y
*
* @return float|string The result, or a string containing an error
* @return float|int|string The result, or a string containing an error
*/
public static function POWER($x = 0, $y = 2)
{
$x = Functions::flattenSingleValue($x);
$y = Functions::flattenSingleValue($y);
// Validate parameters
if ($x == 0.0 && $y == 0.0) {
return Functions::NAN();
} elseif ($x == 0.0 && $y < 0.0) {
return Functions::DIV0();
}
// Return
$result = $x ** $y;
return (!is_nan($result) && !is_infinite($result)) ? $result : Functions::NAN();
return MathTrig\Power::evaluate($x, $y);
}
/**
@ -656,23 +518,18 @@ class MathTrig
}
/**
* RAND.
* RAND/RANDBETWEEN.
*
* @Deprecated 2.0.0 Use the randNoArg or randBetween method in the MathTrig\Random class instead
*
* @param int $min Minimal value
* @param int $max Maximal value
*
* @return int Random number
* @return float|int|string Random number
*/
public static function RAND($min = 0, $max = 0)
{
$min = Functions::flattenSingleValue($min);
$max = Functions::flattenSingleValue($max);
if ($min == 0 && $max == 0) {
return (mt_rand(0, 10000000)) / 10000000;
}
return mt_rand($min, $max);
return MathTrig\Random::randBetween($min, $max);
}
/**
@ -1388,19 +1245,15 @@ class MathTrig
*
* Returns the result of builtin function log after validating args.
*
* @Deprecated 2.0.0 Use the natural method in the MathTrig\Logarithms class instead
*
* @param mixed $number Should be numeric
*
* @return float|string Rounded number
*/
public static function builtinLN($number)
{
$number = Functions::flattenSingleValue($number);
if (!is_numeric($number)) {
return Functions::VALUE();
}
return log($number);
return MathTrig\Logarithms::natural($number);
}
/**
@ -1408,19 +1261,15 @@ class MathTrig
*
* Returns the result of builtin function log after validating args.
*
* @Deprecated 2.0.0 Use the base10 method in the MathTrig\Logarithms class instead
*
* @param mixed $number Should be numeric
*
* @return float|string Rounded number
*/
public static function builtinLOG10($number)
{
$number = Functions::flattenSingleValue($number);
if (!is_numeric($number)) {
return Functions::VALUE();
}
return log10($number);
return MathTrig\Logarithms::base10($number);
}
/**

View File

@ -0,0 +1,95 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
class Arabic
{
private const ROMAN_LOOKUP = [
'M' => 1000,
'D' => 500,
'C' => 100,
'L' => 50,
'X' => 10,
'V' => 5,
'I' => 1,
];
/**
* Recursively calculate the arabic value of a roman numeral.
*
* @param int $sum
* @param int $subtract
*
* @return int
*/
private static function calculateArabic(array $roman, &$sum = 0, $subtract = 0)
{
$numeral = array_shift($roman);
if (!isset(self::ROMAN_LOOKUP[$numeral])) {
throw new Exception('Invalid character detected');
}
$arabic = self::ROMAN_LOOKUP[$numeral];
if (count($roman) > 0 && isset(self::ROMAN_LOOKUP[$roman[0]]) && $arabic < self::ROMAN_LOOKUP[$roman[0]]) {
$subtract += $arabic;
} else {
$sum += ($arabic - $subtract);
$subtract = 0;
}
if (count($roman) > 0) {
self::calculateArabic($roman, $sum, $subtract);
}
return $sum;
}
private static function strSplit(string $roman): array
{
$rslt = str_split($roman);
return is_array($rslt) ? $rslt : [];
}
/**
* ARABIC.
*
* Converts a Roman numeral to an Arabic numeral.
*
* Excel Function:
* ARABIC(text)
*
* @param string $roman
*
* @return int|string the arabic numberal contrived from the roman numeral
*/
public static function evaluate($roman)
{
// An empty string should return 0
$roman = substr(trim(strtoupper((string) Functions::flattenSingleValue($roman))), 0, 255);
if ($roman === '') {
return 0;
}
// Convert the roman numeral to an arabic number
$negativeNumber = $roman[0] === '-';
if ($negativeNumber) {
$roman = substr($roman, 1);
}
try {
$arabic = self::calculateArabic(self::strSplit($roman));
} catch (Exception $e) {
return Functions::VALUE(); // Invalid character detected
}
if ($negativeNumber) {
$arabic *= -1; // The number should be negative
}
return $arabic;
}
}

View File

@ -0,0 +1,74 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use Exception;
class Combinations
{
/**
* COMBIN.
*
* Returns the number of combinations for a given number of items. Use COMBIN to
* determine the total possible number of groups for a given number of items.
*
* Excel Function:
* COMBIN(numObjs,numInSet)
*
* @param mixed $numObjs Number of different objects
* @param mixed $numInSet Number of objects in each combination
*
* @return float|int|string Number of combinations, or a string containing an error
*/
public static function withoutRepetition($numObjs, $numInSet)
{
try {
$numObjs = Helpers::validateNumericNullSubstitution($numObjs, null);
$numInSet = Helpers::validateNumericNullSubstitution($numInSet, null);
Helpers::validateNotNegative($numInSet);
Helpers::validateNotNegative($numObjs - $numInSet);
} catch (Exception $e) {
return $e->getMessage();
}
return round(Fact::funcFact($numObjs) / Fact::funcFact($numObjs - $numInSet)) / Fact::funcFact($numInSet);
}
/**
* COMBIN.
*
* Returns the number of combinations for a given number of items. Use COMBIN to
* determine the total possible number of groups for a given number of items.
*
* Excel Function:
* COMBIN(numObjs,numInSet)
*
* @param mixed $numObjs Number of different objects
* @param mixed $numInSet Number of objects in each combination
*
* @return float|int|string Number of combinations, or a string containing an error
*/
public static function withRepetition($numObjs, $numInSet)
{
try {
$numObjs = Helpers::validateNumericNullSubstitution($numObjs, null);
$numInSet = Helpers::validateNumericNullSubstitution($numInSet, null);
Helpers::validateNotNegative($numInSet);
Helpers::validateNotNegative($numObjs);
$numObjs = (int) $numObjs;
$numInSet = (int) $numInSet;
// Microsoft documentation says following is true, but Excel
// does not enforce this restriction.
//Helpers::validateNotNegative($numObjs - $numInSet);
if ($numObjs === 0) {
Helpers::validateNotNegative(-$numInSet);
return 1;
}
} catch (Exception $e) {
return $e->getMessage();
}
return round(Fact::funcFact($numObjs + $numInSet - 1) / Fact::funcFact($numObjs - 1)) / Fact::funcFact($numInSet);
}
}

View File

@ -0,0 +1,39 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use Exception;
class FactDouble
{
/**
* FACTDOUBLE.
*
* Returns the double factorial of a number.
*
* Excel Function:
* FACTDOUBLE(factVal)
*
* @param float $factVal Factorial Value
*
* @return float|int|string Double Factorial, or a string containing an error
*/
public static function evaluate($factVal)
{
try {
$factVal = Helpers::validateNumericNullSubstitution($factVal, 0);
Helpers::validateNotNegative($factVal);
} catch (Exception $e) {
return $e->getMessage();
}
$factLoop = floor($factVal);
$factorial = 1;
while ($factLoop > 1) {
$factorial *= $factLoop;
$factLoop -= 2;
}
return $factorial;
}
}

View File

@ -0,0 +1,69 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
class Gcd
{
/**
* Recursively determine GCD.
*
* Returns the greatest common divisor of a series of numbers.
* The greatest common divisor is the largest integer that divides both
* number1 and number2 without a remainder.
*
* Excel Function:
* GCD(number1[,number2[, ...]])
*
* @param float|int $a
* @param float|int $b
*
* @return float|int
*/
private static function evaluateGCD($a, $b)
{
return $b ? self::evaluateGCD($b, $a % $b) : $a;
}
/**
* GCD.
*
* Returns the greatest common divisor of a series of numbers.
* The greatest common divisor is the largest integer that divides both
* number1 and number2 without a remainder.
*
* Excel Function:
* GCD(number1[,number2[, ...]])
*
* @param mixed ...$args Data values
*
* @return float|int|string Greatest Common Divisor, or a string containing an error
*/
public static function evaluate(...$args)
{
try {
$arrayArgs = [];
foreach (Functions::flattenArray($args) as $value1) {
if ($value1 !== null) {
$value = Helpers::validateNumericNullSubstitution($value1, 1);
Helpers::validateNotNegative($value);
$arrayArgs[] = (int) $value;
}
}
} catch (Exception $e) {
return $e->getMessage();
}
if (count($arrayArgs) <= 0) {
return Functions::VALUE();
}
$gcd = (int) array_pop($arrayArgs);
do {
$gcd = self::evaluateGCD($gcd, (int) array_pop($arrayArgs));
} while (!empty($arrayArgs));
return $gcd;
}
}

View File

@ -66,13 +66,27 @@ class Helpers
*
* @param float|int $number
*/
public static function validateNotNegative($number): void
public static function validateNotNegative($number, ?string $except = null): void
{
if ($number >= 0) {
return;
}
throw new Exception(Functions::NAN());
throw new Exception($except ?? Functions::NAN());
}
/**
* Confirm number > 0.
*
* @param float|int $number
*/
public static function validatePositive($number, ?string $except = null): void
{
if ($number > 0) {
return;
}
throw new Exception($except ?? Functions::NAN());
}
/**

View File

@ -53,12 +53,15 @@ class Lcm
try {
$arrayArgs = [];
$anyZeros = 0;
$anyNonNulls = 0;
foreach (Functions::flattenArray($args) as $value1) {
$anyNonNulls += (int) ($value1 !== null);
$value = Helpers::validateNumericNullSubstitution($value1, 1);
Helpers::validateNotNegative($value);
$arrayArgs[] = (int) $value;
$anyZeros += (int) !((bool) $value);
}
self::testNonNulls($anyNonNulls);
if ($anyZeros) {
return 0;
}
@ -76,6 +79,17 @@ class Lcm
foreach ($myCountedFactors as $myCountedFactor => $myCountedPower) {
$myPoweredFactors[$myCountedFactor] = $myCountedFactor ** $myCountedPower;
}
self::processPoweredFactors($allPoweredFactors, $myPoweredFactors);
}
foreach ($allPoweredFactors as $allPoweredFactor) {
$returnValue *= (int) $allPoweredFactor;
}
return $returnValue;
}
private static function processPoweredFactors(array &$allPoweredFactors, array &$myPoweredFactors): void
{
foreach ($myPoweredFactors as $myPoweredValue => $myPoweredFactor) {
if (isset($allPoweredFactors[$myPoweredValue])) {
if ($allPoweredFactors[$myPoweredValue] < $myPoweredFactor) {
@ -86,10 +100,11 @@ class Lcm
}
}
}
foreach ($allPoweredFactors as $allPoweredFactor) {
$returnValue *= (int) $allPoweredFactor;
}
return $returnValue;
private static function testNonNulls(int $anyNonNulls): void
{
if (!$anyNonNulls) {
throw new Exception(Functions::VALUE());
}
}
}

View File

@ -0,0 +1,79 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use Exception;
class Logarithms
{
/**
* LOG_BASE.
*
* Returns the logarithm of a number to a specified base. The default base is 10.
*
* Excel Function:
* LOG(number[,base])
*
* @param mixed $number The positive real number for which you want the logarithm
* @param mixed $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
*/
public static function withBase($number, $base = 10)
{
try {
$number = Helpers::validateNumericNullBool($number);
Helpers::validatePositive($number);
$base = Helpers::validateNumericNullBool($base);
Helpers::validatePositive($base);
} catch (Exception $e) {
return $e->getMessage();
}
return log($number, $base);
}
/**
* LOG10.
*
* Returns the result of builtin function log after validating args.
*
* @param mixed $number Should be numeric
*
* @return float|string Rounded number
*/
public static function base10($number)
{
try {
$number = Helpers::validateNumericNullBool($number);
Helpers::validatePositive($number);
} catch (Exception $e) {
return $e->getMessage();
}
return log10($number);
}
/**
* LN.
*
* Returns the result of builtin function log after validating args.
*
* @Deprecated 2.0.0 Use the natural method in the MathTrig\Logarithms class instead
*
* @param mixed $number Should be numeric
*
* @return float|string Rounded number
*/
public static function natural($number)
{
try {
$number = Helpers::validateNumericNullBool($number);
Helpers::validatePositive($number);
} catch (Exception $e) {
return $e->getMessage();
}
return log($number);
}
}

View File

@ -3,6 +3,7 @@
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use Exception;
use Matrix\Builder;
use Matrix\Exception as MatrixException;
use Matrix\Matrix;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
@ -111,4 +112,27 @@ class MatrixFunctions
return $e->getMessage();
}
}
/**
* MUnit.
*
* @param mixed $dimension Number of rows and columns
*
* @return array|string The result, or a string containing an error
*/
public static function funcMUnit($dimension)
{
try {
$dimension = (int) Helpers::validateNumericNullBool($dimension);
Helpers::validatePositive($dimension, Functions::VALUE());
$matrix = Builder::createFilledMatrix(0, $dimension)->toArray();
for ($x = 0; $x < $dimension; ++$x) {
$matrix[$x][$x] = 1;
}
return $matrix;
} catch (Exception $e) {
return $e->getMessage();
}
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use Exception;
class Mod
{
/**
* MOD.
*
* @param mixed $a Dividend
* @param mixed $b Divisor
*
* @return float|int|string Remainder, or a string containing an error
*/
public static function evaluate($a, $b)
{
try {
$a = Helpers::validateNumericNullBool($a);
$b = Helpers::validateNumericNullBool($b);
Helpers::validateNotZero($b);
} catch (Exception $e) {
return $e->getMessage();
}
if (($a < 0.0) && ($b > 0.0)) {
return $b - fmod(abs($a), $b);
}
if (($a > 0.0) && ($b < 0.0)) {
return $b + fmod($a, abs($b));
}
return fmod($a, $b);
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
class Power
{
/**
* POWER.
*
* Computes x raised to the power y.
*
* @param float|int $x
* @param float|int $y
*
* @return float|int|string The result, or a string containing an error
*/
public static function evaluate($x, $y)
{
try {
$x = Helpers::validateNumericNullBool($x);
$y = Helpers::validateNumericNullBool($y);
} catch (Exception $e) {
return $e->getMessage();
}
// Validate parameters
if (!$x && !$y) {
return Functions::NAN();
}
if (!$x && $y < 0.0) {
return Functions::DIV0();
}
// Return
$result = $x ** $y;
return Helpers::numberOrNan($result);
}
}

View File

@ -0,0 +1,39 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use Exception;
class Random
{
/**
* RAND.
*
* @return float Random number
*/
public static function randNoArgs()
{
return (mt_rand(0, 10000000)) / 10000000;
}
/**
* RANDBETWEEN.
*
* @param mixed $min Minimal value
* @param mixed $max Maximal value
*
* @return float|int|string Random number
*/
public static function randBetween($min, $max)
{
try {
$min = (int) Helpers::validateNumericNullBool($min);
$max = (int) Helpers::validateNumericNullBool($max);
Helpers::validateNotNegative($max - $min);
} catch (Exception $e) {
return $e->getMessage();
}
return mt_rand($min, $max);
}
}

View File

@ -53,6 +53,7 @@ CODE
COLUMN
COLUMNS
COMBIN
COMBINA
COMPLEX
CONCAT
CONCATENATE
@ -249,6 +250,7 @@ MODE
MONTH
MROUND
MULTINOMIAL
MUNIT
N
NA
NEGBINOMDIST

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 ArabicTest extends TestCase
class ArabicTest extends AllSetupTeardown
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}
/**
* @dataProvider providerARABIC
*
@ -21,8 +12,12 @@ class ArabicTest extends TestCase
*/
public function testARABIC($expectedResult, $romanNumeral): void
{
$result = MathTrig::ARABIC($romanNumeral);
self::assertEquals($expectedResult, $result);
$this->mightHaveException($expectedResult);
$sheet = $this->sheet;
$sheet->getCell('A1')->setValue($romanNumeral);
$sheet->getCell('B1')->setValue('=ARABIC(A1)');
$result = $sheet->getCell('B1')->getCalculatedValue();
self::assertSame($expectedResult, $result);
}
public function providerARABIC()

View File

@ -0,0 +1,33 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
class CombinATest extends AllSetupTeardown
{
/**
* @dataProvider providerCOMBINA
*
* @param mixed $expectedResult
* @param mixed $numObjs
* @param mixed $numInSet
*/
public function testCOMBINA($expectedResult, $numObjs, $numInSet): void
{
$this->mightHaveException($expectedResult);
$sheet = $this->sheet;
if ($numObjs !== null) {
$sheet->getCell('A1')->setValue($numObjs);
}
if ($numInSet !== null) {
$sheet->getCell('A2')->setValue($numInSet);
}
$sheet->getCell('B1')->setValue('=COMBINA(A1,A2)');
$result = $sheet->getCell('B1')->getCalculatedValue();
self::assertEquals($expectedResult, $result);
}
public function providerCOMBINA()
{
return require 'tests/data/Calculation/MathTrig/COMBINA.php';
}
}

View File

@ -2,26 +2,28 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use PHPUnit\Framework\TestCase;
class CombinTest extends TestCase
class CombinTest extends AllSetupTeardown
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}
/**
* @dataProvider providerCOMBIN
*
* @param mixed $expectedResult
* @param mixed $numObjs
* @param mixed $numInSet
*/
public function testCOMBIN($expectedResult, ...$args): void
public function testCOMBIN($expectedResult, $numObjs, $numInSet): void
{
$result = MathTrig::COMBIN(...$args);
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
$this->mightHaveException($expectedResult);
$sheet = $this->sheet;
if ($numObjs !== null) {
$sheet->getCell('A1')->setValue($numObjs);
}
if ($numInSet !== null) {
$sheet->getCell('A2')->setValue($numInSet);
}
$sheet->getCell('B1')->setValue('=COMBIN(A1,A2)');
$result = $sheet->getCell('B1')->getCalculatedValue();
self::assertEquals($expectedResult, $result);
}
public function providerCOMBIN()

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 FactDoubleTest extends TestCase
class FactDoubleTest extends AllSetupTeardown
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}
/**
* @dataProvider providerFACTDOUBLE
*
@ -21,8 +12,12 @@ class FactDoubleTest extends TestCase
*/
public function testFACTDOUBLE($expectedResult, $value): void
{
$result = MathTrig::FACTDOUBLE($value);
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
$this->mightHaveException($expectedResult);
$sheet = $this->sheet;
$sheet->getCell('A1')->setValue($value);
$sheet->getCell('B1')->setValue('=FACTDOUBLE(A1)');
$result = $sheet->getCell('B1')->getCalculatedValue();
self::assertEquals($expectedResult, $result);
}
public function providerFACTDOUBLE()

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 GcdTest extends TestCase
class GcdTest extends AllSetupTeardown
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}
/**
* @dataProvider providerGCD
*
@ -20,7 +11,21 @@ class GcdTest extends TestCase
*/
public function testGCD($expectedResult, ...$args): void
{
$result = MathTrig::GCD(...$args);
$this->mightHaveException($expectedResult);
$sheet = $this->sheet;
$row = 0;
foreach ($args as $arg) {
++$row;
if ($arg !== null) {
$sheet->getCell("A$row")->setValue($arg);
}
}
if ($row < 1) {
$sheet->getCell('B1')->setValue('=GCD()');
} else {
$sheet->getCell('B1')->setValue("=GCD(A1:A$row)");
}
$result = $sheet->getCell('B1')->getCalculatedValue();
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
}

View File

@ -2,30 +2,27 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PHPUnit\Framework\TestCase;
class LnTest extends TestCase
class LnTest extends AllSetupTeardown
{
/**
* @dataProvider providerLN
*
* @param mixed $expectedResult
* @param mixed $val
* @param mixed $number
*/
public function testLN($expectedResult, $val = null): void
public function testLN($expectedResult, $number = 'omitted'): void
{
if ($val === null) {
$this->expectException(CalcExp::class);
$formula = '=LN()';
} else {
$formula = "=LN($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();
if ($number === 'omitted') {
$sheet->getCell('B1')->setValue('=LN()');
} else {
$sheet->getCell('B1')->setValue('=LN(A1)');
}
$result = $sheet->getCell('B1')->getCalculatedValue();
self::assertEqualsWithDelta($expectedResult, $result, 1E-6);
}

View File

@ -2,30 +2,27 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PHPUnit\Framework\TestCase;
class Log10Test extends TestCase
class Log10Test extends AllSetupTeardown
{
/**
* @dataProvider providerLN
*
* @param mixed $expectedResult
* @param mixed $val
* @param mixed $number
*/
public function testLN($expectedResult, $val = null): void
public function testLN($expectedResult, $number = 'omitted'): void
{
if ($val === null) {
$this->expectException(CalcExp::class);
$formula = '=LOG10()';
} else {
$formula = "=LOG10($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();
if ($number === 'omitted') {
$sheet->getCell('B1')->setValue('=LOG10()');
} else {
$sheet->getCell('B1')->setValue('=LOG10(A1)');
}
$result = $sheet->getCell('B1')->getCalculatedValue();
self::assertEqualsWithDelta($expectedResult, $result, 1E-6);
}

View File

@ -2,25 +2,33 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use PHPUnit\Framework\TestCase;
class LogTest extends TestCase
class LogTest extends AllSetupTeardown
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}
/**
* @dataProvider providerLOG
*
* @param mixed $expectedResult
* @param mixed $number
* @param mixed $base
*/
public function testLOG($expectedResult, ...$args): void
public function testLOG($expectedResult, $number = 'omitted', $base = 'omitted'): void
{
$result = MathTrig::logBase(...$args);
$this->mightHaveException($expectedResult);
$sheet = $this->sheet;
if ($number !== null) {
$sheet->getCell('A1')->setValue($number);
}
if ($base !== null) {
$sheet->getCell('A2')->setValue($base);
}
if ($number === 'omitted') {
$sheet->getCell('B1')->setValue('=LOG()');
} elseif ($base === 'omitted') {
$sheet->getCell('B1')->setValue('=LOG(A1)');
} else {
$sheet->getCell('B1')->setValue('=LOG(A1,A2)');
}
$result = $sheet->getCell('B1')->getCalculatedValue();
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
}

View File

@ -28,5 +28,16 @@ class MMultTest extends AllSetupTeardown
$sheet = $this->sheet;
$sheet->getCell('A1')->setValue('=MMULT({1,2,3}, {1,2,3})'); // incompatible dimensions
self::assertSame('#VALUE!', $sheet->getCell('A1')->getCalculatedValue());
$sheet->getCell('A11')->setValue('=MMULT({1, 2, 3, 4}, {5; 6; 7; 8})');
self::assertEquals(70, $sheet->getCell('A11')->getCalculatedValue());
$sheet->getCell('A2')->setValue(1);
$sheet->getCell('B2')->setValue(2);
$sheet->getCell('C2')->setValue(3);
$sheet->getCell('D2')->setValue(4);
$sheet->getCell('D3')->setValue(5);
$sheet->getCell('D4')->setValue(6);
$sheet->getCell('A12')->setValue('=MMULT(A2:C2,D2:D4)');
self::assertEquals(32, $sheet->getCell('A12')->getCalculatedValue());
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig\MatrixFunctions;
class MUnitTest extends AllSetupTeardown
{
public function testMUNIT(): void
{
$identity = MatrixFunctions::funcMUnit(3);
self::assertEquals([[1, 0, 0], [0, 1, 0], [0, 0, 1]], $identity);
$startArray = [[1, 2, 2], [4, 5, 6], [7, 8, 9]];
$resultArray = MatrixFunctions::funcMMult($startArray, $identity);
self::assertEquals($startArray, $resultArray);
$inverseArray = MatrixFunctions::funcMInverse($startArray);
$resultArray = MatrixFunctions::funcMMult($startArray, $inverseArray);
self::assertEquals($identity, $resultArray);
self::assertEquals('#VALUE!', MatrixFunctions::funcMUnit(0));
self::assertEquals('#VALUE!', MatrixFunctions::funcMUnit(-1));
self::assertEquals('#VALUE!', MatrixFunctions::funcMUnit('X'));
}
}

View File

@ -2,25 +2,33 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use PHPUnit\Framework\TestCase;
class ModTest extends TestCase
class ModTest extends AllSetupTeardown
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}
/**
* @dataProvider providerMOD
*
* @param mixed $expectedResult
* @param mixed $dividend
* @param mixed $divisor
*/
public function testMOD($expectedResult, ...$args): void
public function testMOD($expectedResult, $dividend = 'omitted', $divisor = 'omitted'): void
{
$result = MathTrig::MOD(...$args);
$this->mightHaveException($expectedResult);
$sheet = $this->sheet;
if ($dividend !== null) {
$sheet->getCell('A1')->setValue($dividend);
}
if ($divisor !== null) {
$sheet->getCell('A2')->setValue($divisor);
}
if ($dividend === 'omitted') {
$sheet->getCell('B1')->setValue('=MOD()');
} elseif ($divisor === 'omitted') {
$sheet->getCell('B1')->setValue('=MOD(A1)');
} else {
$sheet->getCell('B1')->setValue('=MOD(A1,A2)');
}
$result = $sheet->getCell('B1')->getCalculatedValue();
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
}

View File

@ -20,6 +20,7 @@ class MovedFunctionsTest extends TestCase
self::assertEqualsWithDelta(0, MathTrig::builtinACOSH(1), 1E-9);
self::assertEqualsWithDelta(3.04192400109863, MathTrig::ACOT(-10), 1E-9);
self::assertEqualsWithDelta(-0.20273255405408, MathTrig::ACOTH(-5), 1E-9);
self::assertSame(49, MathTrig::ARABIC('XLIX'));
self::assertEqualsWithDelta(0, MathTrig::builtinASIN(0), 1E-9);
self::assertEqualsWithDelta(0, MathTrig::builtinASINH(0), 1E-9);
self::assertEqualsWithDelta(0, MathTrig::builtinATAN(0), 1E-9);
@ -27,6 +28,7 @@ class MovedFunctionsTest extends TestCase
self::assertEqualsWithDelta('#DIV/0!', MathTrig::ATAN2(0, 0), 1E-9);
self::assertEquals('12', MathTrig::BASE(10, 8));
self::assertEquals(-6, MathTrig::CEILING(-4.5, -2));
self::assertEquals(15, MathTrig::COMBIN(6, 2));
self::assertEquals(1, MathTrig::builtinCOS(0));
self::assertEquals(1, MathTrig::builtinCOSH(0));
self::assertEquals('#DIV/0!', MathTrig::COT(0));
@ -35,11 +37,15 @@ class MovedFunctionsTest extends TestCase
self::assertEquals('#DIV/0!', MathTrig::CSCH(0));
self::assertEquals(6, MathTrig::EVEN(4.5));
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(-9, MathTrig::INT(-8.3));
self::assertEquals(12, MathTrig::LCM(4, 6));
self::assertEqualswithDelta(2.302585, MathTrig::builtinLN(10), 1E-6);
self::assertEqualswithDelta(0.306762486567556, MathTrig::logBase(1.5, 3.75), 1E-6);
self::assertEqualswithDelta(0.301030, MathTrig::builtinLOG10(2), 1E-6);
self::assertEquals(1, MathTrig::MDETERM([1]));
self::assertEquals(
[[2, 2], [2, 1]],
@ -49,11 +55,14 @@ class MovedFunctionsTest extends TestCase
[[23], [53]],
MathTrig::MMULT([[1, 2], [3, 4]], [[7], [8]])
);
self::assertEquals(1, MathTrig::MOD(5, 2));
self::assertEquals(6, MathTrig::MROUND(7.3, 3));
self::assertEquals(1, MathTrig::MULTINOMIAL(1));
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::assertGreaterThanOrEqual(0, MATHTRIG::RAND());
self::assertEquals('I', MathTrig::ROMAN(1));
self::assertEquals(3.3, MathTrig::builtinROUND(3.27, 1));
self::assertEquals(662, MathTrig::ROUNDDOWN(662.79, 0));

View File

@ -2,25 +2,33 @@
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use PHPUnit\Framework\TestCase;
class PowerTest extends TestCase
class PowerTest extends AllSetupTeardown
{
protected function setUp(): void
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}
/**
* @dataProvider providerPOWER
*
* @param mixed $expectedResult
* @param mixed $base
* @param mixed $exponent
*/
public function testPOWER($expectedResult, ...$args): void
public function testPOWER($expectedResult, $base = 'omitted', $exponent = 'omitted'): void
{
$result = MathTrig::POWER(...$args);
$this->mightHaveException($expectedResult);
$sheet = $this->sheet;
if ($base !== null) {
$sheet->getCell('A1')->setValue($base);
}
if ($exponent !== null) {
$sheet->getCell('A2')->setValue($exponent);
}
if ($base === 'omitted') {
$sheet->getCell('B1')->setValue('=POWER()');
} elseif ($exponent === 'omitted') {
$sheet->getCell('B1')->setValue('=POWER(A1)');
} else {
$sheet->getCell('B1')->setValue('=POWER(A1,A2)');
}
$result = $sheet->getCell('B1')->getCalculatedValue();
self::assertEqualsWithDelta($expectedResult, $result, 1E-12);
}

View File

@ -0,0 +1,46 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
class RandBetweenTest extends AllSetupTeardown
{
/**
* @dataProvider providerRANDBETWEEN
*
* @param mixed $expectedResult
* @param mixed $min
* @param mixed $max
*/
public function testRANDBETWEEN($expectedResult, $min = 'omitted', $max = 'omitted'): void
{
$this->mightHaveException($expectedResult);
$sheet = $this->sheet;
$lower = (int) $min;
$upper = (int) $max;
if ($min !== null) {
$sheet->getCell('A1')->setValue($min);
}
if ($max !== null) {
$sheet->getCell('A2')->setValue($max);
}
if ($min === 'omitted') {
$sheet->getCell('B1')->setValue('=RANDBETWEEN()');
} elseif ($max === 'omitted') {
$sheet->getCell('B1')->setValue('=RANDBETWEEN(A1)');
} else {
$sheet->getCell('B1')->setValue('=RANDBETWEEN(A1,A2)');
}
$result = $sheet->getCell('B1')->getCalculatedValue();
if (is_numeric($expectedResult)) {
self::assertGreaterThanOrEqual($lower, $result);
self::assertLessThanOrEqual($upper, $result);
} else {
self::assertSame($expectedResult, $result);
}
}
public function providerRANDBETWEEN()
{
return require 'tests/data/Calculation/MathTrig/RANDBETWEEN.php';
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
class RandTest extends AllSetupTeardown
{
public function testRand(): void
{
$sheet = $this->sheet;
$sheet->getCell('B1')->setValue('=RAND()');
$result = $sheet->getCell('B1')->getCalculatedValue();
self::assertGreaterThanOrEqual(0, $result);
self::assertLessThanOrEqual(1, $result);
}
public function testRandException(): void
{
$this->mightHaveException('exception');
$sheet = $this->sheet;
$sheet->getCell('B1')->setValue('=RAND(A1)');
$result = $sheet->getCell('B1')->getCalculatedValue();
self::assertEquals(0, $result);
}
}

View File

@ -71,6 +71,11 @@ return [
[
'#VALUE!',
'ABCD',
2,
],
[
'#VALUE!',
3,
'EFGH',
],
[
@ -123,4 +128,8 @@ return [
6,
7,
],
[1, 0, 0],
['#NUM!', 0, 1],
['#VALUE!', 1, true],
['#VALUE!', 1, null],
];

View File

@ -0,0 +1,135 @@
<?php
// NumObjs, NumInSet, Result
return [
[
84,
7,
3,
],
[
36,
8,
2,
],
[
120,
8,
3,
],
[
330,
8,
4,
],
[
171700,
100,
3,
],
[
'#NUM!',
-7,
-10,
],
[
'#NUM!',
-7,
10,
],
[
'#NUM!',
7,
-10,
],
[
4,
2,
3,
],
[
3,
2,
2,
],
[
2,
2,
1,
],
[
1,
2,
0,
],
[
3,
2.5,
2,
],
[
'#VALUE!',
'ABCD',
2,
],
[
'#VALUE!',
3,
'EFGH',
],
[
2002,
10,
5,
],
[
220,
10,
3,
],
[
53130,
21,
5,
],
[
6,
6,
1,
],
[
21,
6,
2,
],
[
56,
6,
3,
],
[
126,
6,
4,
],
[
252,
6,
5,
],
[
462,
6,
6,
],
[
792,
6,
7,
],
[1, 0, 0],
['#NUM!', 0, 1],
['#VALUE!', 1, true],
['#VALUE!', 1, null],
];

View File

@ -129,4 +129,8 @@ return [
481,
143,
],
[13, 182, 481, null, 143],
['#VALUE!', null, null, null, null],
['#VALUE!', null],
['exception'],
];

View File

@ -69,5 +69,7 @@ return [
['#VALUE!', 7, 0, 'X'],
[18, 9.1, 6.7],
[12, 6, null, 4],
['#VALUE!', null, null, null],
['#VALUE!', null],
['#VALUE!', 6, false, 4],
];

View File

@ -1,8 +1,8 @@
<?php
return [
['#VALUE!'], // exception not enough args
['#VALUE!', '"ABC"'],
['exception'], // exception not enough args
['#VALUE!', 'ABC'],
['#NUM!', 0],
['#NUM!', -1],
[-2.302585, 0.1],

View File

@ -341,4 +341,5 @@ return [
200,
3,
],
['exception'],
];

View File

@ -1,8 +1,8 @@
<?php
return [
['#VALUE!'], // exception not enough args
['#VALUE!', '"ABC"'],
['exception'], // exception not enough args
['#VALUE!', 'ABC'],
['#NUM!', 0],
['#NUM!', -1],
[-1, 0.1],

View File

@ -47,8 +47,15 @@ return [
1.3,
],
[
0,
'#VALUE!', // had been 0, which was wrong
'',
1,
],
[0, null, 5],
[0, false, 5],
[1, true, 5],
['#VALUE!', 'XYZ', 2],
['#VALUE!', 2, 'XYZ'],
['exception', 2],
['exception'],
];

View File

@ -406,4 +406,6 @@ return [
4.5,
4.5,
],
['#VALUE!', 'x', 2],
['#VALUE!', 2, 'x'],
];

View File

@ -0,0 +1,19 @@
<?php
return [
['#VALUE!', 'y', 3],
['#VALUE!', 3, 'y'],
['#NUM!', 3, -3],
['#NUM!', 30, 10],
[0, '20', 30],
[0, 20, '30'],
[0, 20, 30],
[0, null, 30],
[0, false, 30],
[0, true, 30],
[0, -30, true],
[0, -30, false],
[0, -30, null],
['exception', -30],
['exception'],
];