albertoarena / calculator
Simple calculator
0.2
2016-04-23 09:58 UTC
Requires
- php: >=5.3.0
- crysalead/kahlan: ^2.4
This package is not auto-updated.
Last update: 2024-04-24 23:00:05 UTC
README
Simple PHP calculator, inspired by shunting-yard algorithm.
Support of basic operators (+, -, /, *, ^), trigonometric operators and precedence.
Operators
The Order of operators follows the standard (see Wikipedia):
High precedence
^
: exponent (power)√
: square root (aliassqrt
)
Medium precedence
\*
: multiplication/
: divisionsin
: sinecos
: cosinetan
: tangentasin
: arc sineacos
: arc cosineatan
: arc tangent
Low precedence
+
: addition-
: subtraction
How to use it
A working example is available in samples/basic.php
.
use Calculator\Calculator; $calculator = new Calculator(); // Basic operators $calculator->number(1) ->operator('+')->number(5) ->operator('*')->number(3.5) ->operator('/')->number(2) ->execute(); echo $calculator; // ==> 1 + 5 * 3.5 / 2 = 9.75 // Power $calculator->number(2) ->operator('*')->number(2) ->operator('^')->number(4) ->execute(); echo $calculator; // ==> 2 * 2 ^ 4 = 32 // Square root $result = $calculator->number(9) ->operator('√') ->execute(); echo $calculator; // ==> √ 9 = 3 // Trigonometry $result = $calculator->number(1) ->operator('cos') ->execute(); echo $calculator; // ==> cos (1) = 0.54030230586814