Debian Codegraph @

lib/3rdParty/composer/brick/math/src/BigRational.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Brick\Math;
6
7 use Brick\Math\Exception\DivisionByZeroException;
8 use Brick\Math\Exception\MathException;
9 use Brick\Math\Exception\NumberFormatException;
10 use Brick\Math\Exception\RoundingNecessaryException;
11 use Override;
12
13 /**
14 * An arbitrarily large rational number.
15 *
16 * This class is immutable.
17 *
18 * @psalm-immutable
19 */
20 final class BigRational extends BigNumber
21 {
22 /**
23 * The numerator.
24 */
25 private readonly BigInteger $numerator;
26
27 /**
28 * The denominator. Always strictly positive.
29 */
30 private readonly BigInteger $denominator;
31
32 /**
33 * Protected constructor. Use a factory method to obtain an instance.
34 *
35 * @param BigInteger $numerator The numerator.
36 * @param BigInteger $denominator The denominator.
37 * @param bool $checkDenominator Whether to check the denominator for negative and zero.
38 *
39 * @throws DivisionByZeroException If the denominator is zero.
40 */
41 protected function __construct(BigInteger $numerator, BigInteger $denominator, bool $checkDenominator)
42 {
43 if ($checkDenominator) {
44 if ($denominator->isZero()) {
45 throw DivisionByZeroException::denominatorMustNotBeZero();
46 }
47
48 if ($denominator->isNegative()) {
49 $numerator = $numerator->negated();
50 $denominator = $denominator->negated();
51 }
52 }
53
54 $this->numerator = $numerator;
55 $this->denominator = $denominator;
56 }
57
58 /**
59 * @psalm-pure
60 */
61 #[Override]
62 protected static function from(BigNumber $number): static
63 {
64 return $number->toBigRational();
65 }
66
67 /**
68 * Creates a BigRational out of a numerator and a denominator.
69 *
70 * If the denominator is negative, the signs of both the numerator and the denominator
71 * will be inverted to ensure that the denominator is always positive.
72 *
73 * @param BigNumber|int|float|string $numerator The numerator. Must be convertible to a BigInteger.
74 * @param BigNumber|int|float|string $denominator The denominator. Must be convertible to a BigInteger.
75 *
76 * @throws NumberFormatException If an argument does not represent a valid number.
77 * @throws RoundingNecessaryException If an argument represents a non-integer number.
78 * @throws DivisionByZeroException If the denominator is zero.
79 *
80 * @psalm-pure
81 */
82 public static function nd(
83 BigNumber|int|float|string $numerator,
84 BigNumber|int|float|string $denominator,
85 ) : BigRational {
86 $numerator = BigInteger::of($numerator);
87 $denominator = BigInteger::of($denominator);
88
89 return new BigRational($numerator, $denominator, true);
90 }
91
92 /**
93 * Returns a BigRational representing zero.
94 *
95 * @psalm-pure
96 */
97 public static function zero() : BigRational
98 {
99 /**
100 * @psalm-suppress ImpureStaticVariable
101 * @var BigRational|null $zero
102 */
103 static $zero;
104
105 if ($zero === null) {
106 $zero = new BigRational(BigInteger::zero(), BigInteger::one(), false);
107 }
108
109 return $zero;
110 }
111
112 /**
113 * Returns a BigRational representing one.
114 *
115 * @psalm-pure
116 */
117 public static function one() : BigRational
118 {
119 /**
120 * @psalm-suppress ImpureStaticVariable
121 * @var BigRational|null $one
122 */
123 static $one;
124
125 if ($one === null) {
126 $one = new BigRational(BigInteger::one(), BigInteger::one(), false);
127 }
128
129 return $one;
130 }
131
132 /**
133 * Returns a BigRational representing ten.
134 *
135 * @psalm-pure
136 */
137 public static function ten() : BigRational
138 {
139 /**
140 * @psalm-suppress ImpureStaticVariable
141 * @var BigRational|null $ten
142 */
143 static $ten;
144
145 if ($ten === null) {
146 $ten = new BigRational(BigInteger::ten(), BigInteger::one(), false);
147 }
148
149 return $ten;
150 }
151
152 public function getNumerator() : BigInteger
153 {
154 return $this->numerator;
155 }
156
157 public function getDenominator() : BigInteger
158 {
159 return $this->denominator;
160 }
161
162 /**
163 * Returns the quotient of the division of the numerator by the denominator.
164 */
165 public function quotient() : BigInteger
166 {
167 return $this->numerator->quotient($this->denominator);
168 }
169
170 /**
171 * Returns the remainder of the division of the numerator by the denominator.
172 */
173 public function remainder() : BigInteger
174 {
175 return $this->numerator->remainder($this->denominator);
176 }
177
178 /**
179 * Returns the quotient and remainder of the division of the numerator by the denominator.
180 *
181 * @return BigInteger[]
182 *
183 * @psalm-return array{BigInteger, BigInteger}
184 */
185 public function quotientAndRemainder() : array
186 {
187 return $this->numerator->quotientAndRemainder($this->denominator);
188 }
189
190 /**
191 * Returns the sum of this number and the given one.
192 *
193 * @param BigNumber|int|float|string $that The number to add.
194 *
195 * @throws MathException If the number is not valid.
196 */
197 public function plus(BigNumber|int|float|string $that) : BigRational
198 {
199 $that = BigRational::of($that);
200
201 $numerator = $this->numerator->multipliedBy($that->denominator);
202 $numerator = $numerator->plus($that->numerator->multipliedBy($this->denominator));
203 $denominator = $this->denominator->multipliedBy($that->denominator);
204
205 return new BigRational($numerator, $denominator, false);
206 }
207
208 /**
209 * Returns the difference of this number and the given one.
210 *
211 * @param BigNumber|int|float|string $that The number to subtract.
212 *
213 * @throws MathException If the number is not valid.
214 */
215 public function minus(BigNumber|int|float|string $that) : BigRational
216 {
217 $that = BigRational::of($that);
218
219 $numerator = $this->numerator->multipliedBy($that->denominator);
220 $numerator = $numerator->minus($that->numerator->multipliedBy($this->denominator));
221 $denominator = $this->denominator->multipliedBy($that->denominator);
222
223 return new BigRational($numerator, $denominator, false);
224 }
225
226 /**
227 * Returns the product of this number and the given one.
228 *
229 * @param BigNumber|int|float|string $that The multiplier.
230 *
231 * @throws MathException If the multiplier is not a valid number.
232 */
233 public function multipliedBy(BigNumber|int|float|string $that) : BigRational
234 {
235 $that = BigRational::of($that);
236
237 $numerator = $this->numerator->multipliedBy($that->numerator);
238 $denominator = $this->denominator->multipliedBy($that->denominator);
239
240 return new BigRational($numerator, $denominator, false);
241 }
242
243 /**
244 * Returns the result of the division of this number by the given one.
245 *
246 * @param BigNumber|int|float|string $that The divisor.
247 *
248 * @throws MathException If the divisor is not a valid number, or is zero.
249 */
250 public function dividedBy(BigNumber|int|float|string $that) : BigRational
251 {
252 $that = BigRational::of($that);
253
254 $numerator = $this->numerator->multipliedBy($that->denominator);
255 $denominator = $this->denominator->multipliedBy($that->numerator);
256
257 return new BigRational($numerator, $denominator, true);
258 }
259
260 /**
261 * Returns this number exponentiated to the given value.
262 *
263 * @throws \InvalidArgumentException If the exponent is not in the range 0 to 1,000,000.
264 */
265 public function power(int $exponent) : BigRational
266 {
267 if ($exponent === 0) {
268 $one = BigInteger::one();
269
270 return new BigRational($one, $one, false);
271 }
272
273 if ($exponent === 1) {
274 return $this;
275 }
276
277 return new BigRational(
278 $this->numerator->power($exponent),
279 $this->denominator->power($exponent),
280 false
281 );
282 }
283
284 /**
285 * Returns the reciprocal of this BigRational.
286 *
287 * The reciprocal has the numerator and denominator swapped.
288 *
289 * @throws DivisionByZeroException If the numerator is zero.
290 */
291 public function reciprocal() : BigRational
292 {
293 return new BigRational($this->denominator, $this->numerator, true);
294 }
295
296 /**
297 * Returns the absolute value of this BigRational.
298 */
299 public function abs() : BigRational
300 {
301 return new BigRational($this->numerator->abs(), $this->denominator, false);
302 }
303
304 /**
305 * Returns the negated value of this BigRational.
306 */
307 public function negated() : BigRational
308 {
309 return new BigRational($this->numerator->negated(), $this->denominator, false);
310 }
311
312 /**
313 * Returns the simplified value of this BigRational.
314 */
315 public function simplified() : BigRational
316 {
317 $gcd = $this->numerator->gcd($this->denominator);
318
319 $numerator = $this->numerator->quotient($gcd);
320 $denominator = $this->denominator->quotient($gcd);
321
322 return new BigRational($numerator, $denominator, false);
323 }
324
325 #[Override]
326 public function compareTo(BigNumber|int|float|string $that) : int
327 {
328 return $this->minus($that)->getSign();
329 }
330
331 #[Override]
332 public function getSign() : int
333 {
334 return $this->numerator->getSign();
335 }
336
337 #[Override]
338 public function toBigInteger() : BigInteger
339 {
340 $simplified = $this->simplified();
341
342 if (! $simplified->denominator->isEqualTo(1)) {
343 throw new RoundingNecessaryException('This rational number cannot be represented as an integer value without rounding.');
344 }
345
346 return $simplified->numerator;
347 }
348
349 #[Override]
350 public function toBigDecimal() : BigDecimal
351 {
352 return $this->numerator->toBigDecimal()->exactlyDividedBy($this->denominator);
353 }
354
355 #[Override]
356 public function toBigRational() : BigRational
357 {
358 return $this;
359 }
360
361 #[Override]
362 public function toScale(int $scale, RoundingMode $roundingMode = RoundingMode::UNNECESSARY) : BigDecimal
363 {
364 return $this->numerator->toBigDecimal()->dividedBy($this->denominator, $scale, $roundingMode);
365 }
366
367 #[Override]
368 public function toInt() : int
369 {
370 return $this->toBigInteger()->toInt();
371 }
372
373 #[Override]
374 public function toFloat() : float
375 {
376 $simplified = $this->simplified();
377 return $simplified->numerator->toFloat() / $simplified->denominator->toFloat();
378 }
379
380 #[Override]
381 public function __toString() : string
382 {
383 $numerator = (string) $this->numerator;
384 $denominator = (string) $this->denominator;
385
386 if ($denominator === '1') {
387 return $numerator;
388 }
389
390 return $numerator . '/' . $denominator;
391 }
392
393 /**
394 * This method is required for serializing the object and SHOULD NOT be accessed directly.
395 *
396 * @internal
397 *
398 * @return array{numerator: BigInteger, denominator: BigInteger}
399 */
400 public function __serialize(): array
401 {
402 return ['numerator' => $this->numerator, 'denominator' => $this->denominator];
403 }
404
405 /**
406 * This method is only here to allow unserializing the object and cannot be accessed directly.
407 *
408 * @internal
409 * @psalm-suppress RedundantPropertyInitializationCheck
410 *
411 * @param array{numerator: BigInteger, denominator: BigInteger} $data
412 *
413 * @throws \LogicException
414 */
415 public function __unserialize(array $data): void
416 {
417 if (isset($this->numerator)) {
418 throw new \LogicException('__unserialize() is an internal function, it must not be called directly.');
419 }
420
421 $this->numerator = $data['numerator'];
422 $this->denominator = $data['denominator'];
423 }
424 }