PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
tablepress / libraries / vendor / PhpSpreadsheet / Calculation / MathTrig / Floor.php

Floor.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Calculation/MathTrig/Floor.php

230 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 Floor
11 {
12 use ArrayEnabled;
13
14 private static function floorCheck1Arg(): void
15 {
16 $compatibility = Functions::getCompatibilityMode();
17 if ($compatibility === Functions::COMPATIBILITY_EXCEL) {
18 throw new Exception('Excel requires 2 arguments for FLOOR');
19 }
20 }
21
22 /**
23 * FLOOR.
24 *
25 * Rounds number down, toward zero, to the nearest multiple of significance.
26 *
27 * Excel Function:
28 * FLOOR(number[,significance])
29 *
30 * @param mixed $number Expect float. Number to round
31 * Or can be an array of values
32 * @param mixed $significance Expect float. Significance
33 * Or can be an array of values
34 *
35 * @return array<mixed>|float|string Rounded Number, or a string containing an error
36 * If an array of numbers is passed as an argument, then the returned result will also be an array
37 * with the same dimensions
38 */
39 public static function floor($number, $significance = null)
40 {
41 if (is_array($number) || is_array($significance)) {
42 return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance);
43 }
44
45 /*
46 // Allow one argument only, for compatibility with Google Sheets, when importing an Excel file.
47 if ($significance === null) {
48 self::floorCheck1Arg();
49 }
50 */
51
52 try {
53 $number = Helpers::validateNumericNullBool($number);
54 $significance = Helpers::validateNumericNullSubstitution($significance, ($number < 0) ? -1 : 1);
55 } catch (Exception $e) {
56 return $e->getMessage();
57 }
58
59 return self::argumentsOk((float) $number, (float) $significance);
60 }
61
62 /**
63 * FLOOR.MATH.
64 *
65 * Round a number down to the nearest integer or to the nearest multiple of significance.
66 *
67 * Excel Function:
68 * FLOOR.MATH(number[,significance[,mode]])
69 *
70 * @param mixed $number Number to round
71 * Or can be an array of values
72 * @param mixed $significance Significance
73 * Or can be an array of values
74 * @param mixed $mode direction to round negative numbers
75 * Or can be an array of values
76 *
77 * @return array<mixed>|float|string Rounded Number, or a string containing an error
78 * If an array of numbers is passed as an argument, then the returned result will also be an array
79 * with the same dimensions
80 */
81 public static function math($number, $significance = null, $mode = 0, bool $checkSigns = false)
82 {
83 if (is_array($number) || is_array($significance) || is_array($mode)) {
84 return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance, $mode);
85 }
86
87 try {
88 $number = Helpers::validateNumericNullBool($number);
89 $significance = Helpers::validateNumericNullSubstitution($significance, ($number < 0) ? -1 : 1);
90 $mode = Helpers::validateNumericNullSubstitution($mode, null);
91 } catch (Exception $e) {
92 return $e->getMessage();
93 }
94
95 if (empty($significance * $number)) {
96 return 0.0;
97 }
98 if ($checkSigns) {
99 if (($number > 0 && $significance < 0) || ($number < 0 && $significance > 0)) {
100 return ExcelError::NAN();
101 }
102 }
103
104 return self::argsOk((float) $number, (float) $significance, (int) $mode);
105 }
106
107 /**
108 * FLOOR.ODS, pseudo-function - FLOOR as implemented in ODS.
109 *
110 * Round a number down to the nearest integer or to the nearest multiple of significance.
111 *
112 * ODS Function (theoretical):
113 * FLOOR.ODS(number[,significance[,mode]])
114 *
115 * @param mixed $number Number to round
116 * @param mixed $significance Significance
117 * @param mixed $mode direction to round negative numbers
118 *
119 * @return array<mixed>|float|string Rounded Number, or a string containing an error
120 */
121 public static function mathOds($number, $significance = null, $mode = 0)
122 {
123 return self::math($number, $significance, $mode, true);
124 }
125
126 /**
127 * FLOOR.PRECISE.
128 *
129 * Rounds number down, toward zero, to the nearest multiple of significance.
130 *
131 * Excel Function:
132 * FLOOR.PRECISE(number[,significance])
133 *
134 * @param array<mixed>|float $number Number to round
135 * Or can be an array of values
136 * @param array<mixed>|float $significance Significance
137 * Or can be an array of values
138 *
139 * @return array<mixed>|float|string Rounded Number, or a string containing an error
140 * If an array of numbers is passed as an argument, then the returned result will also be an array
141 * with the same dimensions
142 */
143 public static function precise($number, $significance = 1)
144 {
145 if (is_array($number) || is_array($significance)) {
146 return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance);
147 }
148
149 try {
150 $number = Helpers::validateNumericNullBool($number);
151 $significance = Helpers::validateNumericNullSubstitution($significance, null);
152 } catch (Exception $e) {
153 return $e->getMessage();
154 }
155 if (!$significance) {
156 return 0.0;
157 }
158
159 return self::argumentsOkPrecise((float) $number, (float) $significance);
160 }
161
162 /**
163 * Avoid Scrutinizer problems concerning complexity.
164 * @return string|float
165 */
166 private static function argumentsOkPrecise(float $number, float $significance)
167 {
168 if ($significance == 0.0) {
169 return ExcelError::DIV0();
170 }
171 if ($number == 0.0) {
172 return 0.0;
173 }
174
175 return floor($number / abs($significance)) * abs($significance);
176 }
177
178 /**
179 * Avoid Scrutinizer complexity problems.
180 *
181 * @return float|string Rounded Number, or a string containing an error
182 */
183 private static function argsOk(float $number, float $significance, int $mode)
184 {
185 if (!$significance) {
186 return ExcelError::DIV0();
187 }
188 if (!$number) {
189 return 0.0;
190 }
191 if (self::floorMathTest($number, $significance, $mode)) {
192 return ceil($number / $significance) * $significance;
193 }
194
195 return floor($number / $significance) * $significance;
196 }
197
198 /**
199 * Let FLOORMATH complexity pass Scrutinizer.
200 */
201 private static function floorMathTest(float $number, float $significance, int $mode): bool
202 {
203 return Helpers::returnSign($significance) == -1 || (Helpers::returnSign($number) == -1 && !empty($mode));
204 }
205
206 /**
207 * Avoid Scrutinizer problems concerning complexity.
208 * @return string|float
209 */
210 private static function argumentsOk(float $number, float $significance)
211 {
212 if ($significance == 0.0) {
213 return ExcelError::DIV0();
214 }
215 if ($number == 0.0) {
216 return 0.0;
217 }
218 $signSig = Helpers::returnSign($significance);
219 $signNum = Helpers::returnSign($number);
220 if (
221 ($signSig === 1 && ($signNum === 1 || Functions::getCompatibilityMode() !== Functions::COMPATIBILITY_GNUMERIC))
222 || ($signNum === -1 && $signSig === -1)
223 ) {
224 return floor($number / $significance) * $significance;
225 }
226
227 return ExcelError::NAN();
228 }
229 }
230