Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Ganze Datei anzeigen | Leerzeichen ignorieren | Details | Blame | Letzte Änderung | Log anzeigen | RSS feed

Revision 148 Revision 399
Zeile 55... Zeile 55...
55
    }
55
    }
Zeile 56... Zeile 56...
56
 
56
 
57
    /**
57
    /**
58
     * Creates a BigRational of the given value.
58
     * Creates a BigRational of the given value.
59
     *
-
 
60
     * @param BigNumber|int|float|string $value
-
 
61
     *
-
 
62
     * @return BigRational
-
 
63
     *
59
     *
64
     * @throws MathException If the value cannot be converted to a BigRational.
60
     * @throws MathException If the value cannot be converted to a BigRational.
65
     *
61
     *
66
     * @psalm-pure
62
     * @psalm-pure
67
     */
63
     */
68
    public static function of($value) : BigNumber
64
    public static function of(BigNumber|int|float|string $value) : BigRational
69
    {
65
    {
70
        return parent::of($value)->toBigRational();
66
        return parent::of($value)->toBigRational();
Zeile 71... Zeile 67...
71
    }
67
    }
Zeile 77... Zeile 73...
77
     * will be inverted to ensure that the denominator is always positive.
73
     * will be inverted to ensure that the denominator is always positive.
78
     *
74
     *
79
     * @param BigNumber|int|float|string $numerator   The numerator. Must be convertible to a BigInteger.
75
     * @param BigNumber|int|float|string $numerator   The numerator. Must be convertible to a BigInteger.
80
     * @param BigNumber|int|float|string $denominator The denominator. Must be convertible to a BigInteger.
76
     * @param BigNumber|int|float|string $denominator The denominator. Must be convertible to a BigInteger.
81
     *
77
     *
82
     * @return BigRational
-
 
83
     *
-
 
84
     * @throws NumberFormatException      If an argument does not represent a valid number.
78
     * @throws NumberFormatException      If an argument does not represent a valid number.
85
     * @throws RoundingNecessaryException If an argument represents a non-integer number.
79
     * @throws RoundingNecessaryException If an argument represents a non-integer number.
86
     * @throws DivisionByZeroException    If the denominator is zero.
80
     * @throws DivisionByZeroException    If the denominator is zero.
87
     *
81
     *
88
     * @psalm-pure
82
     * @psalm-pure
89
     */
83
     */
90
    public static function nd($numerator, $denominator) : BigRational
84
    public static function nd(
-
 
85
        BigNumber|int|float|string $numerator,
-
 
86
        BigNumber|int|float|string $denominator,
91
    {
87
    ) : BigRational {
92
        $numerator   = BigInteger::of($numerator);
88
        $numerator   = BigInteger::of($numerator);
93
        $denominator = BigInteger::of($denominator);
89
        $denominator = BigInteger::of($denominator);
Zeile 94... Zeile 90...
94
 
90
 
95
        return new BigRational($numerator, $denominator, true);
91
        return new BigRational($numerator, $denominator, true);
Zeile 96... Zeile 92...
96
    }
92
    }
97
 
93
 
98
    /**
94
    /**
99
     * Returns a BigRational representing zero.
-
 
100
     *
-
 
101
     * @return BigRational
95
     * Returns a BigRational representing zero.
102
     *
96
     *
103
     * @psalm-pure
97
     * @psalm-pure
104
     */
98
     */
105
    public static function zero() : BigRational
99
    public static function zero() : BigRational
Zeile 118... Zeile 112...
118
    }
112
    }
Zeile 119... Zeile 113...
119
 
113
 
120
    /**
114
    /**
121
     * Returns a BigRational representing one.
115
     * Returns a BigRational representing one.
122
     *
-
 
123
     * @return BigRational
-
 
124
     *
116
     *
125
     * @psalm-pure
117
     * @psalm-pure
126
     */
118
     */
127
    public static function one() : BigRational
119
    public static function one() : BigRational
128
    {
120
    {
Zeile 140... Zeile 132...
140
    }
132
    }
Zeile 141... Zeile 133...
141
 
133
 
142
    /**
134
    /**
143
     * Returns a BigRational representing ten.
135
     * Returns a BigRational representing ten.
144
     *
-
 
145
     * @return BigRational
-
 
146
     *
136
     *
147
     * @psalm-pure
137
     * @psalm-pure
148
     */
138
     */
149
    public static function ten() : BigRational
139
    public static function ten() : BigRational
150
    {
140
    {
Zeile 159... Zeile 149...
159
        }
149
        }
Zeile 160... Zeile 150...
160
 
150
 
161
        return $ten;
151
        return $ten;
Zeile 162... Zeile -...
162
    }
-
 
163
 
-
 
164
    /**
-
 
165
     * @return BigInteger
152
    }
166
     */
