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

Commit dce3858

Browse files
committed
Fix on the tan function when the value is undefined like for tan(pi/2)
1 parent bad0151 commit dce3858

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/Decimal.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -742,9 +742,16 @@ public function cos($scale = null) {
742742
* @return Decimal tan($this)
743743
*/
744744
public function tan($scale = null) {
745-
return $this->sin($scale + 2)
746-
->div($this->cos($scale + 2))
747-
->floor($scale);
745+
$cos = $this->cos($scale + 2);
746+
if ($cos->isZero()) {
747+
throw new \DomainException(
748+
"The tangent of this 'angle' is undefined."
749+
);
750+
}
751+
752+
return $this->sin($scale + 2)
753+
->div($cos)
754+
->floor($scale);
748755
}
749756

750757
public function hasSameSign(Decimal $b) {

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
@@ -26,4 +27,19 @@ public function testSimple($nr, $answer, $digits)
2627
$tanX = $x->tan($digits);
2728
$this->assertTrue(Decimal::fromString($answer)->equals($tanX));
2829
}
30+
31+
public function testTanPiTwoDiv()
32+
{
33+
$PI = DecimalConstants::PI();
34+
$two = Decimal::fromInteger(2);
35+
$PiDividedByTwo = $PI->div($two);
36+
37+
$catched = false;
38+
try {
39+
$PiDividedByTwo->tan();
40+
} catch (\DomainException $e) {
41+
$catched = true;
42+
}
43+
$this->assertTrue($catched);
44+
}
2945
}

0 commit comments

Comments
 (0)