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

Commit 5f81388

Browse files
committed
Adding the method cotan for the calculus of the cotangente
1 parent 094efa1 commit 5f81388

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/Decimal.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,24 @@ public function tan($scale = null) {
753753
return $this->sin($scale + 2)->div($cos)->round($scale);
754754
}
755755

756+
/**
757+
* Calculates the cotangent of this method with the highest possible accuracy
758+
* Note that accuracy is limited by the accuracy of predefined PI;
759+
*
760+
* @param integer $scale
761+
* @return Decimal cotan($this)
762+
*/
763+
public function cotan($scale = null) {
764+
$sin = $this->sin($scale + 2);
765+
if ($sin->isZero()) {
766+
throw new \DomainException(
767+
"The cotangent of this 'angle' is undefined."
768+
);
769+
}
770+
771+
return $this->cos($scale + 2)->div($sin)->round($scale);
772+
}
773+
756774
/**
757775
* Indicates if the passed parameter has the same sign as the method's bound object.
758776
*

tests/Decimal/DecimalCotanTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
use \Litipk\BigNumbers\Decimal as Decimal;
4+
use \Litipk\BigNumbers\DecimalConstants as DecimalConstants;
5+
6+
/**
7+
* @group cotan
8+
*/
9+
class DecimalCotanTest extends PHPUnit_Framework_TestCase
10+
{
11+
public function cotanProvider() {
12+
// Some values providede by mathematica
13+
return array(
14+
array('1', '0.64209261593433', 14),
15+
array('123.123', '1.45891895739232371', 17),
16+
array('15000000000', '-1.04405948230055701685', 20)
17+
18+
);
19+
}
20+
21+
/**
22+
* @dataProvider cotanProvider
23+
*/
24+
public function testSimple($nr, $answer, $digits)
25+
{
26+
$x = Decimal::fromString($nr);
27+
$cotanX = $x->cotan($digits);
28+
$this->assertTrue(
29+
Decimal::fromString($answer)->equals($cotanX),
30+
'cotan('.$nr.') must be equal to '.$answer.', but was '.$cotanX
31+
);
32+
}
33+
34+
public function testCotanPiDiv()
35+
{
36+
$PI = DecimalConstants::PI();
37+
38+
$catched = false;
39+
try {
40+
$PI->cotan();
41+
} catch (\DomainException $e) {
42+
$catched = true;
43+
}
44+
$this->assertTrue($catched);
45+
}
46+
47+
}

0 commit comments

Comments
 (0)