Skip to content

Commit f8a34e1

Browse files
committed
Allows instance of Number in methods taking a number
1 parent 73e27be commit f8a34e1

File tree

2 files changed

+251
-36
lines changed

2 files changed

+251
-36
lines changed

src/ElliotJReed/Maths/Number.php

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ final class Number
1111
{
1212
private string $number;
1313

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)
1515
{
16-
$this->number = (string) $number;
16+
$this->number = $this->castNumberToString($number);
1717
}
1818

1919
public function asString(): string
@@ -35,43 +35,43 @@ public function asInteger(int $roundingMode = \PHP_ROUND_HALF_UP): int
3535
return (int) \round((float) $this->number, mode: $roundingMode);
3636
}
3737

38-
public function add(int | float | string ...$number): self
38+
public function add(self | int | float | string ...$number): self
3939
{
4040
foreach ($number as $numberAsIntegerOrFloatOrString) {
41-
$numberAsString = (string) $numberAsIntegerOrFloatOrString;
41+
$numberAsString = $this->castNumberToString($numberAsIntegerOrFloatOrString);
4242

4343
$this->number = \bcadd($this->number, $numberAsString, $this->precision);
4444
}
4545

4646
return $this;
4747
}
4848

49-
public function subtract(int | float | string ...$number): self
49+
public function subtract(self | int | float | string ...$number): self
5050
{
5151
foreach ($number as $numberAsIntegerOrFloatOrString) {
52-
$numberAsString = (string) $numberAsIntegerOrFloatOrString;
52+
$numberAsString = $this->castNumberToString($numberAsIntegerOrFloatOrString);
5353

5454
$this->number = \bcsub($this->number, $numberAsString, $this->precision);
5555
}
5656

5757
return $this;
5858
}
5959

60-
public function multiply(int | float | string ...$number): self
60+
public function multiply(self | int | float | string ...$number): self
6161
{
6262
foreach ($number as $numberAsIntegerOrFloatOrString) {
63-
$numberAsString = (string) $numberAsIntegerOrFloatOrString;
63+
$numberAsString = $this->castNumberToString($numberAsIntegerOrFloatOrString);
6464

6565
$this->number = \bcmul($this->number, $numberAsString, $this->precision);
6666
}
6767

6868
return $this;
6969
}
7070

71-
public function divide(int | float | string ...$number): self
71+
public function divide(self | int | float | string ...$number): self
7272
{
7373
foreach ($number as $numberAsIntegerOrFloatOrString) {
74-
$numberAsString = (string) $numberAsIntegerOrFloatOrString;
74+
$numberAsString = $this->castNumberToString($numberAsIntegerOrFloatOrString);
7575

7676
$this->number = \bcdiv($this->number, $numberAsString, $this->precision);
7777
}
@@ -86,73 +86,100 @@ public function squareRoot(): self
8686
return $this;
8787
}
8888

89-
public function modulus(int | float | string $divisorNumber): self
89+
public function modulus(self | int | float | string $divisorNumber): self
9090
{
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);
9294

9395
return $this;
9496
}
9597

96-
public function raiseToPower(int | float | string $exponentNumber): self
98+
public function raiseToPower(self | int | float | string $exponentNumber): self
9799
{
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);
100104
}
101105

102-
$this->number = \bcpow($this->number, (string) $exponentNumber, $this->precision);
106+
$this->number = \bcpow($this->number, $numberAsString, $this->precision);
103107

104108
return $this;
105109
}
106110

107111
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
110114
): 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);
113118
}
114119

115-
if (\floor((float) $divisorNumber) !== (float) $divisorNumber) {
120+
$divisorNumberAsString = $this->castNumberToString($divisorNumber);
121+
if (\floor((float) $divisorNumberAsString) !== (float) $divisorNumberAsString) {
116122
throw new InvalidPowerModulusDivisor('Divisor must be a whole number. Invalid divisor: ' . $divisorNumber);
117123
}
118124

119-
$this->number = \bcpowmod($this->number, (string) $exponentNumber, (string) $divisorNumber, $this->precision);
125+
$this->number = \bcpowmod($this->number, $exponentNumberAsString, $divisorNumberAsString, $this->precision);
120126

121127
return $this;
122128
}
123129

124-
public function isLessThan(int | float | string $number): bool
130+
public function isLessThan(self | int | float | string $number): bool
125131
{
126-
$result = \bccomp($this->number, (string) $number, $this->precision);
132+
$numberAsString = $this->castNumberToString($number);
133+
134+
$result = \bccomp($this->number, $numberAsString, $this->precision);
127135

128136
return -1 === $result;
129137
}
130138

131-
public function isGreaterThan(int | float | string $number): bool
139+
public function isGreaterThan(self | int | float | string $number): bool
132140
{
133-
$result = \bccomp($this->number, (string) $number, $this->precision);
141+
$numberAsString = $this->castNumberToString($number);
142+
143+
$result = \bccomp($this->number, $numberAsString, $this->precision);
134144

135145
return 1 === $result;
136146
}
137147

138-
public function isEqualTo(int | float | string $number): bool
148+
public function isEqualTo(self | int | float | string $number): bool
139149
{
140-
$result = \bccomp($this->number, (string) $number, $this->precision);
150+
$numberAsString = $this->castNumberToString($number);
151+
152+
$result = \bccomp($this->number, $numberAsString, $this->precision);
141153

142154
return 0 === $result;
143155
}
144156

145-
public function isLessThanOrEqualTo(int | float | string $number): bool
157+
public function isLessThanOrEqualTo(self | int | float | string $number): bool
146158
{
147-
$result = \bccomp($this->number, (string) $number, $this->precision);
159+
$numberAsString = $this->castNumberToString($number);
160+
161+
$result = \bccomp($this->number, $numberAsString, $this->precision);
148162

149163
return -1 === $result || 0 === $result;
150164
}
151165

152-
public function isGreaterThanOrEqualTo(int | float | string $number): bool
166+
public function isGreaterThanOrEqualTo(self | int | float | string $number): bool
153167
{
154-
$result = \bccomp($this->number, (string) $number, $this->precision);
168+
$numberAsString = $this->castNumberToString($number);
169+
170+
$result = \bccomp($this->number, $numberAsString, $this->precision);
155171

156172
return 1 === $result || 0 === $result;
157173
}
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+
}
158185
}

0 commit comments

Comments
 (0)