All Projects → felixdorn → sey

felixdorn / sey

Licence: MIT license
Sey is a powerful math interpreter with infinite-precision.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to sey

decimal
An object-oriented implementation of basic math operation with arbitrary precision, using BC Math if available.
Stars: ✭ 14 (+7.69%)
Mutual labels:  bcmath

The word Sey on a blue background

Sey

Tests Formats Version Total Downloads License

Sey, pronounce say, is a powerful math interpreter with infinite-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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].