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

Commit b8c89c8

Browse files
committed
Merge pull request #41 from codisart/add-cosec-method
[TECH] Add the cosec method for the Decimal class and the test.
2 parents ccf88b1 + 872653e commit b8c89c8

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

src/Decimal.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,25 @@ function ($i) {
713713
);
714714
}
715715

716+
/**
717+
* Calculates the cosecant of this with the highest possible accuracy
718+
* Note that accuracy is limited by the accuracy of predefined PI;
719+
*
720+
* @param integer $scale
721+
* @return Decimal
722+
*/
723+
public function cosec($scale = null)
724+
{
725+
$sin = $this->sin($scale + 2);
726+
if ($sin->isZero()) {
727+
throw new \DomainException(
728+
"The cosecant of this 'angle' is undefined."
729+
);
730+
}
731+
732+
return Decimal::fromInteger(1)->div($sin)->round($scale);
733+
}
734+
716735
/**
717736
* Calculates the cosine of this method with the highest possible accuracy
718737
* Note that accuracy is limited by the accuracy of predefined PI;

src/InfiniteDecimal.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,20 @@ public function sin($scale = null)
311311
);
312312
}
313313

314+
/**
315+
* Throws exception because cosecant is undefined in the infinite.
316+
*
317+
* @param integer $scale
318+
* @return null
319+
*/
320+
public function cosec($scale = null)
321+
{
322+
throw new \DomainException(($this === self::$pInf) ?
323+
"Cosecant function hasn't limit in the positive infinite." :
324+
"Cosecant function hasn't limit in the negative infinite."
325+
);
326+
}
327+
314328
/**
315329
* Throws exception because cosine is undefined in the infinite.
316330
*

tests/Decimal/DecimalCosecTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Litipk\BigNumbers\Decimal as Decimal;
4+
5+
/**
6+
* @group cosec
7+
*/
8+
class DecimalCosecTest extends PHPUnit_Framework_TestCase
9+
{
10+
public function cosecProvider() {
11+
// Some values provided by Mathematica
12+
return [
13+
['1', '1.18839510577812', 14],
14+
['123.123', '-1.76874094322450309', 17],
15+
['15000000000', '1.44570405082842149818', 20]
16+
];
17+
}
18+
19+
/**
20+
* @dataProvider cosecProvider
21+
*/
22+
public function testSimple($nr, $answer, $digits)
23+
{
24+
$x = Decimal::fromString($nr);
25+
$cosecX = $x->cosec((int)$digits);
26+
27+
$this->assertTrue(
28+
Decimal::fromString($answer)->equals($cosecX),
29+
"The answer must be " . $answer . ", but was " . $cosecX
30+
);
31+
}
32+
}
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 cos
7+
*/
8+
class InfiniteDecimalCosecTest extends PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @expectedException \DomainException
12+
* @expectedExceptionMessage Cosecant function hasn't limit in the positive infinite.
13+
*/
14+
public function testFinitePositiveInfiniteCosec()
15+
{
16+
InfiniteDecimal::getPositiveInfinite()->cosec();
17+
}
18+
19+
/**
20+
* @expectedException \DomainException
21+
* @expectedExceptionMessage Cosecant function hasn't limit in the negative infinite.
22+
*/
23+
public function testFiniteNegativeInfiniteCosec()
24+
{
25+
InfiniteDecimal::getNegativeInfinite()->cosec();
26+
}
27+
}

0 commit comments

Comments
 (0)