/**
* Internal method used to compute arctan and arccotan
*
* @param Decimal $x
* @param Decimal $firstTerm
* @param $scale
* @return Decimal
*/
private static function simplePowerSerie(Decimal $x, Decimal $firstTerm, $scale)
{
$approx = $firstTerm;
$change = InfiniteDecimal::getPositiveInfinite();
$xPowerN = DecimalConstants::One();
// Calculates x^n
$sign = DecimalConstants::One();
// Calculates a_n
for ($i = 1; !$change->floor($scale + 2)->isZero(); $i++) {
$xPowerN = $xPowerN->mul($x);
if ($i % 2 === 0) {
$factorN = DecimalConstants::zero();
} else {
if ($i % 4 === 1) {
$factorN = DecimalConstants::one()->div(Decimal::fromInteger($i), $scale + 2);
} else {
$factorN = DecimalConstants::negativeOne()->div(Decimal::fromInteger($i), $scale + 2);
}
}
if (!$factorN->isZero()) {
$change = $factorN->mul($xPowerN, $scale + 2);
$approx = $approx->add($change, $scale + 2);
}
}
return $approx->round($scale);
}