| 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 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
final class BigRational extends BigNumber |
| 21 |
{ |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
private readonly BigInteger $numerator; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
private readonly BigInteger $denominator; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 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 |
|
| 60 |
|
| 61 |
#[Override] |
| 62 |
protected static function from(BigNumber $number): static |
| 63 |
{ |
| 64 |
return $number->toBigRational(); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 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 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public static function zero() : BigRational |
| 98 |
{ |
| 99 |
|
| 100 |
|
| 101 |
|
| 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 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public static function one() : BigRational |
| 118 |
{ |
| 119 |
|
| 120 |
|
| 121 |
|
| 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 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public static function ten() : BigRational |
| 138 |
{ |
| 139 |
|
| 140 |
|
| 141 |
|
| 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 |
|
| 164 |
|
| 165 |
public function quotient() : BigInteger |
| 166 |
{ |
| 167 |
return $this->numerator->quotient($this->denominator); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public function remainder() : BigInteger |
| 174 |
{ |
| 175 |
return $this->numerator->remainder($this->denominator); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public function quotientAndRemainder() : array |
| 186 |
{ |
| 187 |
return $this->numerator->quotientAndRemainder($this->denominator); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 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 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 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 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 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 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 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 |
|
| 262 |
|
| 263 |
|
| 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 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
public function reciprocal() : BigRational |
| 292 |
{ |
| 293 |
return new BigRational($this->denominator, $this->numerator, true); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
public function abs() : BigRational |
| 300 |
{ |
| 301 |
return new BigRational($this->numerator->abs(), $this->denominator, false); |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
public function negated() : BigRational |
| 308 |
{ |
| 309 |
return new BigRational($this->numerator->negated(), $this->denominator, false); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 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 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
public function __serialize(): array |
| 401 |
{ |
| 402 |
return ['numerator' => $this->numerator, 'denominator' => $this->denominator]; |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 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 |
} |