153
 
167
    public function getNumerator() : BigInteger
154
    public function getNumerator() : BigInteger
168
    {
155
    {
Zeile 169... Zeile -...
169
        return $this->numerator;
-
 
170
    }
-
 
171
 
-
 
172
    /**
156
        return $this->numerator;
173
     * @return BigInteger
157
    }
174
     */
158
 
175
    public function getDenominator() : BigInteger
159
    public function getDenominator() : BigInteger
Zeile 176... Zeile 160...
176
    {
160
    {
177
        return $this->denominator;
161
        return $this->denominator;
178
    }
-
 
179
 
-
 
180
    /**
162
    }
181
     * Returns the quotient of the division of the numerator by the denominator.
163
 
182
     *
164
    /**
183
     * @return BigInteger
165
     * Returns the quotient of the division of the numerator by the denominator.
184
     */
166
     */
Zeile 185... Zeile 167...
185
    public function quotient() : BigInteger
167
    public function quotient() : BigInteger
186
    {
168
    {
187
        return $this->numerator->quotient($this->denominator);
-
 
188
    }
-
 
189
 
169
        return $this->numerator->quotient($this->denominator);
190
    /**
170
    }
191
     * Returns the remainder of the division of the numerator by the denominator.
171
 
192
     *
172
    /**
193
     * @return BigInteger
173
     * Returns the remainder of the division of the numerator by the denominator.
Zeile 210... Zeile 190...
210
    /**
190
    /**
211
     * Returns the sum of this number and the given one.
191
     * Returns the sum of this number and the given one.
212
     *
192
     *
213
     * @param BigNumber|int|float|string $that The number to add.
193
     * @param BigNumber|int|float|string $that The number to add.
214
     *
194
     *
215
     * @return BigRational The result.
-
 
216
     *
-
 
217
     * @throws MathException If the number is not valid.
195
     * @throws MathException If the number is not valid.
218
     */
196
     */
219
    public function plus($that) : BigRational
197
    public function plus(BigNumber|int|float|string $that) : BigRational
220
    {
198
    {
221
        $that = BigRational::of($that);
199
        $that = BigRational::of($that);
Zeile 222... Zeile 200...
222
 
200
 
223
        $numerator   = $this->numerator->multipliedBy($that->denominator);
201
        $numerator   = $this->numerator->multipliedBy($that->denominator);
Zeile 230... Zeile 208...
230
    /**
208
    /**
231
     * Returns the difference of this number and the given one.
209
     * Returns the difference of this number and the given one.
232
     *
210
     *
233
     * @param BigNumber|int|float|string $that The number to subtract.
211
     * @param BigNumber|int|float|string $that The number to subtract.
234
     *
212
     *
235
     * @return BigRational The result.
-
 
236
     *
-
 
237
     * @throws MathException If the number is not valid.
213
     * @throws MathException If the number is not valid.
238
     */
214
     */
239
    public function minus($that) : BigRational
215
    public function minus(BigNumber|int|float|string $that) : BigRational
240
    {
216
    {
241
        $that = BigRational::of($that);
217
        $that = BigRational::of($that);
Zeile 242... Zeile 218...
242
 
218
 
243
        $numerator   = $this->numerator->multipliedBy($that->denominator);
219
        $numerator   = $this->numerator->multipliedBy($that->denominator);
Zeile 250... Zeile 226...
250
    /**
226
    /**
251
     * Returns the product of this number and the given one.
227
     * Returns the product of this number and the given one.
252
     *
228
     *
253
     * @param BigNumber|int|float|string $that The multiplier.
229
     * @param BigNumber|int|float|string $that The multiplier.
254
     *
230
     *
255
     * @return BigRational The result.
-
 
256
     *
-
 
257
     * @throws MathException If the multiplier is not a valid number.
231
     * @throws MathException If the multiplier is not a valid number.
258
     */
232
     */
259
    public function multipliedBy($that) : BigRational
233
    public function multipliedBy(BigNumber|int|float|string $that) : BigRational
260
    {
234
    {
261
        $that = BigRational::of($that);
235
        $that = BigRational::of($that);
Zeile 262... Zeile 236...
262
 
236
 
263
        $numerator   = $this->numerator->multipliedBy($that->numerator);
237
        $numerator   = $this->numerator->multipliedBy($that->numerator);
Zeile 269... Zeile 243...
269
    /**
243
    /**
270
     * Returns the result of the division of this number by the given one.
244
     * Returns the result of the division of this number by the given one.
271
     *
245
     *
272
     * @param BigNumber|int|float|string $that The divisor.
246
     * @param BigNumber|int|float|string $that The divisor.
273
     *
247
     *
274
     * @return BigRational The result.
-
 
275
     *
-
 
276
     * @throws MathException If the divisor is not a valid number, or is zero.
248
     * @throws MathException If the divisor is not a valid number, or is zero.
277
     */
249
     */
278
    public function dividedBy($that) : BigRational
250
    public function dividedBy(BigNumber|int|float|string $that) : BigRational
279
    {
251
    {
280
        $that = BigRational::of($that);
252
        $that = BigRational::of($that);
Zeile 281... Zeile 253...
281
 
253
 
282
        $numerator   = $this->numerator->multipliedBy($that->denominator);
254
        $numerator   = $this->numerator->multipliedBy($that->denominator);
Zeile 286... Zeile 258...
286
    }
258
    }
Zeile 287... Zeile 259...
287
 
259
 
288
    /**
260
    /**
289
     * Returns this number exponentiated to the given value.
261
     * Returns this number exponentiated to the given value.
290
     *
-
 
291
     * @param int $exponent The exponent.
-
 
292
     *
-
 
293
     * @return BigRational The result.
-
 
294
     *
262
     *
295
     * @throws \InvalidArgumentException If the exponent is not in the range 0 to 1,000,000.
263
     * @throws \InvalidArgumentException If the exponent is not in the range 0 to 1,000,000.
296
     */
264
     */
297
    public function power(int $exponent) : BigRational
265
    public function power(int $exponent) : BigRational
298
    {
266
    {
Zeile 316... Zeile 284...
316
    /**
284
    /**
317
     * Returns the reciprocal of this BigRational.
285
     * Returns the reciprocal of this BigRational.
318
     *
286
     *
319
     * The reciprocal has the numerator and denominator swapped.
287
     * The reciprocal has the numerator and denominator swapped.
320
     *
288
     *
321
     * @return BigRational
-
 
322
     *
-
 
323
     * @throws DivisionByZeroException If the numerator is zero.
289
     * @throws DivisionByZeroException If the numerator is zero.
324
     */
290
     */
325
    public function reciprocal() : BigRational
291
    public function reciprocal() : BigRational
326
    {
292
    {
327
        return new BigRational($this->denominator, $this->numerator, true);
293
        return new BigRational($this->denominator, $this->numerator, true);
328
    }
294
    }
Zeile 329... Zeile 295...
329
 
295
 
330
    /**
296
    /**
331
     * Returns the absolute value of this BigRational.
-
 
332
     *
-
 
333
     * @return BigRational
297
     * Returns the absolute value of this BigRational.
334
     */
298
     */
335
    public function abs() : BigRational
299
    public function abs() : BigRational
336
    {
300
    {
337
        return new BigRational($this->numerator->abs(), $this->denominator, false);
301
        return new BigRational($this->numerator->abs(), $this->denominator, false);
Zeile 338... Zeile 302...
338
    }
302
    }
339
 
303
 
340
    /**
-
 
341
     * Returns the negated value of this BigRational.
-
 
342
     *
304
    /**
343
     * @return BigRational
305
     * Returns the negated value of this BigRational.
344
     */
306
     */
345
    public function negated() : BigRational
307
    public function negated() : BigRational
346
    {
308
    {
Zeile 347... Zeile 309...
347
        return new BigRational($this->numerator->negated(), $this->denominator, false);
309
        return new BigRational($this->numerator->negated(), $this->denominator, false);
348
    }
310
    }
349
 
-
 
350
    /**
-
 
351
     * Returns the simplified value of this BigRational.
311
 
352
     *
312
    /**
353
     * @return BigRational
313
     * Returns the simplified value of this BigRational.
354
     */
314
     */
Zeile 360... Zeile 320...
360
        $denominator = $this->denominator->quotient($gcd);
320
        $denominator = $this->denominator->quotient($gcd);
Zeile 361... Zeile 321...
361
 
321
 
362
        return new BigRational($numerator, $denominator, false);
322
        return new BigRational($numerator, $denominator, false);
Zeile 363... Zeile -...
363
    }
-
 
364
 
-
 
365
    /**
-
 
366
     * {@inheritdoc}
323
    }
367
     */
324
 
368
    public function compareTo($that) : int
325
    public function compareTo(BigNumber|int|float|string $that) : int
369
    {
326
    {
Zeile 370... Zeile -...
370
        return $this->minus($that)->getSign();
-
 
371
    }
-
 
372
 
-
 
373
    /**
327
        return $this->minus($that)->getSign();
374
     * {@inheritdoc}
328
    }
375
     */
329
 
376
    public function getSign() : int
330
    public function getSign() : int
Zeile 377... Zeile -...
377
    {
-
 
378
        return $this->numerator->getSign();
-
 
379
    }
-
 
380
 
331
    {
381
    /**
332
        return $this->numerator->getSign();
382
     * {@inheritdoc}
333
    }
Zeile 383... Zeile 334...
383
     */
334
 
Zeile 390... Zeile 341...
390
        }
341
        }
Zeile 391... Zeile 342...
391
 
342
 
392
        return $simplified->numerator;
343
        return $simplified->numerator;
Zeile 393... Zeile -...
393
    }
-
 
394
 
-
 
395
    /**
-
 
396
     * {@inheritdoc}
344
    }
397
     */
345
 
398
    public function toBigDecimal() : BigDecimal
346
    public function toBigDecimal() : BigDecimal
399
    {
347
    {
Zeile 400... Zeile -...
400
        return $this->numerator->toBigDecimal()->exactlyDividedBy($this->denominator);
-
 
401
    }
-
 
402
 
-
 
403
    /**
348
        return $this->numerator->toBigDecimal()->exactlyDividedBy($this->denominator);
404
     * {@inheritdoc}
349
    }
405
     */
350
 
406
    public function toBigRational() : BigRational
351
    public function toBigRational() : BigRational
Zeile 407... Zeile -...
407
    {
-
 
408
        return $this;
-
 
409
    }
-
 
410
 
352
    {
411
    /**
353
        return $this;
412
     * {@inheritdoc}
354
    }
413
     */
355
 
Zeile 414... Zeile -...
414
    public function toScale(int $scale, int $roundingMode = RoundingMode::UNNECESSARY) : BigDecimal
-
 
415
    {
-
 
416
        return $this->numerator->toBigDecimal()->dividedBy($this->denominator, $scale, $roundingMode);
-
 
417
    }
356
    public function toScale(int $scale, int $roundingMode = RoundingMode::UNNECESSARY) : BigDecimal
418
 
357
    {
419
    /**
358
        return $this->numerator->toBigDecimal()->dividedBy($this->denominator, $scale, $roundingMode);
420
     * {@inheritdoc}
359
    }
Zeile 421... Zeile -...
421
     */
-
 
422
    public function toInt() : int
-
 
423
    {
-
 
424
        return $this->toBigInteger()->toInt();
360
 
425
    }
361
    public function toInt() : int
426
 
362
    {
427
    /**
363
        return $this->toBigInteger()->toInt();
428
     * {@inheritdoc}
364
    }
Zeile 429... Zeile -...
429
     */
-
 
430
    public function toFloat() : float
-
 
431
    {
-
 
432
        $simplified = $this->simplified();
365
 
433
        return $simplified->numerator->toFloat() / $simplified->denominator->toFloat();
366
    public function toFloat() : float
434
    }
367
    {
435
 
368
        $simplified = $this->simplified();
Zeile 466... Zeile 399...
466
     * @internal
399
     * @internal
467
     * @psalm-suppress RedundantPropertyInitializationCheck
400
     * @psalm-suppress RedundantPropertyInitializationCheck
468
     *
401
     *
469
     * @param array{numerator: BigInteger, denominator: BigInteger} $data
402
     * @param array{numerator: BigInteger, denominator: BigInteger} $data
470
     *
403
     *
471
     * @return void
-
 
472
     *
-
 
473
     * @throws \LogicException
404
     * @throws \LogicException
474
     */
405
     */
475
    public function __unserialize(array $data): void
406
    public function __unserialize(array $data): void
476
    {
407
    {
477
        if (isset($this->numerator)) {
408
        if (isset($this->numerator)) {
Zeile 484... Zeile 415...
484
 
415
 
485
    /**
416
    /**
486
     * This method is required by interface Serializable and SHOULD NOT be accessed directly.
417
     * This method is required by interface Serializable and SHOULD NOT be accessed directly.
487
     *
418
     *
488
     * @internal
-
 
489
     *
-
 
490
     * @return string
419
     * @internal
491
     */
420
     */
492
    public function serialize() : string
421
    public function serialize() : string
493
    {
422
    {
494
        return $this->numerator . '/' . $this->denominator;
423
        return $this->numerator . '/' . $this->denominator;
Zeile 498... Zeile 427...
498
     * This method is only here to implement interface Serializable and cannot be accessed directly.
427
     * This method is only here to implement interface Serializable and cannot be accessed directly.
499
     *
428
     *
500
     * @internal
429
     * @internal
501
     * @psalm-suppress RedundantPropertyInitializationCheck
430
     * @psalm-suppress RedundantPropertyInitializationCheck
502
     *
431
     *
503
     * @param string $value
-
 
504
     *
-
 
505
     * @return void
-
 
506
     *
-
 
507
     * @throws \LogicException
432
     * @throws \LogicException
508
     */
433
     */
509
    public function unserialize($value) : void
434
    public function unserialize($value) : void
510
    {
435
    {
511
        if (isset($this->numerator)) {
436
        if (isset($this->numerator)) {