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