2017 © Pedro Peláez
 

library calculator-php

Calculator for arithmetic expressions

image

fintara/calculator-php

Calculator for arithmetic expressions

  • Wednesday, September 6, 2017
  • by fintara
  • Repository
  • 1 Watchers
  • 5 Stars
  • 270 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 5 Versions
  • 135 % Grown

The README.md

PHP Calculator for arithmetic expressions

PHP calculator which evaluates different arithmetic expressions:, (*1)

2+5.9       = 7.9
3.5^(2-5)   = 0.02332361516035
5-(2*(1+2)) = -1

The included functions by default are sqrt(x) and log(base, arg), but there is also an option to add custom functions with any number of arguments., (*2)

Basic usage

use \Fintara\Tools\Calculator\Calculator;

$calculator = Calculator::create(); // use default tokenizer
echo $calculator->calculate('1+2*3/4'); // 2.5

Advanced usage

Custom functions

You can add custom functions: * name: name of the function, as it will be used in expressions. All lower case and _ (underscore) allowed. * implementation: how to evaluate the function., (*3)

$calculator->addFunction('cbrt', function($x) {
    return pow($x, 1/3);
});

echo $calculator->calculate('cbrt(27)'); // 3

Tokenizer

You can also use the tokenizer (or supply calculator with your own):, (*4)

$tokenizer = new Tokenizer();
$tokens = $tokenizer->tokenize('1+2*3.5'); // [1, '+', 2, '*', 3.5]

In case the expression contains functions (e.g. sqrt(x)), the tokenizer needs to know all functions' names as second parameter., (*5)

$tokens = $tokenizer->tokenize('1+sqrt(4)', ['sqrt']); // [1, '+', 'sqrt', '(', 4, ')']

Note: The default tokenizer automatically adds * (multiplication) sign between a number and following function or a number and following parenthesis (if the sign is not found)., (*6)

$tokens = $tokenizer->tokenize('2 (1 + 3)'); // [2, '*', '(', 1, '+', 3, ')']

Changelog

2.0.1 * Added LICENSE, (*7)

2.0.0 * Breaking: operator mod is renamed to %. * Breaking: addFunction does not need number of arguments anymore (removed 3rd argument). * Breaking: adding function with existing name throws. You can use the helper replaceFunction. * Breaking: getReversePolishNotation is private. * Breaking: setExpression is removed. Use directly calculate($expression). * Breaking: ILexer and DefaultLexer are replaced with TokenizerInterface and Tokenizer respectively., (*8)

The Versions

06/09 2017

dev-feature/variables

dev-feature/variables

Calculator for arithmetic expressions

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by Tsvetan Ovedenski

06/09 2017

dev-master

9999999-dev

Calculator for arithmetic expressions

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by Tsvetan Ovedenski

06/09 2017

2.0.0

2.0.0.0

Calculator for arithmetic expressions

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by Tsvetan Ovedenski

19/02 2014

v0.1

0.1.0.0

Calculator for arithmetic expressions

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Tsvetan Ovedenski

18/02 2014

v0.1-alpha

0.1.0.0-alpha

Calculator for arithmetic expressions

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Tsvetan Ovedenski