1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bavix\Wallet\Services; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class MathService |
7
|
|
|
* @package Bavix\Wallet\Services |
8
|
|
|
* @codeCoverageIgnore |
9
|
|
|
*/ |
10
|
|
|
class MathService |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @param string|int|float $first |
15
|
|
|
* @param string|int|float $second |
16
|
|
|
* @return string |
17
|
|
|
*/ |
18
|
|
|
public function add($first, $second) |
19
|
|
|
{ |
20
|
|
|
if (config('wallet.bcmath.enabled')) { |
21
|
|
|
return bcadd($first, $second, config('wallet.bcmath.scale', 16)); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
return $first + $second; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string|int|float $first |
29
|
|
|
* @param string|int|float $second |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
|
|
public function sub($first, $second): string |
33
|
|
|
{ |
34
|
|
|
if (config('wallet.bcmath.enabled')) { |
35
|
|
|
return bcsub($first, $second, config('wallet.bcmath.scale', 16)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $first - $second; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string|int|float $first |
43
|
|
|
* @param string|int|float $second |
44
|
|
|
* @return float|int|string|null |
45
|
|
|
*/ |
46
|
|
|
public function div($first, $second): string |
47
|
|
|
{ |
48
|
|
|
if (config('wallet.bcmath.enabled')) { |
49
|
|
|
return bcdiv($first, $second, config('wallet.bcmath.scale', 16)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $first / $second; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string|int|float $first |
57
|
|
|
* @param string|int|float $second |
58
|
|
|
* @return float|int|string |
59
|
|
|
*/ |
60
|
|
|
public function mul($first, $second): string |
61
|
|
|
{ |
62
|
|
|
if (config('wallet.bcmath.enabled')) { |
63
|
|
|
return bcmul($first, $second, config('wallet.bcmath.scale', 16)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $first * $second; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string|int|float $first |
71
|
|
|
* @param string|int|float $second |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function pow($first, $second): string |
75
|
|
|
{ |
76
|
|
|
if (config('wallet.bcmath.enabled')) { |
77
|
|
|
return bcpow($first, $second, config('wallet.bcmath.scale', 16)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $first ** $second; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
|