Skip to content

Commit

Permalink
Merge pull request #149 from JordanRL/f-code-quality
Browse files Browse the repository at this point in the history
Refactoring the remainder of Inverse Trigonometry
  • Loading branch information
JordanRL committed Oct 23, 2022
2 parents fe6efbe + 23cb23c commit 72b5bf4
Show file tree
Hide file tree
Showing 9 changed files with 427 additions and 381 deletions.
2 changes: 1 addition & 1 deletion .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ checks:
return-statements:
enabled: true
config:
threshold: 4
threshold: 8
similar-code:
enabled: true
identical-code:
Expand Down
6 changes: 3 additions & 3 deletions src/Samsara/Fermat/Core/Provider/SequenceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class SequenceProvider
* @throws IntegrityConstraint
* @throws MissingPackage
*/
public static function nthOddNumber(int $n, bool $asCollection = false, int $collectionSize = 10): ImmutableDecimal|NumberCollection
public static function nthOddNumber(int $n, int $scale = null, bool $asCollection = false, int $collectionSize = 10): ImmutableDecimal|NumberCollection
{
if ($asCollection) {
$sequence = new NumberCollection();
Expand All @@ -97,11 +97,11 @@ public static function nthOddNumber(int $n, bool $asCollection = false, int $col
}

if ($n >= (PHP_INT_MAX/2)) {
$n = new ImmutableDecimal($n, 100);
$n = new ImmutableDecimal($n, $scale);

return $n->multiply(2)->add(1);
} else {
return new ImmutableDecimal(($n*2)+1, 100);
return new ImmutableDecimal(($n*2)+1, $scale);
}

}
Expand Down
134 changes: 16 additions & 118 deletions src/Samsara/Fermat/Core/Types/Traits/InverseTrigonometrySimpleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace Samsara\Fermat\Core\Types\Traits;

use Samsara\Exceptions\UsageError\IntegrityConstraint;
use Samsara\Fermat\Core\Enums\CalcOperation;
use Samsara\Fermat\Core\Enums\NumberBase;
use Samsara\Fermat\Core\Numbers;
use Samsara\Fermat\Core\Types\Base\Interfaces\Numbers\DecimalInterface;
use Samsara\Fermat\Core\Types\Traits\Trigonometry\InverseTrigonometryNativeTrait;
use Samsara\Fermat\Core\Types\Traits\Trigonometry\InverseTrigonometryScaleTrait;
use Samsara\Fermat\Core\Types\Traits\Trigonometry\InverseTrigonometrySelectionTrait;
use Samsara\Fermat\Core\Values\ImmutableDecimal;

/**
* @package Samsara\Fermat\Core
Expand All @@ -29,26 +29,7 @@ trait InverseTrigonometrySimpleTrait
*/
public function arcsin(?int $scale = null, bool $round = true): DecimalInterface
{
$abs = $this instanceof ImmutableDecimal ? $this->abs() : new ImmutableDecimal($this->absValue());
if ($abs->isGreaterThan(1)) {
throw new IntegrityConstraint(
'The input for arcsin must have an absolute value of 1 or smaller',
'Only calculate arcsin for values of 1 or smaller',
'The arcsin function only has real values for inputs which have an absolute value of 1 or smaller'
);
}

$answer = $this->arcsinSelector($scale);

$finalScale = $scale ?? $this->getScale();

if ($round) {
$result = $this->setValue($answer)->roundToScale($finalScale);
} else {
$result = $this->setValue($answer)->truncateToScale($finalScale);
}

return $result;
return $this->helperArcsinArccosSimple(CalcOperation::ArcSin, $scale, $round);
}

/**
Expand All @@ -59,26 +40,7 @@ public function arcsin(?int $scale = null, bool $round = true): DecimalInterface
*/
public function arccos(?int $scale = null, bool $round = true): DecimalInterface
{
$abs = $this instanceof ImmutableDecimal ? $this->abs() : new ImmutableDecimal($this->absValue());
if ($abs->isGreaterThan(1)) {
throw new IntegrityConstraint(
'The input for arccos must have an absolute value of 1 or smaller',
'Only calculate arccos for values of 1 or smaller',
'The arccos function only has real values for inputs which have an absolute value of 1 or smaller'
);
}

$answer = $this->arccosSelector($scale);

$finalScale = $scale ?? $this->getScale();

if ($round) {
$result = $this->setValue($answer)->roundToScale($finalScale);
} else {
$result = $this->setValue($answer)->truncateToScale($finalScale);
}

return $result;
return $this->helperArcsinArccosSimple(CalcOperation::ArcCos, $scale, $round);
}

/**
Expand All @@ -88,53 +50,17 @@ public function arccos(?int $scale = null, bool $round = true): DecimalInterface
*/
public function arctan(?int $scale = null, bool $round = true): DecimalInterface
{
$answer = $this->arctanSelector($scale);

$finalScale = $scale ?? $this->getScale();

if ($round) {
$result = $this->setValue($answer)->roundToScale($finalScale);
} else {
$result = $this->setValue($answer)->truncateToScale($finalScale);
}

return $result;
return $this->helperArcBasicSimple(CalcOperation::ArcTan, $scale, $round);
}

/**
* @param int|null $scale
* @param bool $round
* @return DecimalInterface
* @throws IntegrityConstraint
*/
public function arcsec(?int $scale = null, bool $round = true): DecimalInterface
public function arccot(?int $scale = null, bool $round = true): DecimalInterface
{
$abs = $this instanceof ImmutableDecimal ? $this->abs() : new ImmutableDecimal($this->absValue());
if ($abs->isLessThan(1)) {
throw new IntegrityConstraint(
'The input for arcsec must have an absolute value greater than or equal to 1',
'Only calculate arcsec for values greater than 1',
'The arcsec function only has real values for inputs which have an absolute value greater than or equal to 1'
);
}

if ($this->isEqual(1)) {
$answer = '0';
} elseif ($this->isEqual(-1)) {
$answer = Numbers::makePi(($scale ?? $this->getScale())+1)->getValue(NumberBase::Ten);
} else {
$answer = $this->arcsecSelector($scale);
}

$finalScale = $scale ?? $this->getScale();

if ($round) {
$result = $this->setValue($answer)->roundToScale($finalScale);
} else {
$result = $this->setValue($answer)->truncateToScale($finalScale);
}

return $result;
return $this->helperArcBasicSimple(CalcOperation::ArcCot, $scale, $round);
}

/**
Expand All @@ -143,54 +69,26 @@ public function arcsec(?int $scale = null, bool $round = true): DecimalInterface
* @return DecimalInterface
* @throws IntegrityConstraint
*/
public function arccsc(?int $scale = null, bool $round = true): DecimalInterface
public function arcsec(?int $scale = null, bool $round = true): DecimalInterface
{
$abs = $this instanceof ImmutableDecimal ? $this->abs() : new ImmutableDecimal($this->absValue());
if ($abs->isLessThan(1)) {
throw new IntegrityConstraint(
'The input for arccsc must have an absolute value greater than or equal to 1',
'Only calculate arccsc for values greater than 1',
'The arccsc function only has real values for inputs which have an absolute value greater than or equal to 1'
);
}

if ($this->isEqual(1)) {
$answer = Numbers::makePi(($scale ?? $this->getScale())+2)->divide(2, ($scale ?? $this->getScale())+2)->getValue();
} elseif ($this->isEqual(-1)) {
$answer = Numbers::makePi(($scale ?? $this->getScale())+2)->divide(2, ($scale ?? $this->getScale())+2)->multiply(-1)->getValue();
} else {
$answer = $this->arccscSelector($scale);
}

$finalScale = $scale ?? $this->getScale();
$oneInputVal = '0';
$negOneInputVal = Numbers::makePi(($scale ?? $this->getScale())+1)->getValue(NumberBase::Ten);

if ($round) {
$result = $this->setValue($answer)->roundToScale($finalScale);
} else {
$result = $this->setValue($answer)->truncateToScale($finalScale);
}

return $result;
return $this->helperArcsecArccscSimple(CalcOperation::ArcSec, $scale, $oneInputVal, $negOneInputVal, $round);
}

/**
* @param int|null $scale
* @param bool $round
* @return DecimalInterface
* @throws IntegrityConstraint
*/
public function arccot(?int $scale = null, bool $round = true): DecimalInterface
public function arccsc(?int $scale = null, bool $round = true): DecimalInterface
{
$answer = $this->arccotSelector($scale);

$finalScale = $scale ?? $this->getScale();

if ($round) {
$result = $this->setValue($answer)->roundToScale($finalScale);
} else {
$result = $this->setValue($answer)->truncateToScale($finalScale);
}

return $result;
$oneInputVal = Numbers::makePi(($scale ?? $this->getScale())+2)->divide(2, ($scale ?? $this->getScale())+2)->getValue();
$negOneInputVal = Numbers::makePi(($scale ?? $this->getScale())+2)->divide(2, ($scale ?? $this->getScale())+2)->multiply(-1)->getValue();

return $this->helperArcsecArccscSimple(CalcOperation::ArcCsc, $scale, $oneInputVal, $negOneInputVal, $round);
}

}
Loading

0 comments on commit 72b5bf4

Please sign in to comment.