| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\MathTrig; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Exception; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Functions; |
| 8 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
| 9 |
|
| 10 |
class Operations |
| 11 |
{ |
| 12 |
use ArrayEnabled; |
| 13 |
|
| 14 |
/** |
| 15 |
* MOD. |
| 16 |
* |
| 17 |
* @param mixed $dividend Dividend |
| 18 |
* Or can be an array of values |
| 19 |
* @param mixed $divisor Divisor |
| 20 |
* Or can be an array of values |
| 21 |
* |
| 22 |
* @return array<mixed>|float|string Remainder, or a string containing an error |
| 23 |
* If an array of numbers is passed as an argument, then the returned result will also be an array |
| 24 |
* with the same dimensions |
| 25 |
*/ |
| 26 |
public static function mod($dividend, $divisor) |
| 27 |
{ |
| 28 |
if (is_array($dividend) || is_array($divisor)) { |
| 29 |
return self::evaluateArrayArguments([self::class, __FUNCTION__], $dividend, $divisor); |
| 30 |
} |
| 31 |
|
| 32 |
try { |
| 33 |
$dividend = Helpers::validateNumericNullBool($dividend); |
| 34 |
$divisor = Helpers::validateNumericNullBool($divisor); |
| 35 |
Helpers::validateNotZero($divisor); |
| 36 |
} catch (Exception $e) { |
| 37 |
return $e->getMessage(); |
| 38 |
} |
| 39 |
|
| 40 |
if (($dividend < 0.0) && ($divisor > 0.0)) { |
| 41 |
return $divisor - fmod(abs($dividend), $divisor); |
| 42 |
} |
| 43 |
if (($dividend > 0.0) && ($divisor < 0.0)) { |
| 44 |
return $divisor + fmod($dividend, abs($divisor)); |
| 45 |
} |
| 46 |
|
| 47 |
return fmod($dividend, $divisor); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* POWER. |
| 52 |
* |
| 53 |
* Computes x raised to the power y. |
| 54 |
* |
| 55 |
* @param null|array<mixed>|bool|float|int|string $x Or can be an array of values |
| 56 |
* @param null|array<mixed>|bool|float|int|string $y Or can be an array of values |
| 57 |
* |
| 58 |
* @return array<mixed>|float|int|string The result, or a string containing an error |
| 59 |
* If an array of numbers is passed as an argument, then the returned result will also be an array |
| 60 |
* with the same dimensions |
| 61 |
*/ |
| 62 |
public static function power($x, $y) |
| 63 |
{ |
| 64 |
if (is_array($x) || is_array($y)) { |
| 65 |
return self::evaluateArrayArguments([self::class, __FUNCTION__], $x, $y); |
| 66 |
} |
| 67 |
|
| 68 |
try { |
| 69 |
$x = Helpers::validateNumericNullBool($x); |
| 70 |
$y = Helpers::validateNumericNullBool($y); |
| 71 |
} catch (Exception $e) { |
| 72 |
return $e->getMessage(); |
| 73 |
} |
| 74 |
|
| 75 |
// Validate parameters |
| 76 |
if (!$x && !$y) { |
| 77 |
return ExcelError::NAN(); |
| 78 |
} |
| 79 |
if (!$x && $y < 0.0) { |
| 80 |
return ExcelError::DIV0(); |
| 81 |
} |
| 82 |
|
| 83 |
// Return |
| 84 |
$result = $x ** $y; |
| 85 |
|
| 86 |
return Helpers::numberOrNan($result); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* PRODUCT. |
| 91 |
* |
| 92 |
* PRODUCT returns the product of all the values and cells referenced in the argument list. |
| 93 |
* |
| 94 |
* Excel Function: |
| 95 |
* PRODUCT(value1[,value2[, ...]]) |
| 96 |
* |
| 97 |
* @param mixed ...$args Data values |
| 98 |
* @return string|float |
| 99 |
*/ |
| 100 |
public static function product(...$args) |
| 101 |
{ |
| 102 |
$args = array_filter( |
| 103 |
Functions::flattenArray($args), |
| 104 |
fn ($value): bool => $value !== null |
| 105 |
); |
| 106 |
|
| 107 |
// Return value |
| 108 |
$returnValue = (count($args) === 0) ? 0.0 : 1.0; |
| 109 |
|
| 110 |
// Loop through arguments |
| 111 |
foreach ($args as $arg) { |
| 112 |
// Is it a numeric value? |
| 113 |
if (is_numeric($arg)) { |
| 114 |
$returnValue *= $arg; |
| 115 |
} else { |
| 116 |
return ExcelError::throwError($arg); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
return (float) $returnValue; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* QUOTIENT. |
| 125 |
* |
| 126 |
* QUOTIENT function returns the integer portion of a division. Numerator is the divided number |
| 127 |
* and denominator is the divisor. |
| 128 |
* |
| 129 |
* Excel Function: |
| 130 |
* QUOTIENT(value1,value2) |
| 131 |
* |
| 132 |
* @param mixed $numerator Expect float|int |
| 133 |
* Or can be an array of values |
| 134 |
* @param mixed $denominator Expect float|int |
| 135 |
* Or can be an array of values |
| 136 |
* |
| 137 |
* @return array<mixed>|int|string If an array of numbers is passed as an argument, then the returned result will also be an array |
| 138 |
* with the same dimensions |
| 139 |
*/ |
| 140 |
public static function quotient($numerator, $denominator) |
| 141 |
{ |
| 142 |
if (is_array($numerator) || is_array($denominator)) { |
| 143 |
return self::evaluateArrayArguments([self::class, __FUNCTION__], $numerator, $denominator); |
| 144 |
} |
| 145 |
|
| 146 |
try { |
| 147 |
$numerator = Helpers::validateNumericNullSubstitution($numerator, 0); |
| 148 |
$denominator = Helpers::validateNumericNullSubstitution($denominator, 0); |
| 149 |
Helpers::validateNotZero($denominator); |
| 150 |
} catch (Exception $e) { |
| 151 |
return $e->getMessage(); |
| 152 |
} |
| 153 |
|
| 154 |
return (int) ($numerator / $denominator); |
| 155 |
} |
| 156 |
} |
| 157 |
|