@@ -18,195 +18,195 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Math |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * Current scale = precision. |
|
| 23 | - * |
|
| 24 | - * @var int |
|
| 25 | - */ |
|
| 26 | - public static $scale = 2; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Standard scale = precision. |
|
| 30 | - * |
|
| 31 | - * @var int |
|
| 32 | - */ |
|
| 33 | - public static $standardScale = 2; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * High scale for more accurate calculations when needed (ratio for example). |
|
| 37 | - * |
|
| 38 | - * @var int |
|
| 39 | - */ |
|
| 40 | - public static $highScale = 6; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Set more accurate calculation. |
|
| 44 | - * |
|
| 45 | - * @param bool $accurate |
|
| 46 | - */ |
|
| 47 | - public static function setAccurate(bool $accurate) |
|
| 48 | - { |
|
| 49 | - if ($accurate) { |
|
| 50 | - static::$scale = static::$highScale; |
|
| 51 | - } else { |
|
| 52 | - static::$scale = static::$standardScale; |
|
| 53 | - } |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Add two numbers. |
|
| 58 | - * |
|
| 59 | - * @params string[] $numbers |
|
| 60 | - * |
|
| 61 | - * @param string[] $numbers |
|
| 62 | - * |
|
| 63 | - * @return string |
|
| 64 | - */ |
|
| 65 | - public static function add(string ...$numbers) |
|
| 66 | - { |
|
| 67 | - if (!isset($numbers[2])) { |
|
| 68 | - // % is not fully supported, so if % appears here for some reason, it is removed. |
|
| 69 | - if(!is_numeric($numbers[0])) { |
|
| 70 | - $numbers[0] = (string) (float) $numbers[0]; |
|
| 71 | - } |
|
| 72 | - return bcadd($numbers[0], $numbers[1], static::$scale); |
|
| 73 | - } |
|
| 74 | - $result = '0'; |
|
| 75 | - foreach ($numbers as $number) { |
|
| 76 | - $result = bcadd($result, $number, static::$scale); |
|
| 77 | - } |
|
| 78 | - return $result; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Subtract two numbers. |
|
| 83 | - * |
|
| 84 | - * @params string[] $numbers |
|
| 85 | - * |
|
| 86 | - * @param string[] $numbers |
|
| 87 | - * |
|
| 88 | - * @return string |
|
| 89 | - */ |
|
| 90 | - public static function sub(string ...$numbers) |
|
| 91 | - { |
|
| 92 | - if (!isset($numbers[2])) { |
|
| 93 | - return bcsub($numbers[0], $numbers[1], static::$scale); |
|
| 94 | - } |
|
| 95 | - $result = $numbers[0]; |
|
| 96 | - for ($i = 1,$len = \count($numbers); $i < $len; ++$i) { |
|
| 97 | - $result = bcsub($result, $numbers[$i], static::$scale); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - return $result; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Multiply numbers. |
|
| 105 | - * |
|
| 106 | - * @param string[] $numbers |
|
| 107 | - * |
|
| 108 | - * @return mixed|string |
|
| 109 | - */ |
|
| 110 | - public static function mul(string ...$numbers) |
|
| 111 | - { |
|
| 112 | - if (!isset($numbers[2])) { |
|
| 113 | - return bcmul($numbers[0], $numbers[1], static::$scale); |
|
| 114 | - } |
|
| 115 | - $result = '1'; |
|
| 116 | - foreach ($numbers as $number) { |
|
| 117 | - $result = bcmul($result, $number, static::$scale); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - return $result; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Divide two numbers. |
|
| 125 | - * |
|
| 126 | - * @params string[] $numbers |
|
| 127 | - * |
|
| 128 | - * @param string[] $numbers |
|
| 129 | - * |
|
| 130 | - * @return string |
|
| 131 | - */ |
|
| 132 | - public static function div(string ...$numbers) |
|
| 133 | - { |
|
| 134 | - if (!isset($numbers[2])) { |
|
| 135 | - if ((float) $numbers[0] !== (float) 0 && (float) $numbers[1] !== (float) 0) { |
|
| 136 | - return bcdiv($numbers[0], $numbers[1], static::$scale); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - return '0'; |
|
| 140 | - } |
|
| 141 | - $result = $numbers[0]; |
|
| 142 | - for ($i = 1,$len = \count($numbers); $i < $len; ++$i) { |
|
| 143 | - if ((float) $numbers[$i] === (float) 0) { |
|
| 144 | - return '0'; |
|
| 145 | - } |
|
| 146 | - $result = bcdiv($result, $numbers[$i], static::$scale); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - return $result; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Compare two numbers. |
|
| 154 | - * |
|
| 155 | - * @param string $left |
|
| 156 | - * @param string $right |
|
| 157 | - * |
|
| 158 | - * @return string |
|
| 159 | - */ |
|
| 160 | - public static function comp(string $left, string $right) |
|
| 161 | - { |
|
| 162 | - return bccomp($left, $right, static::$scale); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Get max number. |
|
| 167 | - * |
|
| 168 | - * @param string ...$numbers |
|
| 169 | - * |
|
| 170 | - * @return string |
|
| 171 | - */ |
|
| 172 | - public static function max(string ...$numbers) |
|
| 173 | - { |
|
| 174 | - $result = '0'; |
|
| 175 | - foreach ($numbers as $number) { |
|
| 176 | - $result = 1 === bccomp($number, $result, static::$scale) ? $number : $result; |
|
| 177 | - } |
|
| 178 | - return $result; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Get min number. |
|
| 183 | - * |
|
| 184 | - * @param string ...$numbers |
|
| 185 | - * |
|
| 186 | - * @return string |
|
| 187 | - */ |
|
| 188 | - public static function min(string ...$numbers) |
|
| 189 | - { |
|
| 190 | - $result = $numbers[0]; |
|
| 191 | - for ($i = 1,$len = \count($numbers); $i < $len; ++$i) { |
|
| 192 | - $result = 1 === bccomp($result, $numbers[$i], static::$scale) ? $numbers[$i] : $result; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - return $result; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * Get percent from value. |
|
| 200 | - * |
|
| 201 | - * @param string $percent |
|
| 202 | - * @param string $from |
|
| 203 | - * |
|
| 204 | - * @return mixed|string |
|
| 205 | - */ |
|
| 206 | - public static function percent(string $percent, string $from) |
|
| 207 | - { |
|
| 208 | - $percent = trim($percent, '%'); |
|
| 209 | - |
|
| 210 | - return static::mul(static::div($from, '100'), $percent); |
|
| 211 | - } |
|
| 21 | + /** |
|
| 22 | + * Current scale = precision. |
|
| 23 | + * |
|
| 24 | + * @var int |
|
| 25 | + */ |
|
| 26 | + public static $scale = 2; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Standard scale = precision. |
|
| 30 | + * |
|
| 31 | + * @var int |
|
| 32 | + */ |
|
| 33 | + public static $standardScale = 2; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * High scale for more accurate calculations when needed (ratio for example). |
|
| 37 | + * |
|
| 38 | + * @var int |
|
| 39 | + */ |
|
| 40 | + public static $highScale = 6; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Set more accurate calculation. |
|
| 44 | + * |
|
| 45 | + * @param bool $accurate |
|
| 46 | + */ |
|
| 47 | + public static function setAccurate(bool $accurate) |
|
| 48 | + { |
|
| 49 | + if ($accurate) { |
|
| 50 | + static::$scale = static::$highScale; |
|
| 51 | + } else { |
|
| 52 | + static::$scale = static::$standardScale; |
|
| 53 | + } |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Add two numbers. |
|
| 58 | + * |
|
| 59 | + * @params string[] $numbers |
|
| 60 | + * |
|
| 61 | + * @param string[] $numbers |
|
| 62 | + * |
|
| 63 | + * @return string |
|
| 64 | + */ |
|
| 65 | + public static function add(string ...$numbers) |
|
| 66 | + { |
|
| 67 | + if (!isset($numbers[2])) { |
|
| 68 | + // % is not fully supported, so if % appears here for some reason, it is removed. |
|
| 69 | + if(!is_numeric($numbers[0])) { |
|
| 70 | + $numbers[0] = (string) (float) $numbers[0]; |
|
| 71 | + } |
|
| 72 | + return bcadd($numbers[0], $numbers[1], static::$scale); |
|
| 73 | + } |
|
| 74 | + $result = '0'; |
|
| 75 | + foreach ($numbers as $number) { |
|
| 76 | + $result = bcadd($result, $number, static::$scale); |
|
| 77 | + } |
|
| 78 | + return $result; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Subtract two numbers. |
|
| 83 | + * |
|
| 84 | + * @params string[] $numbers |
|
| 85 | + * |
|
| 86 | + * @param string[] $numbers |
|
| 87 | + * |
|
| 88 | + * @return string |
|
| 89 | + */ |
|
| 90 | + public static function sub(string ...$numbers) |
|
| 91 | + { |
|
| 92 | + if (!isset($numbers[2])) { |
|
| 93 | + return bcsub($numbers[0], $numbers[1], static::$scale); |
|
| 94 | + } |
|
| 95 | + $result = $numbers[0]; |
|
| 96 | + for ($i = 1,$len = \count($numbers); $i < $len; ++$i) { |
|
| 97 | + $result = bcsub($result, $numbers[$i], static::$scale); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + return $result; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Multiply numbers. |
|
| 105 | + * |
|
| 106 | + * @param string[] $numbers |
|
| 107 | + * |
|
| 108 | + * @return mixed|string |
|
| 109 | + */ |
|
| 110 | + public static function mul(string ...$numbers) |
|
| 111 | + { |
|
| 112 | + if (!isset($numbers[2])) { |
|
| 113 | + return bcmul($numbers[0], $numbers[1], static::$scale); |
|
| 114 | + } |
|
| 115 | + $result = '1'; |
|
| 116 | + foreach ($numbers as $number) { |
|
| 117 | + $result = bcmul($result, $number, static::$scale); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + return $result; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Divide two numbers. |
|
| 125 | + * |
|
| 126 | + * @params string[] $numbers |
|
| 127 | + * |
|
| 128 | + * @param string[] $numbers |
|
| 129 | + * |
|
| 130 | + * @return string |
|
| 131 | + */ |
|
| 132 | + public static function div(string ...$numbers) |
|
| 133 | + { |
|
| 134 | + if (!isset($numbers[2])) { |
|
| 135 | + if ((float) $numbers[0] !== (float) 0 && (float) $numbers[1] !== (float) 0) { |
|
| 136 | + return bcdiv($numbers[0], $numbers[1], static::$scale); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + return '0'; |
|
| 140 | + } |
|
| 141 | + $result = $numbers[0]; |
|
| 142 | + for ($i = 1,$len = \count($numbers); $i < $len; ++$i) { |
|
| 143 | + if ((float) $numbers[$i] === (float) 0) { |
|
| 144 | + return '0'; |
|
| 145 | + } |
|
| 146 | + $result = bcdiv($result, $numbers[$i], static::$scale); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + return $result; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Compare two numbers. |
|
| 154 | + * |
|
| 155 | + * @param string $left |
|
| 156 | + * @param string $right |
|
| 157 | + * |
|
| 158 | + * @return string |
|
| 159 | + */ |
|
| 160 | + public static function comp(string $left, string $right) |
|
| 161 | + { |
|
| 162 | + return bccomp($left, $right, static::$scale); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Get max number. |
|
| 167 | + * |
|
| 168 | + * @param string ...$numbers |
|
| 169 | + * |
|
| 170 | + * @return string |
|
| 171 | + */ |
|
| 172 | + public static function max(string ...$numbers) |
|
| 173 | + { |
|
| 174 | + $result = '0'; |
|
| 175 | + foreach ($numbers as $number) { |
|
| 176 | + $result = 1 === bccomp($number, $result, static::$scale) ? $number : $result; |
|
| 177 | + } |
|
| 178 | + return $result; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Get min number. |
|
| 183 | + * |
|
| 184 | + * @param string ...$numbers |
|
| 185 | + * |
|
| 186 | + * @return string |
|
| 187 | + */ |
|
| 188 | + public static function min(string ...$numbers) |
|
| 189 | + { |
|
| 190 | + $result = $numbers[0]; |
|
| 191 | + for ($i = 1,$len = \count($numbers); $i < $len; ++$i) { |
|
| 192 | + $result = 1 === bccomp($result, $numbers[$i], static::$scale) ? $numbers[$i] : $result; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + return $result; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * Get percent from value. |
|
| 200 | + * |
|
| 201 | + * @param string $percent |
|
| 202 | + * @param string $from |
|
| 203 | + * |
|
| 204 | + * @return mixed|string |
|
| 205 | + */ |
|
| 206 | + public static function percent(string $percent, string $from) |
|
| 207 | + { |
|
| 208 | + $percent = trim($percent, '%'); |
|
| 209 | + |
|
| 210 | + return static::mul(static::div($from, '100'), $percent); |
|
| 211 | + } |
|
| 212 | 212 | } |
@@ -66,7 +66,7 @@ discard block |
||
| 66 | 66 | { |
| 67 | 67 | if (!isset($numbers[2])) { |
| 68 | 68 | // % is not fully supported, so if % appears here for some reason, it is removed. |
| 69 | - if(!is_numeric($numbers[0])) { |
|
| 69 | + if (!is_numeric($numbers[0])) { |
|
| 70 | 70 | $numbers[0] = (string) (float) $numbers[0]; |
| 71 | 71 | } |
| 72 | 72 | return bcadd($numbers[0], $numbers[1], static::$scale); |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | return bcsub($numbers[0], $numbers[1], static::$scale); |
| 94 | 94 | } |
| 95 | 95 | $result = $numbers[0]; |
| 96 | - for ($i = 1,$len = \count($numbers); $i < $len; ++$i) { |
|
| 96 | + for ($i = 1, $len = \count($numbers); $i < $len; ++$i) { |
|
| 97 | 97 | $result = bcsub($result, $numbers[$i], static::$scale); |
| 98 | 98 | } |
| 99 | 99 | |
@@ -139,7 +139,7 @@ discard block |
||
| 139 | 139 | return '0'; |
| 140 | 140 | } |
| 141 | 141 | $result = $numbers[0]; |
| 142 | - for ($i = 1,$len = \count($numbers); $i < $len; ++$i) { |
|
| 142 | + for ($i = 1, $len = \count($numbers); $i < $len; ++$i) { |
|
| 143 | 143 | if ((float) $numbers[$i] === (float) 0) { |
| 144 | 144 | return '0'; |
| 145 | 145 | } |
@@ -188,7 +188,7 @@ discard block |
||
| 188 | 188 | public static function min(string ...$numbers) |
| 189 | 189 | { |
| 190 | 190 | $result = $numbers[0]; |
| 191 | - for ($i = 1,$len = \count($numbers); $i < $len; ++$i) { |
|
| 191 | + for ($i = 1, $len = \count($numbers); $i < $len; ++$i) { |
|
| 192 | 192 | $result = 1 === bccomp($result, $numbers[$i], static::$scale) ? $numbers[$i] : $result; |
| 193 | 193 | } |
| 194 | 194 | |