evaler - 一个简单的浮点算术表达式求值Go (golang)程序

网友投稿 704 2022-10-27

evaler - 一个简单的浮点算术表达式求值Go (golang)程序

evaler - 一个简单的浮点算术表达式求值Go (golang)程序

evaler

Package evaler implements a simple floating point arithmetic expression evaluator.

Evaler uses Dijkstra's Shunting Yard algorithm to convert an infix expression to postfix/RPN format, then evaluates the RPN expression. The implementation is adapted from a Java implementation. The results are returned as a *big.Rat.

Usage

result, err := evaler.Eval("1+2")

Operators

The operators supported are:

+ - * / ^ ** () < > <= >= == !=

(^ and ** are both exponent operators)

Logical operators like < (less than) or > (greater than) get lowest precedence, all other precedence is as expected - BODMAS.

Logical tests like < and > tests will evaluate to 0.0 for false and 1.0 for true, allowing expressions like:

3 * (1 < 2) # returns 3.03 * (1 > 2) # returns 0.0

Minus implements both binary and unary operations.

See evaler_test.go for more examples of using operators.

Trigonometric Operators

The trigonometric operators supported are:

sin, cos, tan, ln, arcsin, arccos, arctan

For example:

cos(1)sin(2-1)sin(1)+2**2

See evaler_test.go for more examples of using trigonometric operators.

Variables

EvalWithVariables() allows variables to be passed into expressions, for example evaluate "x + 1", where x=5.

See evaler_test.go for more examples of using variables.

Issues

The math/big library doesn't have an exponent function ** and implenting one for big.Rat numbers is non-trivial. As a work around, arguments are converted to float64's, the calculation is done using the math.Pow() function, the result is converted to a big.Rat and placed back on the stack.

floating point numbers missing leading digits (like ".5 * 2") are failing - PR's welcome

Documentation

http://godoc.org/github.com/soniah/evaler

There are also a number of utility functions e.g. BigratToFloat(), BigratToInt() that may be useful when working with evaler.

Contributions

Contributions are welcome.

If you've never contributed to a Go project before here is an example workflow.

fork this repo on the GitHub webpagego get github.com/soniah/evalercd $GOPATH/src/github.com/soniah/evalergit remote rename origin upstreamgit remote add origin git@github.com:/evaler.gitgit checkout -b developmentgit push -u origin development (setup where you push to, check it works)

Author

Sonia Hamilton sonia@snowfrog-

Dem Waffles dem-waffles@server.fake - trigonometric operators

License

Modified BSD License (BSD-3)

Links

[1] http://en.wikipedia.org/wiki/Shunting-yard_algorithm

[2] http://en.wikipedia.org/wiki/Reverse_Polish_notation

[3] http://willcode4beer.com/design.jsp?set=evalInfix

[4] http://mathsisfun.com/operation-order-bodmas.html

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:kalilinux安装VMware Tools(水文)
下一篇:dubbo服务注册到nacos的过程剖析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~