Configuration

Arokettu\ArithmeticParser\Config class is used to configure the calculator and the parser.

Config is mutable but it is cloned internally when passed to prevent external alterations.

Config::default()

The default preset used when no config is specified.

Functions

Added in version 1.1.0: removeFunction() and clearFunctions()

The function must be a callable that accepts a single float argument.

Default functions: abs, exp, log, log10, sqrt, acos, asin, atan, cos, sin, tan, acosh, asinh, atanh, cosh, sinh, tanh, ceil, floor, round, deg2rad, rad2deg.

You can:

  • Replace functions with your own list:

    <?php
    $config->setFunctions(myfunc2: fn ($a) => a ** 2);
    
  • Add new functions:

    <?php
    $config->addFunctions(myfunc3: fn ($a) => a ** 3);
    
  • Remove functions:

    <?php
    $config->removeFunctions('acos', 'asin');
    $config->removeFunction('acos'); // semantic alias for removeFunctions('acos')
    $config->clearFunctions(); // remove all functions
    

Operators

Added in version 1.1.0: removeOperator() and clearOperators()

Operators can be unary and binary. Operator symbol can be any string without digits. Be wary when using latin character based operators, they are case-sensitive and may shadow variables and functions.

Default operators:

  • +, - in both unary and binary form. They are built-in and are not configurable.

  • *, /.

You can:

  • Replace operators with your own list:

    <?php
    $config->setOperators(
        new BinaryOperator('×', fn ($a, $b) => $a * $b, BinaryOperator::PRIORITY_MUL),
        new BinaryOperator('÷', fn ($a, $b) => $a / $b, BinaryOperator::PRIORITY_MUL),
    );
    
  • Add new operators:

    <?php
    // assuming you have factorial() defined
    $config->addOperators(
        new BinaryOperator('^', pow(...), BinaryOperator::PRIORITY_POW, BinaryAssoc::RIGHT),
        new UnaryOperator('!', factorial(...), UnaryPos::POSTFIX),
    );
    
  • Remove operators:

    <?php
    // remove any custom or built-in operators except for + and -
    $config->removeOperators('*', '/');
    // you cannot divide by zero if you cannot divide
    $config->removeOperator('/'); // semantic alias for removeOperators('/')
    // leave only + and -
    $config->clearOperators(); // + and - are handled specially and can't be removed