add new functions and aliases to the old functions (#76)

* add new functions and aliases to the old functions

* add tests for new functions
This commit is contained in:
diman3210 2020-10-20 01:48:30 +03:00 committed by GitHub
parent d9eb39e38d
commit e28c1bf9e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 117 additions and 7 deletions

View file

@ -34,17 +34,22 @@ echo $executor->execute('1 + 2 * (2 - (4+10))^2 + sin(10)');
## Functions: ## Functions:
Default functions: Default functions:
* abs * abs
* acos * acos (arccos)
* acosh * acosh
* asin * arcctg (arccot, arccotan)
* atan (atn) * arcsec
* arccsc (arccosec)
* asin (arcsin)
* atan (atn, arctan, arctg)
* atan2 * atan2
* atanh * atanh
* avg * avg
* bindec * bindec
* ceil * ceil
* cos * cos
* cosec (csc)
* cosh * cosh
* ctg (cot, cotan, cotg, ctn)
* decbin * decbin
* dechex * dechex
* decoct * decoct
@ -57,8 +62,8 @@ Default functions:
* hypot * hypot
* if * if
* intdiv * intdiv
* log * log (ln)
* log10 * log10 (lg)
* log1p * log1p
* max * max
* min * min
@ -67,10 +72,11 @@ Default functions:
* pow * pow
* rad2deg * rad2deg
* round * round
* sec
* sin * sin
* sinh * sinh
* sqrt * sqrt
* tan (tn) * tan (tn, tg)
* tanh * tanh
Add custom function to executor: Add custom function to executor:

View file

@ -220,6 +220,36 @@ class MathExecutor
'acosh' => function ($arg) { 'acosh' => function ($arg) {
return acosh($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) { 'asin' => function ($arg) {
return asin($arg); return asin($arg);
}, },
@ -247,9 +277,30 @@ class MathExecutor
'cos' => function ($arg) { 'cos' => function ($arg) {
return cos($arg); return cos($arg);
}, },
'cosec' => function ($arg) {
return 1 / sin($arg);
},
'csc' => function ($arg) {
return 1 / sin($arg);
},
'cosh' => function ($arg) { 'cosh' => function ($arg) {
return cosh($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) { 'decbin' => function ($arg) {
return decbin($arg); return decbin($arg);
}, },
@ -295,6 +346,12 @@ class MathExecutor
'intdiv' => function ($arg1, $arg2) { 'intdiv' => function ($arg1, $arg2) {
return intdiv($arg1, $arg2); return intdiv($arg1, $arg2);
}, },
'ln' => function ($arg) {
return log($arg);
},
'lg' => function ($arg) {
return log10($arg);
},
'log' => function ($arg) { 'log' => function ($arg) {
return log($arg); return log($arg);
}, },
@ -317,7 +374,7 @@ class MathExecutor
return pi(); return pi();
}, },
'pow' => function ($arg1, $arg2) { 'pow' => function ($arg1, $arg2) {
return pow($arg1, $arg2); return $arg1 ** $arg2;
}, },
'rad2deg' => function ($arg) { 'rad2deg' => function ($arg) {
return rad2deg($arg); return rad2deg($arg);
@ -331,6 +388,9 @@ class MathExecutor
'sinh' => function ($arg) { 'sinh' => function ($arg) {
return sinh($arg); return sinh($arg);
}, },
'sec' => function ($arg) {
return 1 / cos($arg);
},
'sqrt' => function ($arg) { 'sqrt' => function ($arg) {
return sqrt($arg); return sqrt($arg);
}, },
@ -342,6 +402,9 @@ class MathExecutor
}, },
'tn' => function ($arg) { 'tn' => function ($arg) {
return tan($arg); return tan($arg);
},
'tg' => function ($arg) {
return tan($arg);
} }
]; ];
} }

View file

@ -526,4 +526,45 @@ class MathTest extends TestCase
$this->expectException(MathExecutorException::class); $this->expectException(MathExecutorException::class);
$calculator->setVar('resource', tmpfile()); $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],
];
}
} }