Use pear's RPN database to process the wage calculation formula

Source: Internet
Author: User
Tags asin integer division natural logarithm value of pi

A wage calculation formula is usually defined in the wage system. For example, real-time = should be issued-should be deducted and so on. To calculate such an expression, the traditional method is to use a reverse polish expression to parse it.

The following describes a simple method for computing.

Use RPN. php In the pear library of PHP. Usage:

<?
Include 'include/pear/RPN. php ';
// $ Expression = "(2 ^ 3) + sin (30 )-(! 4) + (3/4 )";
$ Expression = "(2.33*6 )";
$ RPN = new math_rpn ();
Echo $ RPN-> calculate ($ expression, 'deg ', false );
?>

That is to say, replace each item in the formula with a specific value, and then calculate the value.

The operators supported by RPN are:

VaR $ _ operation = array (
'(' => Array ('left bracket ', 0 ),
')' => Array ('Right bracket ', 1 ),
'+' => Array ('sum', 1, 2, '_ Sum '),
'-' => Array ('difference ', 1, 2,' _ Difference '),
'*' => Array ('multiplication ', 2, 2,' _ multiplication '),
'/' => Array ('division ', 2, 2,' _ division '),
'R' => array ('root', 3, 2, '_ root '),
'^' => Array ('power', 3, 2, '_ power '),
'Sin' => array ('sine', 3, 1, '_ sin '),
'Cos '=> array ('cosine', 3, 1,' _ cos '),
'Tan' => array ('tangent', 3, 1, '_ tan '),
'Asin' => array ('asine', 3, 1, '_ asin '),
'Accos '=> array ('acosine', 3, 1,' _ accos '),
'Https' => array ('hangout', 3, 1, '_ https '),
'Sqrt '=> array ('square root', 3, 1,' _ SQRT '),
'Expires' => array ('Power of E', 3, 1, '_ expires '),
'Log' => array ('logarithm ', 3, 1,' _ log '),
'Ln '=> array ('natural logarithm', 3, 1, '_ ln '),
'E' => array ('Power of 10', 3, 1, '_ e '),
'Abs '=> array ('absolute value', 3, 1,' _ ABS '),
'! '=> Array ('factorial', 3, 1, '_ factorial '),
'Pi '=> array ('value of PI', 4, 0, '_ const_pi '),
'E' => array ('value of E', 4, 0, '_ const_e '),
'Mod' => array ('modulo', 3, 2, '_ mod '),
'Div '=> array ('integer division', 3, 2, '_ Div '),
);

Of course, we can add our own functions, such as tax () for tax calculation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.