Sey is a powerful math interpreter with arbitrary-precision.

1.0.0 2021-11-05 18:02 UTC

This package is auto-updated.

Last update: 2024-04-29 04:34:27 UTC


README

The word Sey on a blue background

Sey

Tests Formats Version Total Downloads License

Sey, pronounce say, is a powerful math interpreter with arbitrary-precision.

Installation

Requires PHP 8.0.0+ and the bcmath extension

You can install the package via composer:

composer require felixdorn/sey

Usage

Sey::parse('(0.5 + 0.5) / 3)'); // 0.3333333333333333
// or
sey('a / b', ['a' => 1, 'b' => 2]); // 0.5

Precision

By default, the maximum floating precision is 16.

You may change it:

\Felix\Sey\Sey:precision(32);

Syntax

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()

Variables

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
]);

Functions

  • sqrt: bcsqrt

  • powmod: bcpowmod

  • pi(): custom bcpi function

    This function returns pi with your defined precision up to 999 digits.

  • !(n): custom bcfact function

    This computes n!, if you need to do it really quickly, you should probably use a lookup table.

Custom functions

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.

Tests

composer test

sey was created by Félix Dorn under the MIT license.