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

Commit 828b361

Browse files
committed
[TECH] Add the cosec method for the Decimal class and the test.
1 parent ccf88b1 commit 828b361

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-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;

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+
}

0 commit comments

Comments
 (0)