This repository was archived by the owner on Dec 27, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +65
-0
lines changed Expand file tree Collapse file tree 2 files changed +65
-0
lines changed Original file line number Diff line number Diff line change @@ -753,6 +753,24 @@ public function tan($scale = null) {
753
753
return $ this ->sin ($ scale + 2 )->div ($ cos )->round ($ scale );
754
754
}
755
755
756
+ /**
757
+ * Calculates the cotangent of this method with the highest possible accuracy
758
+ * Note that accuracy is limited by the accuracy of predefined PI;
759
+ *
760
+ * @param integer $scale
761
+ * @return Decimal cotan($this)
762
+ */
763
+ public function cotan ($ scale = null ) {
764
+ $ sin = $ this ->sin ($ scale + 2 );
765
+ if ($ sin ->isZero ()) {
766
+ throw new \DomainException (
767
+ "The cotangent of this 'angle' is undefined. "
768
+ );
769
+ }
770
+
771
+ return $ this ->cos ($ scale + 2 )->div ($ sin )->round ($ scale );
772
+ }
773
+
756
774
/**
757
775
* Indicates if the passed parameter has the same sign as the method's bound object.
758
776
*
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ use \Litipk \BigNumbers \Decimal as Decimal ;
4
+ use \Litipk \BigNumbers \DecimalConstants as DecimalConstants ;
5
+
6
+ /**
7
+ * @group cotan
8
+ */
9
+ class DecimalCotanTest extends PHPUnit_Framework_TestCase
10
+ {
11
+ public function cotanProvider () {
12
+ // Some values providede by mathematica
13
+ return array (
14
+ array ('1 ' , '0.64209261593433 ' , 14 ),
15
+ array ('123.123 ' , '1.45891895739232371 ' , 17 ),
16
+ array ('15000000000 ' , '-1.04405948230055701685 ' , 20 )
17
+
18
+ );
19
+ }
20
+
21
+ /**
22
+ * @dataProvider cotanProvider
23
+ */
24
+ public function testSimple ($ nr , $ answer , $ digits )
25
+ {
26
+ $ x = Decimal::fromString ($ nr );
27
+ $ cotanX = $ x ->cotan ($ digits );
28
+ $ this ->assertTrue (
29
+ Decimal::fromString ($ answer )->equals ($ cotanX ),
30
+ 'cotan( ' .$ nr .') must be equal to ' .$ answer .', but was ' .$ cotanX
31
+ );
32
+ }
33
+
34
+ public function testCotanPiDiv ()
35
+ {
36
+ $ PI = DecimalConstants::PI ();
37
+
38
+ $ catched = false ;
39
+ try {
40
+ $ PI ->cotan ();
41
+ } catch (\DomainException $ e ) {
42
+ $ catched = true ;
43
+ }
44
+ $ this ->assertTrue ($ catched );
45
+ }
46
+
47
+ }
You can’t perform that action at this time.
0 commit comments