| 1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
|
|
|
4 |
|
|
|
5 |
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
|
|
|
6 |
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
|
|
|
7 |
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
|
|
|
8 |
use 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|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(mixed $number, mixed $significance = null)
|
|
|
40 |
{
|
|
|
41 |
if (is_array($number) || is_array($significance)) {
|
|
|
42 |
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance);
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
if ($significance === null) {
|
|
|
46 |
self::floorCheck1Arg();
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
try {
|
|
|
50 |
$number = Helpers::validateNumericNullBool($number);
|
|
|
51 |
$significance = Helpers::validateNumericNullSubstitution($significance, ($number < 0) ? -1 : 1);
|
|
|
52 |
} catch (Exception $e) {
|
|
|
53 |
return $e->getMessage();
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
return self::argumentsOk((float) $number, (float) $significance);
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* FLOOR.MATH.
|
|
|
61 |
*
|
|
|
62 |
* Round a number down to the nearest integer or to the nearest multiple of significance.
|
|
|
63 |
*
|
|
|
64 |
* Excel Function:
|
|
|
65 |
* FLOOR.MATH(number[,significance[,mode]])
|
|
|
66 |
*
|
|
|
67 |
* @param mixed $number Number to round
|
|
|
68 |
* Or can be an array of values
|
|
|
69 |
* @param mixed $significance Significance
|
|
|
70 |
* Or can be an array of values
|
|
|
71 |
* @param mixed $mode direction to round negative numbers
|
|
|
72 |
* Or can be an array of values
|
|
|
73 |
*
|
|
|
74 |
* @return array|float|string Rounded Number, or a string containing an error
|
|
|
75 |
* If an array of numbers is passed as an argument, then the returned result will also be an array
|
|
|
76 |
* with the same dimensions
|
|
|
77 |
*/
|
|
|
78 |
public static function math(mixed $number, mixed $significance = null, mixed $mode = 0)
|
|
|
79 |
{
|
|
|
80 |
if (is_array($number) || is_array($significance) || is_array($mode)) {
|
|
|
81 |
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance, $mode);
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
try {
|
|
|
85 |
$number = Helpers::validateNumericNullBool($number);
|
|
|
86 |
$significance = Helpers::validateNumericNullSubstitution($significance, ($number < 0) ? -1 : 1);
|
|
|
87 |
$mode = Helpers::validateNumericNullSubstitution($mode, null);
|
|
|
88 |
} catch (Exception $e) {
|
|
|
89 |
return $e->getMessage();
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
return self::argsOk((float) $number, (float) $significance, (int) $mode);
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* FLOOR.PRECISE.
|
|
|
97 |
*
|
|
|
98 |
* Rounds number down, toward zero, to the nearest multiple of significance.
|
|
|
99 |
*
|
|
|
100 |
* Excel Function:
|
|
|
101 |
* FLOOR.PRECISE(number[,significance])
|
|
|
102 |
*
|
|
|
103 |
* @param array|float $number Number to round
|
|
|
104 |
* Or can be an array of values
|
|
|
105 |
* @param array|float $significance Significance
|
|
|
106 |
* Or can be an array of values
|
|
|
107 |
*
|
|
|
108 |
* @return array|float|string Rounded Number, or a string containing an error
|
|
|
109 |
* If an array of numbers is passed as an argument, then the returned result will also be an array
|
|
|
110 |
* with the same dimensions
|
|
|
111 |
*/
|
|
|
112 |
public static function precise($number, $significance = 1)
|
|
|
113 |
{
|
|
|
114 |
if (is_array($number) || is_array($significance)) {
|
|
|
115 |
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance);
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
try {
|
|
|
119 |
$number = Helpers::validateNumericNullBool($number);
|
|
|
120 |
$significance = Helpers::validateNumericNullSubstitution($significance, null);
|
|
|
121 |
} catch (Exception $e) {
|
|
|
122 |
return $e->getMessage();
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
return self::argumentsOkPrecise((float) $number, (float) $significance);
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
/**
|
|
|
129 |
* Avoid Scrutinizer problems concerning complexity.
|
|
|
130 |
*/
|
|
|
131 |
private static function argumentsOkPrecise(float $number, float $significance): string|float
|
|
|
132 |
{
|
|
|
133 |
if ($significance == 0.0) {
|
|
|
134 |
return ExcelError::DIV0();
|
|
|
135 |
}
|
|
|
136 |
if ($number == 0.0) {
|
|
|
137 |
return 0.0;
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
return floor($number / abs($significance)) * abs($significance);
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
/**
|
|
|
144 |
* Avoid Scrutinizer complexity problems.
|
|
|
145 |
*
|
|
|
146 |
* @return float|string Rounded Number, or a string containing an error
|
|
|
147 |
*/
|
|
|
148 |
private static function argsOk(float $number, float $significance, int $mode): string|float
|
|
|
149 |
{
|
|
|
150 |
if (!$significance) {
|
|
|
151 |
return ExcelError::DIV0();
|
|
|
152 |
}
|
|
|
153 |
if (!$number) {
|
|
|
154 |
return 0.0;
|
|
|
155 |
}
|
|
|
156 |
if (self::floorMathTest($number, $significance, $mode)) {
|
|
|
157 |
return ceil($number / $significance) * $significance;
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
return floor($number / $significance) * $significance;
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
/**
|
|
|
164 |
* Let FLOORMATH complexity pass Scrutinizer.
|
|
|
165 |
*/
|
|
|
166 |
private static function floorMathTest(float $number, float $significance, int $mode): bool
|
|
|
167 |
{
|
|
|
168 |
return Helpers::returnSign($significance) == -1 || (Helpers::returnSign($number) == -1 && !empty($mode));
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
/**
|
|
|
172 |
* Avoid Scrutinizer problems concerning complexity.
|
|
|
173 |
*/
|
|
|
174 |
private static function argumentsOk(float $number, float $significance): string|float
|
|
|
175 |
{
|
|
|
176 |
if ($significance == 0.0) {
|
|
|
177 |
return ExcelError::DIV0();
|
|
|
178 |
}
|
|
|
179 |
if ($number == 0.0) {
|
|
|
180 |
return 0.0;
|
|
|
181 |
}
|
|
|
182 |
if (Helpers::returnSign($significance) == 1) {
|
|
|
183 |
return floor($number / $significance) * $significance;
|
|
|
184 |
}
|
|
|
185 |
if (Helpers::returnSign($number) == -1 && Helpers::returnSign($significance) == -1) {
|
|
|
186 |
return floor($number / $significance) * $significance;
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
return ExcelError::NAN();
|
|
|
190 |
}
|
|
|
191 |
}
|