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

Commit ef221ca

Browse files
authored
Merge pull request #49 from codisart/add-arcsec-arccsc-methods
[TECH] Add two new methods and their tests. Thanks to @codisart .
2 parents 7a222da + 56aeab3 commit ef221ca

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

src/Decimal.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,68 @@ public function arccot($scale = null) {
908908
)->round($scale);
909909
}
910910

911+
/**
912+
* Calculates the arcsecant of this with the highest possible accuracy
913+
*
914+
* @param integer $scale
915+
* @return Decimal
916+
*/
917+
public function arcsec($scale = null) {
918+
if($this->comp(DecimalConstants::one(), $scale + 2) === -1 && $this->comp(DecimalConstants::negativeOne(), $scale + 2) === 1) {
919+
throw new \DomainException(
920+
"The arcsecant of this number is undefined."
921+
);
922+
}
923+
924+
$piOverTwo = DecimalConstants::pi()->div(Decimal::fromInteger(2), $scale + 2)->round($scale);
925+
926+
if ($this->round($scale)->equals(DecimalConstants::one())) {
927+
return DecimalConstants::zero();
928+
}
929+
if ($this->round($scale)->equals(DecimalConstants::negativeOne())) {
930+
return DecimalConstants::pi()->round($scale);
931+
}
932+
933+
$scale = ($scale === null) ? 32 : $scale;
934+
935+
return $piOverTwo->sub(
936+
self::powerSerie(
937+
DecimalConstants::one()->div($this, $scale + 2),
938+
DecimalConstants::zero(),
939+
$scale + 2
940+
)
941+
)->round($scale);
942+
}
943+
944+
/**
945+
* Calculates the arccosecant of this with the highest possible accuracy
946+
*
947+
* @param integer $scale
948+
* @return Decimal
949+
*/
950+
public function arccsc($scale = null) {
951+
if($this->comp(DecimalConstants::one(), $scale + 2) === -1 && $this->comp(DecimalConstants::negativeOne(), $scale + 2) === 1) {
952+
throw new \DomainException(
953+
"The arccosecant of this number is undefined."
954+
);
955+
}
956+
957+
$scale = ($scale === null) ? 32 : $scale;
958+
959+
if ($this->round($scale)->equals(DecimalConstants::one())) {
960+
return DecimalConstants::pi()->div(Decimal::fromInteger(2), $scale + 2)->round($scale);
961+
}
962+
if ($this->round($scale)->equals(DecimalConstants::negativeOne())) {
963+
return DecimalConstants::pi()->div(Decimal::fromInteger(-2), $scale + 2)->round($scale);
964+
}
965+
966+
return self::powerSerie(
967+
DecimalConstants::one()->div($this, $scale + 2),
968+
DecimalConstants::zero(),
969+
$scale + 2
970+
)->round($scale);
971+
}
972+
911973
/**
912974
* Returns exp($this), said in other words: e^$this .
913975
*

tests/Decimal/DecimalArccscTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
use Litipk\BigNumbers\Decimal as Decimal;
4+
5+
/**
6+
* @group arccsc
7+
*/
8+
class DecimalArccscTest extends PHPUnit_Framework_TestCase
9+
{
10+
public function arccscProvider() {
11+
// Some values provided by wolframalpha
12+
return [
13+
['25.546', '0.03915507577327', 14],
14+
['1.5', '0.729728', 6],
15+
['1', '1.57079632679489662', 17],
16+
['-1', '-1.57079632679489662', 17],
17+
];
18+
}
19+
20+
/**
21+
* @dataProvider arccscProvider
22+
*/
23+
public function testSimple($nr, $answer, $digits)
24+
{
25+
$x = Decimal::fromString($nr);
26+
$arccscX = $x->arccsc($digits);
27+
28+
$this->assertTrue(
29+
Decimal::fromString($answer)->equals($arccscX),
30+
"The answer must be " . $answer . ", but was " . $arccscX
31+
);
32+
}
33+
34+
/**
35+
* @expectedException \DomainException
36+
* @expectedExceptionMessage The arccosecant of this number is undefined.
37+
*/
38+
public function testArccscBetweenOneAndNegativeOne()
39+
{
40+
Decimal::fromString('0.546')->arccsc();
41+
}
42+
}

tests/Decimal/DecimalArcsecTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
use Litipk\BigNumbers\Decimal as Decimal;
4+
5+
/**
6+
* @group arcsec
7+
*/
8+
class DecimalArcsecTest extends PHPUnit_Framework_TestCase
9+
{
10+
public function arcsecProvider() {
11+
// Some values provided by wolframalpha
12+
return [
13+
['25.546', '1.53164125102163', 14],
14+
['1.5', '0.841068', 6],
15+
['1', '0', 17],
16+
['-1', '3.14159265358979324', 17],
17+
];
18+
}
19+
20+
/**
21+
* @dataProvider arcsecProvider
22+
*/
23+
public function testSimple($nr, $answer, $digits)
24+
{
25+
$x = Decimal::fromString($nr);
26+
$arcsecX = $x->arcsec($digits);
27+
28+
$this->assertTrue(
29+
Decimal::fromString($answer)->equals($arcsecX),
30+
"The answer must be " . $answer . ", but was " . $arcsecX
31+
);
32+
}
33+
34+
/**
35+
* @expectedException \DomainException
36+
* @expectedExceptionMessage The arcsecant of this number is undefined.
37+
*/
38+
public function testArcsecBetweenOneAndNegativeOne()
39+
{
40+
Decimal::fromString('0.546')->arcsec();
41+
}
42+
}

0 commit comments

Comments
 (0)