Sey, pronounce say, is a powerful math interpreter with arbitrary-precision.
Requires PHP 8.0.0+ and the
bcmathextension
You can install the package via composer:
composer require felixdorn/seySey::parse('(0.5 + 0.5) / 3)'); // 0.3333333333333333 // or sey('a / b', ['a' => 1, 'b' => 2]); // 0.5By default, the maximum floating precision is 16.
You may change it:
\Felix\Sey\Sey:precision(32);It's just math.
1 + 2 2 - 3 3 * 4 4 / 5 5 % 6 6 ^ 7 7 * (8 + 9) sqrt(10) powmod(11) 11(12 - 13) (14 + 15)^16 !(5) pi() You can not define variables in your code but you can pass them at compile-time.
Sey::parse('2 * r * pi', [ 'r' => 10, 'pi' => 3.1415 ]);-
sqrt:bcsqrt -
powmod:bcpowmod -
pi(): custombcpifunctionThis function returns pi with your defined precision up to 999 digits.
-
!(n): custombcfactfunctionThis computes
n!, if you need to do it really quickly, you should probably use a lookup table.
You can override built-ins functions.
Sey::define('!', function (int $n, /* as many arguments as you want */) { return $factorials[$n] ?? bcfact($n); });The function name must match the following regex [a-z_A-Z!]+[a-z_A-Z0-9]*.
So, first character must be a letter or ! followed by any number of letters or numbers.
composer testsey was created by Félix Dorn under the MIT license.