1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: yiranzai |
5
|
|
|
* Date: 19-3-22 |
6
|
|
|
* Time: 上午11:02 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Yiranzai\Tools; |
10
|
|
|
|
11
|
|
|
class Math |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* 四舍五入 格式化除法 |
16
|
|
|
* @param $divisor |
17
|
|
|
* @param $divided |
18
|
|
|
* @param int $scale |
19
|
|
|
* @return int|string |
20
|
|
|
*/ |
21
|
3 |
|
public static function formatDiv($divisor, $divided, int $scale = 2) |
22
|
|
|
{ |
23
|
3 |
|
if (empty((int)$divided)) { |
24
|
3 |
|
return sprintf('%.' . $scale . 'f', 0); |
25
|
|
|
} |
26
|
3 |
|
return $scale === 0 ? |
27
|
3 |
|
bcdiv($divisor, $divided) : |
28
|
3 |
|
sprintf('%.' . $scale . 'f', bcdiv($divisor, $divided, $scale + 1)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* 四舍五入 格式化取余(模运算) |
34
|
|
|
* @param $leftOperand |
35
|
|
|
* @param $rightOperand |
36
|
|
|
* @return int|string |
37
|
|
|
*/ |
38
|
3 |
|
public static function formatMod($leftOperand, $rightOperand) |
39
|
|
|
{ |
40
|
3 |
|
if (empty((int)$rightOperand)) { |
41
|
3 |
|
return '0'; |
42
|
|
|
} |
43
|
3 |
|
return bcmod($leftOperand, $rightOperand); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* 四舍五入 格式化乘法 |
49
|
|
|
* @param $leftOperand |
50
|
|
|
* @param $rightOperand |
51
|
|
|
* @param int $scale |
52
|
|
|
* @return int|string |
53
|
|
|
*/ |
54
|
3 |
|
public static function formatMul($leftOperand, $rightOperand, int $scale = 2) |
55
|
|
|
{ |
56
|
3 |
|
return $scale === 0 ? |
57
|
3 |
|
bcmul($leftOperand, $rightOperand) : |
58
|
3 |
|
sprintf('%.' . $scale . 'f', bcmul($leftOperand, $rightOperand, $scale + 1)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* 四舍五入 格式化减法 |
64
|
|
|
* @param $leftOperand |
65
|
|
|
* @param $rightOperand |
66
|
|
|
* @param int $scale |
67
|
|
|
* @return int|string |
68
|
|
|
*/ |
69
|
3 |
|
public static function formatSub($leftOperand, $rightOperand, int $scale = 2) |
70
|
|
|
{ |
71
|
3 |
|
return $scale === 0 ? |
72
|
3 |
|
bcsub($leftOperand, $rightOperand) : |
73
|
3 |
|
sprintf('%.' . $scale . 'f', bcsub($leftOperand, $rightOperand, $scale + 1)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* 四舍五入 格式化加法 |
79
|
|
|
* @param $leftOperand |
80
|
|
|
* @param $rightOperand |
81
|
|
|
* @param int $scale |
82
|
|
|
* @return int|string |
83
|
|
|
*/ |
84
|
3 |
|
public static function formatAdd($leftOperand, $rightOperand, int $scale = 2) |
85
|
|
|
{ |
86
|
3 |
|
return $scale === 0 ? |
87
|
3 |
|
bcadd($leftOperand, $rightOperand) : |
88
|
3 |
|
sprintf('%.' . $scale . 'f', bcadd($leftOperand, $rightOperand, $scale + 1)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* 求两个数的最大公约数 |
93
|
|
|
* |
94
|
|
|
* @param $a |
95
|
|
|
* @param $b |
96
|
|
|
* @return int |
97
|
|
|
*/ |
98
|
3 |
|
public static function gcd(int $a, int $b): int |
99
|
|
|
{ |
100
|
3 |
|
if ($a === 0 || $b === 0) { |
101
|
3 |
|
return abs(max(abs($a), abs($b))); |
102
|
|
|
} |
103
|
|
|
|
104
|
3 |
|
$r = $a % $b; |
105
|
3 |
|
return ($r !== 0) ? |
106
|
3 |
|
self::gcd($b, $r) : |
107
|
3 |
|
abs($b); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* 求一个数组的最大公约数 |
112
|
|
|
* |
113
|
|
|
* @param array $array |
114
|
|
|
* @param int $a |
115
|
|
|
* @return int |
116
|
|
|
*/ |
117
|
3 |
|
public static function gcdArray(array $array, $a = 0): int |
118
|
|
|
{ |
119
|
3 |
|
$b = array_pop($array); |
120
|
3 |
|
return ($b === null) ? |
121
|
3 |
|
(int)$a : |
122
|
3 |
|
self::gcdArray($array, self::gcd($a, $b)); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|