88use Brick \Math \Exception \MathException ;
99use Brick \Math \Exception \NumberFormatException ;
1010use Brick \Math \Exception \RoundingNecessaryException ;
11+ use InvalidArgumentException ;
12+ use LogicException ;
1113use Override ;
1214
1315/**
@@ -46,21 +48,15 @@ protected function __construct(BigInteger $numerator, BigInteger $denominator, b
4648 }
4749
4850 if ($ denominator ->isNegative ()) {
49- $ numerator = $ numerator ->negated ();
51+ $ numerator = $ numerator ->negated ();
5052 $ denominator = $ denominator ->negated ();
5153 }
5254 }
5355
54- $ this ->numerator = $ numerator ;
56+ $ this ->numerator = $ numerator ;
5557 $ this ->denominator = $ denominator ;
5658 }
5759
58- #[Override]
59- protected static function from (BigNumber $ number ): static
60- {
61- return $ number ->toBigRational ();
62- }
63-
6460 /**
6561 * Creates a BigRational out of a numerator and a denominator.
6662 *
@@ -79,8 +75,8 @@ protected static function from(BigNumber $number): static
7975 public static function nd (
8076 BigNumber |int |float |string $ numerator ,
8177 BigNumber |int |float |string $ denominator ,
82- ) : BigRational {
83- $ numerator = BigInteger::of ($ numerator );
78+ ): BigRational {
79+ $ numerator = BigInteger::of ($ numerator );
8480 $ denominator = BigInteger::of ($ denominator );
8581
8682 return new BigRational ($ numerator , $ denominator , true );
@@ -91,7 +87,7 @@ public static function nd(
9187 *
9288 * @pure
9389 */
94- public static function zero () : BigRational
90+ public static function zero (): BigRational
9591 {
9692 /** @var BigRational|null $zero */
9793 static $ zero ;
@@ -108,7 +104,7 @@ public static function zero() : BigRational
108104 *
109105 * @pure
110106 */
111- public static function one () : BigRational
107+ public static function one (): BigRational
112108 {
113109 /** @var BigRational|null $one */
114110 static $ one ;
@@ -125,7 +121,7 @@ public static function one() : BigRational
125121 *
126122 * @pure
127123 */
128- public static function ten () : BigRational
124+ public static function ten (): BigRational
129125 {
130126 /** @var BigRational|null $ten */
131127 static $ ten ;
@@ -140,15 +136,15 @@ public static function ten() : BigRational
140136 /**
141137 * @pure
142138 */
143- public function getNumerator () : BigInteger
139+ public function getNumerator (): BigInteger
144140 {
145141 return $ this ->numerator ;
146142 }
147143
148144 /**
149145 * @pure
150146 */
151- public function getDenominator () : BigInteger
147+ public function getDenominator (): BigInteger
152148 {
153149 return $ this ->denominator ;
154150 }
@@ -158,7 +154,7 @@ public function getDenominator() : BigInteger
158154 *
159155 * @pure
160156 */
161- public function quotient () : BigInteger
157+ public function quotient (): BigInteger
162158 {
163159 return $ this ->numerator ->quotient ($ this ->denominator );
164160 }
@@ -168,7 +164,7 @@ public function quotient() : BigInteger
168164 *
169165 * @pure
170166 */
171- public function remainder () : BigInteger
167+ public function remainder (): BigInteger
172168 {
173169 return $ this ->numerator ->remainder ($ this ->denominator );
174170 }
@@ -180,7 +176,7 @@ public function remainder() : BigInteger
180176 *
181177 * @pure
182178 */
183- public function quotientAndRemainder () : array
179+ public function quotientAndRemainder (): array
184180 {
185181 return $ this ->numerator ->quotientAndRemainder ($ this ->denominator );
186182 }
@@ -194,12 +190,12 @@ public function quotientAndRemainder() : array
194190 *
195191 * @pure
196192 */
197- public function plus (BigNumber |int |float |string $ that ) : BigRational
193+ public function plus (BigNumber |int |float |string $ that ): BigRational
198194 {
199195 $ that = BigRational::of ($ that );
200196
201- $ numerator = $ this ->numerator ->multipliedBy ($ that ->denominator );
202- $ numerator = $ numerator ->plus ($ that ->numerator ->multipliedBy ($ this ->denominator ));
197+ $ numerator = $ this ->numerator ->multipliedBy ($ that ->denominator );
198+ $ numerator = $ numerator ->plus ($ that ->numerator ->multipliedBy ($ this ->denominator ));
203199 $ denominator = $ this ->denominator ->multipliedBy ($ that ->denominator );
204200
205201 return new BigRational ($ numerator , $ denominator , false );
@@ -214,12 +210,12 @@ public function plus(BigNumber|int|float|string $that) : BigRational
214210 *
215211 * @pure
216212 */
217- public function minus (BigNumber |int |float |string $ that ) : BigRational
213+ public function minus (BigNumber |int |float |string $ that ): BigRational
218214 {
219215 $ that = BigRational::of ($ that );
220216
221- $ numerator = $ this ->numerator ->multipliedBy ($ that ->denominator );
222- $ numerator = $ numerator ->minus ($ that ->numerator ->multipliedBy ($ this ->denominator ));
217+ $ numerator = $ this ->numerator ->multipliedBy ($ that ->denominator );
218+ $ numerator = $ numerator ->minus ($ that ->numerator ->multipliedBy ($ this ->denominator ));
223219 $ denominator = $ this ->denominator ->multipliedBy ($ that ->denominator );
224220
225221 return new BigRational ($ numerator , $ denominator , false );
@@ -234,11 +230,11 @@ public function minus(BigNumber|int|float|string $that) : BigRational
234230 *
235231 * @pure
236232 */
237- public function multipliedBy (BigNumber |int |float |string $ that ) : BigRational
233+ public function multipliedBy (BigNumber |int |float |string $ that ): BigRational
238234 {
239235 $ that = BigRational::of ($ that );
240236
241- $ numerator = $ this ->numerator ->multipliedBy ($ that ->numerator );
237+ $ numerator = $ this ->numerator ->multipliedBy ($ that ->numerator );
242238 $ denominator = $ this ->denominator ->multipliedBy ($ that ->denominator );
243239
244240 return new BigRational ($ numerator , $ denominator , false );
@@ -253,11 +249,11 @@ public function multipliedBy(BigNumber|int|float|string $that) : BigRational
253249 *
254250 * @pure
255251 */
256- public function dividedBy (BigNumber |int |float |string $ that ) : BigRational
252+ public function dividedBy (BigNumber |int |float |string $ that ): BigRational
257253 {
258254 $ that = BigRational::of ($ that );
259255
260- $ numerator = $ this ->numerator ->multipliedBy ($ that ->denominator );
256+ $ numerator = $ this ->numerator ->multipliedBy ($ that ->denominator );
261257 $ denominator = $ this ->denominator ->multipliedBy ($ that ->numerator );
262258
263259 return new BigRational ($ numerator , $ denominator , true );
@@ -266,11 +262,11 @@ public function dividedBy(BigNumber|int|float|string $that) : BigRational
266262 /**
267263 * Returns this number exponentiated to the given value.
268264 *
269- * @throws \ InvalidArgumentException If the exponent is not in the range 0 to 1,000,000.
265+ * @throws InvalidArgumentException If the exponent is not in the range 0 to 1,000,000.
270266 *
271267 * @pure
272268 */
273- public function power (int $ exponent ) : BigRational
269+ public function power (int $ exponent ): BigRational
274270 {
275271 if ($ exponent === 0 ) {
276272 $ one = BigInteger::one ();
@@ -285,7 +281,7 @@ public function power(int $exponent) : BigRational
285281 return new BigRational (
286282 $ this ->numerator ->power ($ exponent ),
287283 $ this ->denominator ->power ($ exponent ),
288- false
284+ false ,
289285 );
290286 }
291287
@@ -298,7 +294,7 @@ public function power(int $exponent) : BigRational
298294 *
299295 * @pure
300296 */
301- public function reciprocal () : BigRational
297+ public function reciprocal (): BigRational
302298 {
303299 return new BigRational ($ this ->denominator , $ this ->numerator , true );
304300 }
@@ -308,7 +304,7 @@ public function reciprocal() : BigRational
308304 *
309305 * @pure
310306 */
311- public function abs () : BigRational
307+ public function abs (): BigRational
312308 {
313309 return new BigRational ($ this ->numerator ->abs (), $ this ->denominator , false );
314310 }
@@ -318,7 +314,7 @@ public function abs() : BigRational
318314 *
319315 * @pure
320316 */
321- public function negated () : BigRational
317+ public function negated (): BigRational
322318 {
323319 return new BigRational ($ this ->numerator ->negated (), $ this ->denominator , false );
324320 }
@@ -328,7 +324,7 @@ public function negated() : BigRational
328324 *
329325 * @pure
330326 */
331- public function simplified () : BigRational
327+ public function simplified (): BigRational
332328 {
333329 $ gcd = $ this ->numerator ->gcd ($ this ->denominator );
334330
@@ -339,19 +335,19 @@ public function simplified() : BigRational
339335 }
340336
341337 #[Override]
342- public function compareTo (BigNumber |int |float |string $ that ) : int
338+ public function compareTo (BigNumber |int |float |string $ that ): int
343339 {
344340 return $ this ->minus ($ that )->getSign ();
345341 }
346342
347343 #[Override]
348- public function getSign () : int
344+ public function getSign (): int
349345 {
350346 return $ this ->numerator ->getSign ();
351347 }
352348
353349 #[Override]
354- public function toBigInteger () : BigInteger
350+ public function toBigInteger (): BigInteger
355351 {
356352 $ simplified = $ this ->simplified ();
357353
@@ -363,40 +359,41 @@ public function toBigInteger() : BigInteger
363359 }
364360
365361 #[Override]
366- public function toBigDecimal () : BigDecimal
362+ public function toBigDecimal (): BigDecimal
367363 {
368364 return $ this ->numerator ->toBigDecimal ()->exactlyDividedBy ($ this ->denominator );
369365 }
370366
371367 #[Override]
372- public function toBigRational () : BigRational
368+ public function toBigRational (): BigRational
373369 {
374370 return $ this ;
375371 }
376372
377373 #[Override]
378- public function toScale (int $ scale , RoundingMode $ roundingMode = RoundingMode::UNNECESSARY ) : BigDecimal
374+ public function toScale (int $ scale , RoundingMode $ roundingMode = RoundingMode::UNNECESSARY ): BigDecimal
379375 {
380376 return $ this ->numerator ->toBigDecimal ()->dividedBy ($ this ->denominator , $ scale , $ roundingMode );
381377 }
382378
383379 #[Override]
384- public function toInt () : int
380+ public function toInt (): int
385381 {
386382 return $ this ->toBigInteger ()->toInt ();
387383 }
388384
389385 #[Override]
390- public function toFloat () : float
386+ public function toFloat (): float
391387 {
392388 $ simplified = $ this ->simplified ();
389+
393390 return $ simplified ->numerator ->toFloat () / $ simplified ->denominator ->toFloat ();
394391 }
395392
396393 #[Override]
397- public function __toString () : string
394+ public function __toString (): string
398395 {
399- $ numerator = (string ) $ this ->numerator ;
396+ $ numerator = (string ) $ this ->numerator ;
400397 $ denominator = (string ) $ this ->denominator ;
401398
402399 if ($ denominator === '1 ' ) {
@@ -425,17 +422,23 @@ public function __serialize(): array
425422 *
426423 * @param array{numerator: BigInteger, denominator: BigInteger} $data
427424 *
428- * @throws \ LogicException
425+ * @throws LogicException
429426 */
430427 public function __unserialize (array $ data ): void
431428 {
432429 /** @phpstan-ignore isset.initializedProperty */
433430 if (isset ($ this ->numerator )) {
434- throw new \ LogicException ('__unserialize() is an internal function, it must not be called directly. ' );
431+ throw new LogicException ('__unserialize() is an internal function, it must not be called directly. ' );
435432 }
436433
437434 /** @phpstan-ignore deadCode.unreachable */
438435 $ this ->numerator = $ data ['numerator ' ];
439436 $ this ->denominator = $ data ['denominator ' ];
440437 }
438+
439+ #[Override]
440+ protected static function from (BigNumber $ number ): static
441+ {
442+ return $ number ->toBigRational ();
443+ }
441444}
0 commit comments