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

Commit ec108cd

Browse files
committed
Merge pull request #45 from codisart/add-arccos-method
[TECH] Add the arccosine function
2 parents adbd243 + ad00595 commit ec108cd

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
lines changed

src/Decimal.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ public function sec($scale = null)
781781
/**
782782
* Calculates the arcsine of this with the highest possible accuracy
783783
*
784-
* @param integer $scale [description]
784+
* @param integer $scale
785785
* @return Decimal
786786
*/
787787
public function arcsin($scale = null)
@@ -810,6 +810,44 @@ public function arcsin($scale = null)
810810
$scale
811811
);
812812
}
813+
814+
/**
815+
* Calculates the arccosine of this with the highest possible accuracy
816+
*
817+
* @param integer $scale
818+
* @return Decimal
819+
*/
820+
public function arccos($scale = null)
821+
{
822+
if($this->comp(DecimalConstants::one(), $scale + 2) === 1 || $this->comp(DecimalConstants::negativeOne(), $scale + 2) === -1) {
823+
throw new \DomainException(
824+
"The arccos of this number is undefined."
825+
);
826+
}
827+
828+
$piOverTwo = DecimalConstants::pi()->div(Decimal::fromInteger(2), $scale + 2)->round($scale);
829+
830+
if ($this->round($scale)->isZero()) {
831+
return $piOverTwo;
832+
}
833+
if ($this->round($scale)->equals(DecimalConstants::one())) {
834+
return DecimalConstants::zero();
835+
}
836+
if ($this->round($scale)->equals(DecimalConstants::negativeOne())) {
837+
return DecimalConstants::pi()->round($scale);
838+
}
839+
840+
$scale = ($scale === null) ? 32 : $scale;
841+
842+
return $piOverTwo->sub(
843+
self::powerSerie(
844+
$this,
845+
DecimalConstants::zero(),
846+
$scale
847+
)
848+
)->round($scale);
849+
}
850+
813851
/**
814852
* Returns exp($this), said in other words: e^$this .
815853
*

tests/Decimal/DecimalArccosTest.php

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

tests/Decimal/DecimalArcsinTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class DecimalArcsinTest extends PHPUnit_Framework_TestCase
99
{
1010
public function arcsinProvider() {
11-
// Some values providede by wolframalpha
11+
// Some values provided by wolframalpha
1212
return [
1313
['0.154', '0.15461530016096', 14],
1414
['1', '1.57079632679489662', 17],

0 commit comments

Comments
 (0)