@@ -11,9 +11,9 @@ final class Number
11
11
{
12
12
private string $ number ;
13
13
14
- public function __construct (int | float | string $ number = 0 , private readonly int $ precision = 64 )
14
+ public function __construct (self | int | float | string $ number = 0 , private readonly int $ precision = 64 )
15
15
{
16
- $ this ->number = ( string ) $ number ;
16
+ $ this ->number = $ this -> castNumberToString ( $ number) ;
17
17
}
18
18
19
19
public function asString (): string
@@ -35,43 +35,43 @@ public function asInteger(int $roundingMode = \PHP_ROUND_HALF_UP): int
35
35
return (int ) \round ((float ) $ this ->number , mode: $ roundingMode );
36
36
}
37
37
38
- public function add (int | float | string ...$ number ): self
38
+ public function add (self | int | float | string ...$ number ): self
39
39
{
40
40
foreach ($ number as $ numberAsIntegerOrFloatOrString ) {
41
- $ numberAsString = ( string ) $ numberAsIntegerOrFloatOrString ;
41
+ $ numberAsString = $ this -> castNumberToString ( $ numberAsIntegerOrFloatOrString) ;
42
42
43
43
$ this ->number = \bcadd ($ this ->number , $ numberAsString , $ this ->precision );
44
44
}
45
45
46
46
return $ this ;
47
47
}
48
48
49
- public function subtract (int | float | string ...$ number ): self
49
+ public function subtract (self | int | float | string ...$ number ): self
50
50
{
51
51
foreach ($ number as $ numberAsIntegerOrFloatOrString ) {
52
- $ numberAsString = ( string ) $ numberAsIntegerOrFloatOrString ;
52
+ $ numberAsString = $ this -> castNumberToString ( $ numberAsIntegerOrFloatOrString) ;
53
53
54
54
$ this ->number = \bcsub ($ this ->number , $ numberAsString , $ this ->precision );
55
55
}
56
56
57
57
return $ this ;
58
58
}
59
59
60
- public function multiply (int | float | string ...$ number ): self
60
+ public function multiply (self | int | float | string ...$ number ): self
61
61
{
62
62
foreach ($ number as $ numberAsIntegerOrFloatOrString ) {
63
- $ numberAsString = ( string ) $ numberAsIntegerOrFloatOrString ;
63
+ $ numberAsString = $ this -> castNumberToString ( $ numberAsIntegerOrFloatOrString) ;
64
64
65
65
$ this ->number = \bcmul ($ this ->number , $ numberAsString , $ this ->precision );
66
66
}
67
67
68
68
return $ this ;
69
69
}
70
70
71
- public function divide (int | float | string ...$ number ): self
71
+ public function divide (self | int | float | string ...$ number ): self
72
72
{
73
73
foreach ($ number as $ numberAsIntegerOrFloatOrString ) {
74
- $ numberAsString = ( string ) $ numberAsIntegerOrFloatOrString ;
74
+ $ numberAsString = $ this -> castNumberToString ( $ numberAsIntegerOrFloatOrString) ;
75
75
76
76
$ this ->number = \bcdiv ($ this ->number , $ numberAsString , $ this ->precision );
77
77
}
@@ -86,73 +86,100 @@ public function squareRoot(): self
86
86
return $ this ;
87
87
}
88
88
89
- public function modulus (int | float | string $ divisorNumber ): self
89
+ public function modulus (self | int | float | string $ divisorNumber ): self
90
90
{
91
- $ this ->number = \bcmod ($ this ->number , (string ) $ divisorNumber , $ this ->precision );
91
+ $ numberAsString = $ this ->castNumberToString ($ divisorNumber );
92
+
93
+ $ this ->number = \bcmod ($ this ->number , $ numberAsString , $ this ->precision );
92
94
93
95
return $ this ;
94
96
}
95
97
96
- public function raiseToPower (int | float | string $ exponentNumber ): self
98
+ public function raiseToPower (self | int | float | string $ exponentNumber ): self
97
99
{
98
- if (\floor ((float ) $ exponentNumber ) !== (float ) $ exponentNumber ) {
99
- throw new InvalidExponent ('Exponent must be a whole number. Invalid exponent: ' . $ exponentNumber );
100
+ $ numberAsString = $ this ->castNumberToString ($ exponentNumber );
101
+
102
+ if (\floor ((float ) $ numberAsString ) !== (float ) $ numberAsString ) {
103
+ throw new InvalidExponent ('Exponent must be a whole number. Invalid exponent: ' . $ numberAsString );
100
104
}
101
105
102
- $ this ->number = \bcpow ($ this ->number , ( string ) $ exponentNumber , $ this ->precision );
106
+ $ this ->number = \bcpow ($ this ->number , $ numberAsString , $ this ->precision );
103
107
104
108
return $ this ;
105
109
}
106
110
107
111
public function raiseToPowerReduceByModulus (
108
- int | float | string $ exponentNumber ,
109
- int | float | string $ divisorNumber
112
+ self | int | float | string $ exponentNumber ,
113
+ self | int | float | string $ divisorNumber
110
114
): self {
111
- if (\floor ((float ) $ exponentNumber ) !== (float ) $ exponentNumber ) {
112
- throw new InvalidExponent ('Exponent must be a whole number. Invalid exponent: ' . $ exponentNumber );
115
+ $ exponentNumberAsString = $ this ->castNumberToString ($ exponentNumber );
116
+ if (\floor ((float ) $ exponentNumberAsString ) !== (float ) $ exponentNumberAsString ) {
117
+ throw new InvalidExponent ('Exponent must be a whole number. Invalid exponent: ' . $ exponentNumberAsString );
113
118
}
114
119
115
- if (\floor ((float ) $ divisorNumber ) !== (float ) $ divisorNumber ) {
120
+ $ divisorNumberAsString = $ this ->castNumberToString ($ divisorNumber );
121
+ if (\floor ((float ) $ divisorNumberAsString ) !== (float ) $ divisorNumberAsString ) {
116
122
throw new InvalidPowerModulusDivisor ('Divisor must be a whole number. Invalid divisor: ' . $ divisorNumber );
117
123
}
118
124
119
- $ this ->number = \bcpowmod ($ this ->number , ( string ) $ exponentNumber , ( string ) $ divisorNumber , $ this ->precision );
125
+ $ this ->number = \bcpowmod ($ this ->number , $ exponentNumberAsString , $ divisorNumberAsString , $ this ->precision );
120
126
121
127
return $ this ;
122
128
}
123
129
124
- public function isLessThan (int | float | string $ number ): bool
130
+ public function isLessThan (self | int | float | string $ number ): bool
125
131
{
126
- $ result = \bccomp ($ this ->number , (string ) $ number , $ this ->precision );
132
+ $ numberAsString = $ this ->castNumberToString ($ number );
133
+
134
+ $ result = \bccomp ($ this ->number , $ numberAsString , $ this ->precision );
127
135
128
136
return -1 === $ result ;
129
137
}
130
138
131
- public function isGreaterThan (int | float | string $ number ): bool
139
+ public function isGreaterThan (self | int | float | string $ number ): bool
132
140
{
133
- $ result = \bccomp ($ this ->number , (string ) $ number , $ this ->precision );
141
+ $ numberAsString = $ this ->castNumberToString ($ number );
142
+
143
+ $ result = \bccomp ($ this ->number , $ numberAsString , $ this ->precision );
134
144
135
145
return 1 === $ result ;
136
146
}
137
147
138
- public function isEqualTo (int | float | string $ number ): bool
148
+ public function isEqualTo (self | int | float | string $ number ): bool
139
149
{
140
- $ result = \bccomp ($ this ->number , (string ) $ number , $ this ->precision );
150
+ $ numberAsString = $ this ->castNumberToString ($ number );
151
+
152
+ $ result = \bccomp ($ this ->number , $ numberAsString , $ this ->precision );
141
153
142
154
return 0 === $ result ;
143
155
}
144
156
145
- public function isLessThanOrEqualTo (int | float | string $ number ): bool
157
+ public function isLessThanOrEqualTo (self | int | float | string $ number ): bool
146
158
{
147
- $ result = \bccomp ($ this ->number , (string ) $ number , $ this ->precision );
159
+ $ numberAsString = $ this ->castNumberToString ($ number );
160
+
161
+ $ result = \bccomp ($ this ->number , $ numberAsString , $ this ->precision );
148
162
149
163
return -1 === $ result || 0 === $ result ;
150
164
}
151
165
152
- public function isGreaterThanOrEqualTo (int | float | string $ number ): bool
166
+ public function isGreaterThanOrEqualTo (self | int | float | string $ number ): bool
153
167
{
154
- $ result = \bccomp ($ this ->number , (string ) $ number , $ this ->precision );
168
+ $ numberAsString = $ this ->castNumberToString ($ number );
169
+
170
+ $ result = \bccomp ($ this ->number , $ numberAsString , $ this ->precision );
155
171
156
172
return 1 === $ result || 0 === $ result ;
157
173
}
174
+
175
+ private function castNumberToString (self | int | float | string $ number ): string
176
+ {
177
+ if ($ number instanceof self) {
178
+ $ numberAsString = $ number ->asString ();
179
+ } else {
180
+ $ numberAsString = (string ) $ number ;
181
+ }
182
+
183
+ return $ numberAsString ;
184
+ }
158
185
}
0 commit comments