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

Commit 86ec451

Browse files
committed
[TECH] Add the sec method
Add the sec method for the Decimal class and the tests Add the sec method for the InfiniteDecimal class and the tests
1 parent b8c89c8 commit 86ec451

File tree

5 files changed

+94
-1
lines changed

5 files changed

+94
-1
lines changed

src/Decimal.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,26 @@ function ($i) {
759759
);
760760
}
761761

762+
/**
763+
* Calculates the secant of this with the highest possible accuracy
764+
* Note that accuracy is limited by the accuracy of predefined PI;
765+
*
766+
* @param integer $scale
767+
* @return Decimal
768+
*/
769+
public function sec($scale = null)
770+
{
771+
$cos = $this->cos($scale + 2);
772+
if ($cos->isZero()) {
773+
throw new \DomainException(
774+
"The secant of this 'angle' is undefined."
775+
);
776+
}
777+
778+
return Decimal::fromInteger(1)->div($cos)->round($scale);
779+
}
780+
781+
762782
/**
763783
* Returns exp($this), said in other words: e^$this .
764784
*

src/InfiniteDecimal.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,20 @@ public function cos($scale = null)
339339
);
340340
}
341341

342+
/**
343+
* Throws exception because secant is undefined in the infinite.
344+
*
345+
* @param integer $scale
346+
* @return null
347+
*/
348+
public function sec($scale = null)
349+
{
350+
throw new \DomainException(($this === self::$pInf) ?
351+
"Secant function hasn't limit in the positive infinite." :
352+
"Secant function hasn't limit in the negative infinite."
353+
);
354+
}
355+
342356
/**
343357
* Returns exp($this), said in other words: e^$this .
344358
*

tests/Decimal/DecimalSecTest.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 sec
7+
*/
8+
class DecimalSecTest extends PHPUnit_Framework_TestCase
9+
{
10+
public function SecProvider() {
11+
// Some values provided by Mathematica
12+
return [
13+
['5', '3.52532008581609', 14],
14+
['456.456', '-1.66172995090378344', 17],
15+
['28000000000', '-1.11551381955633891873', 20],
16+
];
17+
}
18+
19+
/**
20+
* @dataProvider secProvider
21+
*/
22+
public function testSimple($nr, $answer, $digits)
23+
{
24+
$x = Decimal::fromString($nr);
25+
$secX = $x->sec((int)$digits);
26+
27+
$this->assertTrue(
28+
Decimal::fromString($answer)->equals($secX),
29+
"The answer must be " . $answer . ", but was " . $secX
30+
);
31+
}
32+
}

tests/InfiniteDecimal/InfiniteDecimalCosecTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Litipk\BigNumbers\InfiniteDecimal as InfiniteDecimal;
44

55
/**
6-
* @group cos
6+
* @group cosec
77
*/
88
class InfiniteDecimalCosecTest extends PHPUnit_Framework_TestCase
99
{
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 sec
7+
*/
8+
class InfiniteDecimalSecTest extends PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @expectedException \DomainException
12+
* @expectedExceptionMessage Secant function hasn't limit in the positive infinite.
13+
*/
14+
public function testFinitePositiveInfiniteSec()
15+
{
16+
InfiniteDecimal::getPositiveInfinite()->sec();
17+
}
18+
19+
/**
20+
* @expectedException \DomainException
21+
* @expectedExceptionMessage Secant function hasn't limit in the negative infinite.
22+
*/
23+
public function testFiniteNegativeInfiniteSec()
24+
{
25+
InfiniteDecimal::getNegativeInfinite()->sec();
26+
}
27+
}

0 commit comments

Comments
 (0)