2017 © Pedro Peláez
 

library php-calc

Simple calculation expressions

image

sufir/php-calc

Simple calculation expressions

  • Monday, January 4, 2016
  • by Sufir
  • Repository
  • 2 Watchers
  • 1 Stars
  • 3 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

sufir/php-calc

Latest Version on Packagist Build Status Quality Score Coverage Status Issues Software License, (*1)

Install

Via Composer, (*2)

``` bash $ composer require sufir/php-calc, (*3)


## Usage ``` php $lexer = new Lexer(); $converter = new Converter(); $calc = new Calc(20);

D&D d20 check example:

``` php $calc->registerFunction('d20', function () { return rand(1, 20); });, (*4)

$charAbilities = ['ability' => 15, /*... etc */]; $difficultyClass = 12; $checkExpr = 'd20() + ($ability / 2 – 5)';, (*5)

$tokens = $lexer->parse($checkExpr); $result = $calc->evaluate( $converter->converToPostfix($tokens), $charAbilities );, (*6)

// simple d20 ability check if ($result < $difficultyClass) { echo 'You fail!!!'; } else { echo 'Congratulation!'; }, (*7)


### Area of a circle example: ``` php $expr = '$Pi*$r^2'; $radiusList = [5, 10, 15, 25, 50]; $calc->defineVar('$Pi', '3.14159265358979323846'); $tokens = $lexer->parse($expr); foreach ($radiusList as $radius) { echo 'Pi * ', $radius , '^2 = ', $calc->evaluate( $converter->converToPostfix($tokens), ['r' => $radius] ), "\n"; } /* Pi * 5^2 = 78.53981633974483096150 Pi * 10^2 = 314.15926535897932384600 Pi * 15^2 = 706.85834705770347865350 Pi * 25^2 = 1963.49540849362077403750 Pi * 50^2 = 7853.98163397448309615000 */

Normal (Gaussian) distribution example:

``` php $expr = 'normal_distribution(1, $s, $m)';, (*8)

$calc->defineVar('$s', 1); $calc->defineVar('$m', 0); $calc->registerFunction('normal_distribution', function ($x, $σ = 1, $μ = 0) { $Pi = '3.1415926536'; $p = bcmul( bcdiv( 1, bcmul($σ, pow(bcmul(2, $Pi), 0.5)) ), exp( -bcdiv( pow(bcsub($x, $μ), 2), bcmul(2, pow($σ, 2)) ) ) );, (*9)

return $p;

});, (*10)

$tokens = $lexer->parse($expr); $nd = $calc->evaluate($tokens);, (*11)


## Testing ``` bash $ composer test

Credits

License

The MIT License (MIT)., (*12)

The Versions

04/01 2016

dev-master

9999999-dev https://github.com/Sufir/php-calc

Simple calculation expressions

  Sources   Download

MIT

The Requires

  • php >=5.6
  • ext-bcmath *

 

The Development Requires

by Avatar Sufir

calculation evaluation expressions

04/01 2016

1.0.3

1.0.3.0 https://github.com/Sufir/php-calc

Simple calculation expressions

  Sources   Download

MIT

The Requires

  • php >=5.6
  • ext-bcmath *

 

The Development Requires

by Avatar Sufir

calculation evaluation expressions

27/12 2015

1.0.2

1.0.2.0 https://github.com/Sufir/php-calc

Simple calculation expressions

  Sources   Download

MIT

The Requires

  • php >=5.6
  • ext-bcmath *

 

The Development Requires

by Avatar Sufir

calculation evaluation expressions

27/12 2015

1.0.1

1.0.1.0 https://github.com/Sufir/php-calc

Simple calculation expressions

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Avatar Sufir

calculation evaluation expressions

27/12 2015

1.0.0

1.0.0.0 https://github.com/Sufir/php-calc

Simple calculation expressions

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Avatar Sufir

calculation evaluation expressions