phpcalc/README.md

1.2 KiB

PHP Calc

The CLI calculator you never knew you needed. Also usable as a library!

Usage

CLI

Call with an expression to evaluate:

$ calc "42*atan(pi/2)"
42.163162517863

Or call without arguments to go interactive:

$ calc
~ 42*atan(pi/2)
  42.163162517863

Library

As simple as falling off a rock:

use NoccyLabs\PhpCalc\Calculator;

$calc = new Calculator();
$result = $calc->evaluate("42 * atan(pi/2)");

What it does

Supported operations:

  • + addition
  • - subtraction
  • * multiplication
  • / division
  • ^ exponent/raise to power

Supported functions:

  • abs
  • acos
  • acosh
  • asin
  • asin
  • atan
  • atanh
  • ceil
  • cos
  • cosh
  • floor
  • sin
  • sinh
  • log
  • round
  • sqrt
  • tan
  • tanh

Constants:

  • pi

Useful tips

  • You can set the precision using the _p variable. If the expression is passed on the command line, prepend the variable followed by a semicolon, such as _p=2;pi for the result 3.14.

Bugs and known issues

  • Negative numbers and subtraction should be surrounded by whitespace to parse properly. Passing 2-1 will result in 2 and -1, returning 2 and a warning. Passing 2 - 1 will return 1 as expected, as will 2- 1.