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

Commit 094efa1

Browse files
committed
Merge pull request #20 from punkka/tan-fix
Fix on the tan function when the value is undefined like for tan(pi/2)
2 parents 944a883 + debaec1 commit 094efa1

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/Decimal.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,14 @@ public function cos($scale = null) {
743743
* @return Decimal tan($this)
744744
*/
745745
public function tan($scale = null) {
746-
return $this->sin($scale + 2)->div($this->cos($scale + 2))->round($scale);
746+
$cos = $this->cos($scale + 2);
747+
if ($cos->isZero()) {
748+
throw new \DomainException(
749+
"The tangent of this 'angle' is undefined."
750+
);
751+
}
752+
753+
return $this->sin($scale + 2)->div($cos)->round($scale);
747754
}
748755

749756
/**

tests/Decimal/DecimalTanTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Litipk\BigNumbers\Decimal as Decimal;
4+
use \Litipk\BigNumbers\DecimalConstants as DecimalConstants;
45

56
/**
67
* @group tan
@@ -29,4 +30,19 @@ public function testSimple($nr, $answer, $digits)
2930
'tan('.$nr.') must be equal to '.$answer.', but was '.$tanX
3031
);
3132
}
33+
34+
public function testTanPiTwoDiv()
35+
{
36+
$PI = DecimalConstants::PI();
37+
$two = Decimal::fromInteger(2);
38+
$PiDividedByTwo = $PI->div($two);
39+
40+
$catched = false;
41+
try {
42+
$PiDividedByTwo->tan();
43+
} catch (\DomainException $e) {
44+
$catched = true;
45+
}
46+
$this->assertTrue($catched);
47+
}
3248
}

0 commit comments

Comments
 (0)