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

Commit

Permalink
Merge pull request #49 from codisart/add-arcsec-arccsc-methods
Browse files Browse the repository at this point in the history
[TECH] Add two new methods and their tests. Thanks to @codisart .
  • Loading branch information
castarco committed Jun 23, 2016
2 parents 7a222da + 56aeab3 commit ef221ca
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/Decimal.php
Expand Up @@ -908,6 +908,68 @@ public function arccot($scale = null) {
)->round($scale);
}

/**
* Calculates the arcsecant of this with the highest possible accuracy
*
* @param integer $scale
* @return Decimal
*/
public function arcsec($scale = null) {
if($this->comp(DecimalConstants::one(), $scale + 2) === -1 && $this->comp(DecimalConstants::negativeOne(), $scale + 2) === 1) {
throw new \DomainException(
"The arcsecant of this number is undefined."
);
}

$piOverTwo = DecimalConstants::pi()->div(Decimal::fromInteger(2), $scale + 2)->round($scale);

if ($this->round($scale)->equals(DecimalConstants::one())) {
return DecimalConstants::zero();
}
if ($this->round($scale)->equals(DecimalConstants::negativeOne())) {
return DecimalConstants::pi()->round($scale);
}

$scale = ($scale === null) ? 32 : $scale;

return $piOverTwo->sub(
self::powerSerie(
DecimalConstants::one()->div($this, $scale + 2),
DecimalConstants::zero(),
$scale + 2
)
)->round($scale);
}

/**
* Calculates the arccosecant of this with the highest possible accuracy
*
* @param integer $scale
* @return Decimal
*/
public function arccsc($scale = null) {
if($this->comp(DecimalConstants::one(), $scale + 2) === -1 && $this->comp(DecimalConstants::negativeOne(), $scale + 2) === 1) {
throw new \DomainException(
"The arccosecant of this number is undefined."
);
}

$scale = ($scale === null) ? 32 : $scale;

if ($this->round($scale)->equals(DecimalConstants::one())) {
return DecimalConstants::pi()->div(Decimal::fromInteger(2), $scale + 2)->round($scale);
}
if ($this->round($scale)->equals(DecimalConstants::negativeOne())) {
return DecimalConstants::pi()->div(Decimal::fromInteger(-2), $scale + 2)->round($scale);
}

return self::powerSerie(
DecimalConstants::one()->div($this, $scale + 2),
DecimalConstants::zero(),
$scale + 2
)->round($scale);
}

/**
* Returns exp($this), said in other words: e^$this .
*
Expand Down
42 changes: 42 additions & 0 deletions tests/Decimal/DecimalArccscTest.php
@@ -0,0 +1,42 @@
<?php

use Litipk\BigNumbers\Decimal as Decimal;

/**
* @group arccsc
*/
class DecimalArccscTest extends PHPUnit_Framework_TestCase
{
public function arccscProvider() {
// Some values provided by wolframalpha
return [
['25.546', '0.03915507577327', 14],
['1.5', '0.729728', 6],
['1', '1.57079632679489662', 17],
['-1', '-1.57079632679489662', 17],
];
}

/**
* @dataProvider arccscProvider
*/
public function testSimple($nr, $answer, $digits)
{
$x = Decimal::fromString($nr);
$arccscX = $x->arccsc($digits);

$this->assertTrue(
Decimal::fromString($answer)->equals($arccscX),
"The answer must be " . $answer . ", but was " . $arccscX
);
}

/**
* @expectedException \DomainException
* @expectedExceptionMessage The arccosecant of this number is undefined.
*/
public function testArccscBetweenOneAndNegativeOne()
{
Decimal::fromString('0.546')->arccsc();
}
}
42 changes: 42 additions & 0 deletions tests/Decimal/DecimalArcsecTest.php
@@ -0,0 +1,42 @@
<?php

use Litipk\BigNumbers\Decimal as Decimal;

/**
* @group arcsec
*/
class DecimalArcsecTest extends PHPUnit_Framework_TestCase
{
public function arcsecProvider() {
// Some values provided by wolframalpha
return [
['25.546', '1.53164125102163', 14],
['1.5', '0.841068', 6],
['1', '0', 17],
['-1', '3.14159265358979324', 17],
];
}

/**
* @dataProvider arcsecProvider
*/
public function testSimple($nr, $answer, $digits)
{
$x = Decimal::fromString($nr);
$arcsecX = $x->arcsec($digits);

$this->assertTrue(
Decimal::fromString($answer)->equals($arcsecX),
"The answer must be " . $answer . ", but was " . $arcsecX
);
}

/**
* @expectedException \DomainException
* @expectedExceptionMessage The arcsecant of this number is undefined.
*/
public function testArcsecBetweenOneAndNegativeOne()
{
Decimal::fromString('0.546')->arcsec();
}
}

0 comments on commit ef221ca

Please sign in to comment.