aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordiman3210 <diman-3210@mail.ru>2020-10-20 01:48:30 +0300
committerGitHub <noreply@github.com>2020-10-20 01:48:30 +0300
commite28c1bf9e7683e3b1adc3f487c3a993c0d8b2b2d (patch)
tree3351aecda276f666b821a57ed79b0e5d8d95f9d3
parentd9eb39e38d090eaf00904a37d04aef6db4ceadac (diff)
add new functions and aliases to the old functions (#76)
* add new functions and aliases to the old functions * add tests for new functions
-rw-r--r--README.md18
-rw-r--r--src/NXP/MathExecutor.php65
-rw-r--r--tests/MathTest.php41
3 files changed, 117 insertions, 7 deletions
diff --git a/README.md b/README.md
index f88e5e3..f589116 100644
--- a/README.md
+++ b/README.md
@@ -34,17 +34,22 @@ echo $executor->execute('1 + 2 * (2 - (4+10))^2 + sin(10)');
## Functions:
Default functions:
* abs
-* acos
+* acos (arccos)
* acosh
-* asin
-* atan (atn)
+* arcctg (arccot, arccotan)
+* arcsec
+* arccsc (arccosec)
+* asin (arcsin)
+* atan (atn, arctan, arctg)
* atan2
* atanh
* avg
* bindec
* ceil
* cos
+* cosec (csc)
* cosh
+* ctg (cot, cotan, cotg, ctn)
* decbin
* dechex
* decoct
@@ -57,8 +62,8 @@ Default functions:
* hypot
* if
* intdiv
-* log
-* log10
+* log (ln)
+* log10 (lg)
* log1p
* max
* min
@@ -67,10 +72,11 @@ Default functions:
* pow
* rad2deg
* round
+* sec
* sin
* sinh
* sqrt
-* tan (tn)
+* tan (tn, tg)
* tanh
Add custom function to executor:
diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php
index b7abcad..1debc50 100644
--- a/src/NXP/MathExecutor.php
+++ b/src/NXP/MathExecutor.php
@@ -220,6 +220,36 @@ class MathExecutor
'acosh' => function ($arg) {
return acosh($arg);
},
+ 'arcsin' => function ($arg) {
+ return asin($arg);
+ },
+ 'arcctg' => function ($arg) {
+ return M_PI/2 - atan($arg);
+ },
+ 'arccot' => function ($arg) {
+ return M_PI/2 - atan($arg);
+ },
+ 'arccotan' => function ($arg) {
+ return M_PI/2 - atan($arg);
+ },
+ 'arcsec' => function ($arg) {
+ return acos(1/$arg);
+ },
+ 'arccosec' => function ($arg) {
+ return asin(1/$arg);
+ },
+ 'arccsc' => function ($arg) {
+ return asin(1/$arg);
+ },
+ 'arccos' => function ($arg) {
+ return acos($arg);
+ },
+ 'arctan' => function ($arg) {
+ return atan($arg);
+ },
+ 'arctg' => function ($arg) {
+ return atan($arg);
+ },
'asin' => function ($arg) {
return asin($arg);
},
@@ -247,9 +277,30 @@ class MathExecutor
'cos' => function ($arg) {
return cos($arg);
},
+ 'cosec' => function ($arg) {
+ return 1 / sin($arg);
+ },
+ 'csc' => function ($arg) {
+ return 1 / sin($arg);
+ },
'cosh' => function ($arg) {
return cosh($arg);
},
+ 'ctg' => function ($arg) {
+ return cos($arg) / sin($arg);
+ },
+ 'cot' => function ($arg) {
+ return cos($arg) / sin($arg);
+ },
+ 'cotan' => function ($arg) {
+ return cos($arg) / sin($arg);
+ },
+ 'cotg' => function ($arg) {
+ return cos($arg) / sin($arg);
+ },
+ 'ctn' => function ($arg) {
+ return cos($arg) / sin($arg);
+ },
'decbin' => function ($arg) {
return decbin($arg);
},
@@ -295,6 +346,12 @@ class MathExecutor
'intdiv' => function ($arg1, $arg2) {
return intdiv($arg1, $arg2);
},
+ 'ln' => function ($arg) {
+ return log($arg);
+ },
+ 'lg' => function ($arg) {
+ return log10($arg);
+ },
'log' => function ($arg) {
return log($arg);
},
@@ -317,7 +374,7 @@ class MathExecutor
return pi();
},
'pow' => function ($arg1, $arg2) {
- return pow($arg1, $arg2);
+ return $arg1 ** $arg2;
},
'rad2deg' => function ($arg) {
return rad2deg($arg);
@@ -331,6 +388,9 @@ class MathExecutor
'sinh' => function ($arg) {
return sinh($arg);
},
+ 'sec' => function ($arg) {
+ return 1 / cos($arg);
+ },
'sqrt' => function ($arg) {
return sqrt($arg);
},
@@ -342,6 +402,9 @@ class MathExecutor
},
'tn' => function ($arg) {
return tan($arg);
+ },
+ 'tg' => function ($arg) {
+ return tan($arg);
}
];
}
diff --git a/tests/MathTest.php b/tests/MathTest.php
index 4ea0cb0..65d8eac 100644
--- a/tests/MathTest.php
+++ b/tests/MathTest.php
@@ -526,4 +526,45 @@ class MathTest extends TestCase
$this->expectException(MathExecutorException::class);
$calculator->setVar('resource', tmpfile());
}
+
+ /**
+ * @dataProvider providerExpressionValues
+ */
+ public function testCalculatingValues($expression, $value)
+ {
+ $calculator = new MathExecutor();
+
+ try {
+ $result = $calculator->execute($expression);
+ } catch (Exception $e) {
+ $this->fail(sprintf("Exception: %s (%s:%d), expression was: %s", get_class($e), $e->getFile(), $e->getLine(), $expression));
+ }
+ $this->assertEquals($value, $result, "${expression} did not evaluate to {$value}");
+ }
+
+ /**
+ * Expressions data provider
+ *
+ * Most tests can go in here. The idea is that each expression will be evaluated by MathExecutor and by PHP with eval.
+ * The results should be the same. If they are not, then the test fails. No need to add extra test unless you are doing
+ * something more complex and not a simple mathmatical expression.
+ */
+ public function providerExpressionValues()
+ {
+ return [
+ ['arcsec(4)', 1.3181160716528],
+ ['arccsc(4)', 0.2526802551421],
+ ['arctan(4)', 1.3258176636680],
+ ['cosec(4)', -1.3213487088109],
+ ['cotan(4)', 0.8636911544506],
+ ['sec(4)', -1.5298856564664],
+ ['tg(4)', 1.1578212823496],
+ ['arcsin(0.5)', 0.5235987755983],
+ ['arccosec(4)', 0.2526802551421],
+ ['arccos(0.5)', 1.0471975511966],
+ ['arccotan(4)', 0.2449786631269],
+ ['ln(2)', 0.6931471805599],
+ ['lg(2)', 0.3010299956639],
+ ];
+ }
} \ No newline at end of file