Skip to content

Commit

Permalink
Result of some operation can be imcompatible with SmartNumber. Now al…
Browse files Browse the repository at this point in the history
…l internal logic return SmartNumber instance as result.
  • Loading branch information
janbarasek committed Oct 10, 2020
1 parent bc6d5bd commit 114f465
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
16 changes: 8 additions & 8 deletions src/Operation/AddNumbers.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ public function process(NumberToken $left, NumberToken $right, Query $query): Nu
$leftFraction = $leftNumber->toBigRational();
$rightFraction = $rightNumber->toBigRational();

$result = BigRational::nd(
$rightFraction->getDenominator()->multipliedBy($leftFraction->getNumerator())
->plus(
$leftFraction->getDenominator()->multipliedBy($rightFraction->getNumerator())
),
$leftFraction->getDenominator()->multipliedBy($rightFraction->getDenominator())
)->simplified();
$result = SmartNumber::of(
BigRational::nd(
$rightFraction->getDenominator()->multipliedBy($leftFraction->getNumerator())
->plus($leftFraction->getDenominator()->multipliedBy($rightFraction->getNumerator())),
$leftFraction->getDenominator()->multipliedBy($rightFraction->getDenominator())
)->simplified()
);
}

$newNumber = new NumberToken(SmartNumber::of($result->getNumber()));
$newNumber = new NumberToken($result);
$newNumber
->setToken((string) $newNumber->getNumber())
->setPosition($left->getPosition())
Expand Down
10 changes: 6 additions & 4 deletions src/Operation/MultiplicationNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ public function process(NumberToken $left, NumberToken $right, Query $query): Nu
$leftFraction = $left->getNumber()->toBigRational();
$rightFraction = $right->getNumber()->toBigRational();

$result = BigRational::nd(
$leftFraction->getNumerator()->multipliedBy($rightFraction->getNumerator()),
$leftFraction->getDenominator()->multipliedBy($rightFraction->getDenominator())
$result = SmartNumber::of(
BigRational::nd(
$leftFraction->getNumerator()->multipliedBy($rightFraction->getNumerator()),
$leftFraction->getDenominator()->multipliedBy($rightFraction->getDenominator())
)
);
}

$newNumber = new NumberToken(SmartNumber::of($result->getNumber()));
$newNumber = new NumberToken($result);
$newNumber->setToken((string) $newNumber->getNumber())
->setPosition($left->getPosition())
->setType('number');
Expand Down
37 changes: 19 additions & 18 deletions src/Operation/PowNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,43 +30,44 @@ public function process(NumberToken $left, NumberToken $right, Query $query): Nu
$leftFraction = $leftNumber->toBigRational();
$rightFraction = $rightNumber->toBigRational();

$result = null;
$rightIsInteger = $rightNumber->isInteger();

if ($rightIsInteger && $leftNumber->isInteger()) {
if (($rightIsInteger = $rightNumber->isInteger()) === true && $leftNumber->isInteger()) {
if ($leftNumber->isEqualTo(0) && $rightNumber->isEqualTo(0)) {
throw new UndefinedOperationException(__METHOD__ . ': Undefined operation.');
}

$result = Calculation::of($left->getNumber())
->power($right->getNumber()->toInt())
->getResult();
} elseif ($rightIsInteger) {
$result = BigRational::nd(
$leftFraction->getNumerator()->power($right->getNumber()->toInt()),
$leftFraction->getDenominator()->power($right->getNumber()->toInt())
} elseif ($rightIsInteger === true) {
$result = SmartNumber::of(
BigRational::nd(
$leftFraction->getNumerator()->power($right->getNumber()->toInt()),
$leftFraction->getDenominator()->power($right->getNumber()->toInt())
)
);
} else {
if ($rightNumber->isNegative()) {
if ($rightNumber->isNegative() === true) {
$rightFraction = BigRational::nd(
$rightFraction->getDenominator(),
$rightFraction->getNumerator()
);
}

$result = BigRational::nd(
pow(
$leftFraction->getNumerator()->power($rightFraction->getNumerator()->toInt())->toInt(),
BigDecimal::one()->dividedBy($rightFraction->getDenominator(), $query->getDecimals(), RoundingMode::HALF_UP)->toFloat()
),
pow(
$leftFraction->getDenominator()->power($rightFraction->getNumerator()->toInt())->toInt(),
BigDecimal::one()->dividedBy($rightFraction->getDenominator(), $query->getDecimals(), RoundingMode::HALF_UP)->toFloat()
$result = SmartNumber::of(
BigRational::nd(
pow(
$leftFraction->getNumerator()->power($rightFraction->getNumerator()->toInt())->toInt(),
BigDecimal::one()->dividedBy($rightFraction->getDenominator(), $query->getDecimals(), RoundingMode::HALF_UP)->toFloat()
),
pow(
$leftFraction->getDenominator()->power($rightFraction->getNumerator()->toInt())->toInt(),
BigDecimal::one()->dividedBy($rightFraction->getDenominator(), $query->getDecimals(), RoundingMode::HALF_UP)->toFloat()
)
)
);
}

$newNumber = new NumberToken(SmartNumber::of($result->getNumber()));
$newNumber = new NumberToken($result);
$newNumber->setToken((string) $newNumber->getNumber())
->setPosition($left->getPosition())
->setType('number');
Expand Down

0 comments on commit 114f465

Please sign in to comment.