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

Commit d47cf15

Browse files
committed
Merge pull request #25 from punkka/cotan-add
Adding the method cotan for the calculus of the cotangente, by @punkka .
2 parents 495cc75 + 939fc62 commit d47cf15

File tree

5 files changed

+99
-1
lines changed

5 files changed

+99
-1
lines changed

src/Decimal.php

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

780+
/**
781+
* Calculates the cotangent of this method with the highest possible accuracy
782+
* Note that accuracy is limited by the accuracy of predefined PI;
783+
*
784+
* @param integer $scale
785+
* @return Decimal cotan($this)
786+
*/
787+
public function cotan($scale = null) {
788+
$sin = $this->sin($scale + 2);
789+
if ($sin->isZero()) {
790+
throw new \DomainException(
791+
"The cotangent of this 'angle' is undefined."
792+
);
793+
}
794+
795+
return $this->cos($scale + 2)->div($sin)->round($scale);
796+
}
797+
780798
/**
781799
* Indicates if the passed parameter has the same sign as the method's bound object.
782800
*

src/InfiniteDecimal.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,14 @@ public function tan($scale = null)
321321
);
322322
}
323323

324+
public function cotan($scale = null)
325+
{
326+
throw new \DomainException(($this === self::$pInf) ?
327+
"Cotangent function hasn't limit in the positive infinite." :
328+
"Cotangent function hasn't limit in the negative infinite."
329+
);
330+
}
331+
324332
/**
325333
* @param integer $scale Has no effect, exists only for compatibility.
326334
* @return boolean

tests/Decimal/DecimalCotanTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
/**
35+
* @expectedException \DomainException
36+
* @expectedExceptionMessage The cotangent of this 'angle' is undefined.
37+
*/
38+
public function testCotanPiDiv()
39+
{
40+
$PI = DecimalConstants::PI();
41+
$PI->cotan();
42+
}
43+
44+
}

tests/Decimal/DecimalTanTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use Litipk\BigNumbers\Decimal as Decimal;
3+
use \Litipk\BigNumbers\Decimal as Decimal;
44
use \Litipk\BigNumbers\DecimalConstants as DecimalConstants;
55

66
/**
@@ -39,4 +39,5 @@ public function testTanPiTwoDiv()
3939
$PiDividedByTwo = DecimalConstants::PI()->div(Decimal::fromInteger(2));
4040
$PiDividedByTwo->tan();
4141
}
42+
4243
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
use Litipk\BigNumbers\InfiniteDecimal as InfiniteDecimal;
4+
5+
/**
6+
* @group cotan
7+
*/
8+
class InfiniteDecimalCotanTest extends PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @expectedException \DomainException
12+
* @expectedExceptionMessage Cotangent function hasn't limit in the positive infinite.
13+
*/
14+
public function testFinitePositiveInfiniteCotan()
15+
{
16+
InfiniteDecimal::getPositiveInfinite()->cotan();
17+
}
18+
19+
/**
20+
* @expectedException \DomainException
21+
* @expectedExceptionMessage Cotangent function hasn't limit in the negative infinite.
22+
*/
23+
public function testFiniteNegativeInfiniteCotan()
24+
{
25+
InfiniteDecimal::getNegativeInfinite()->cotan();
26+
}
27+
}

0 commit comments

Comments
 (0)