Implements a simple floating point arithmetic expression evaluator in Go (golang).
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.


Build Status Coverage GoDoc

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.


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


The operators supported are:

+ - * / ** () < >

< (less than) and > (greater than) get lowest precedence, all other precedence is as expected - BODMAS.

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

3 * (1 < 2) # returns 3.0
3 * (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:


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


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.


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


Contributions are welcome.

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

  1. fork this repo on the GitHub webpage
  2. go get
  3. cd $GOPATH/src/
  4. git remote rename origin upstream
  5. git remote add origin<your-github-username>/evaler.git
  6. git checkout -b development
  7. git push -u origin development (setup where you push to, check it works)


Sonia Hamilton

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


Modified BSD License (BSD-3)