Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit b9f9679

Browse files
committed
Merge pull request #48 from codisart/add-arccot-method
[TECH] Add the arccot method and a new unit test. Thanx to @codisart .
2 parents d0e894d + 1899a9c commit b9f9679

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/Decimal.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,38 @@ public function arctan($scale = null)
873873
return self::simplePowerSerie(
874874
$this,
875875
DecimalConstants::zero(),
876-
$scale
876+
$scale + 2
877+
)->round($scale);
878+
}
879+
880+
/**
881+
* Calculates the arccotangente of this with the highest possible accuracy
882+
*
883+
* @param integer $scale
884+
* @return Decimal
885+
*/
886+
public function arccot($scale = null) {
887+
$scale = ($scale === null) ? 32 : $scale;
888+
889+
$piOverTwo = DecimalConstants::pi()->div(Decimal::fromInteger(2), $scale + 2);
890+
if ($this->round($scale)->isZero()) {
891+
return $piOverTwo->round($scale);
892+
}
893+
894+
$piOverFour = DecimalConstants::pi()->div(Decimal::fromInteger(4), $scale + 2);
895+
if ($this->round($scale)->equals(DecimalConstants::one())) {
896+
return $piOverFour->round($scale);
897+
}
898+
if ($this->round($scale)->equals(DecimalConstants::negativeOne())) {
899+
return DecimalConstants::negativeOne()->mul($piOverFour, $scale + 2)->round($scale);
900+
}
901+
902+
return $piOverTwo->sub(
903+
self::simplePowerSerie(
904+
$this,
905+
DecimalConstants::zero(),
906+
$scale + 2
907+
)
877908
)->round($scale);
878909
}
879910

tests/Decimal/DecimalArccotTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Litipk\BigNumbers\Decimal as Decimal;
4+
5+
/**
6+
* @group arccot
7+
*/
8+
class DecimalArccotTest extends PHPUnit_Framework_TestCase
9+
{
10+
public function arccotProvider() {
11+
// Some values provided by wolframalpha
12+
return [
13+
['0.154', '1.41799671285823', 14],
14+
['0', '1.57079632679489662', 17],
15+
['-1', '-0.78540', 5],
16+
];
17+
}
18+
19+
/**
20+
* @dataProvider arccotProvider
21+
*/
22+
public function testSimple($nr, $answer, $digits)
23+
{
24+
$x = Decimal::fromString($nr);
25+
$arccotX = $x->arccot($digits);
26+
27+
$this->assertTrue(
28+
Decimal::fromString($answer)->equals($arccotX),
29+
"The answer must be " . $answer . ", but was " . $arccotX
30+
);
31+
}
32+
33+
}

0 commit comments

Comments
 (0)