Implements a simple floating point arithmetic expression evaluator in Go (golang).

Related tags

go golang


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:

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

(^ 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.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.


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.


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


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 [email protected]:<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 [email protected]

Dem Waffles [email protected] - trigonometric operators


Modified BSD License (BSD-3)






  • Support unary minus.

    Support unary minus.

    Hi @soniah

    I've enhanced the library to support unary minus. If this enhancement has worth for the library, could you review the code and merge it into master?

    opened by hiroxy 4
  • Add support for variables

    Add support for variables

    The Eval function should also accept a hashmap of string -> float64 for passing variables and their values in. This should use a symboltable under the covers.

    I will likely work on this feature.

    opened by thecardinelli 2
  • Fixed issue #6

    Fixed issue #6

    opened by thecardinelli 2
  • #5 expression fails with wrong result

    #5 expression fails with wrong result

    fixed #5 If you would like to pull the changes in, feel free.

    opened by hiroxy 2
  • fixed floating point number regex, added test

    fixed floating point number regex, added test

    There was a bug allowing only one digit after the decimal point.

    opened by lexszero 1
  • Review of evaler

    Review of evaler

    opened by rameshb2 1
  • Add a SQRT method

    Add a SQRT method

    opened by reteps 1
  • fails w/o initial zeros

    fails w/o initial zeros

    .5 * 2 comes out as 10 fix is var fp_rx = regexp.MustCompile(([0-9]*\.?[0-9]+))
    I believe. (and add a test like): {".5 * 2", big.NewRat(1, 1), true}, // no leading zero

    tried to submit, but I clearly dont know how to use github submitting as issue. -s

    opened by stevesmoot 1
  • Added == and != operators to the library.

    Added == and != operators to the library.


    I added a couple comparison operators to your library, if you would like to pull the changes in, feel free.

    Thanks, Jim

    opened by bocajim 1
  • Add support for trigonometric and logarithmic functions

    Add support for trigonometric and logarithmic functions

    e.g. sin(1) = 0.84 sin(0) = 0 ... ln(0) = 1

    opened by thecardinelli 1
  • radix?


    numbers that lead with '0' is radix 8 and '0x' is 16.

    opened by rbucker 0
  • add modulo operator?

    add modulo operator?

    BTW this was very simple to add to picol.go. Could use the module operator.

    opened by rbucker 0
  • allow variable names to contain numbers

    allow variable names to contain numbers

    Unfortunately it gives an error if I try to pass a variable name which has a number in it; For example, a map with {"catX":"33"} evaluates fine; but if I pass: {"cat1":"33"} it says that there is no such variable "cat", ignoring the number in the name

    if there is no way to solve this, maybe indicate that in the "readme" file

    opened by JJBordy 0
  • pass two or more variables to expression,it doesn't work

    pass two or more variables to expression,it doesn't work

    for k := range symbolTable { s += k } symbols_rx = regexp.MustCompile(fmt.Sprintf("(%s)", s)) func Tokenis (expr string) []string { if symbols_rx != nil { spaced = symbols_rx.ReplaceAllString(spaced, " ${1} ")//it doesn't work } }

    opened by lx217197 0
  • goroutine-safe?


    It seems this lib is not goroutine-safe for there is a variable symbolTable in package.

    opened by sailorfeng 0
Sonia Hamilton
Australian IT professional; ex-Google; ex gosnmp maintainer
Sonia Hamilton
An arbitrary-precision decimal floating-point arithmetic package for Go

decimal Package decimal implements arbitrary-precision decimal floating-point arithmetic for Go. Rationale How computers represent numbers internally

Denis Bernard 22 Jun 5, 2021
Arbitrary-precision decimals for Go

apd apd is an arbitrary-precision decimal package for Go. apd implements much of the decimal specification from the General Decimal Arithmetic descrip

CockroachDB 358 May 23, 2021
Arbitrary-precision fixed-point decimal numbers in go

decimal Arbitrary-precision fixed-point decimal numbers in go. Note: Decimal library can "only" represent numbers with a maximum of 2^31 digits after

Spring Engineering 3.2k Jul 24, 2021
A fast SAT solver

Gini SAT Solver The Gini sat solver is a fast, clean SAT solver written in Go. It is to our knowledge the first ever performant pure-Go SAT solver mad

IRI France R&D Open Organisation 121 Jul 20, 2021
Go library that makes it easy to work with physical types, e.g. distances, velocities and angles.

Units Use at your own risk Note that this library is NOT production ready. If you want to use it anyway, contributions and bug reports are welcome! Br

Sindre R√łkenes Myren 19 Feb 26, 2021
Go language bindings for the COIN-OR Linear Programming library

clp Description The clp package provides a Go interface to the COIN-OR Linear Programming (CLP) library, part of the COIN-OR (COmputational INfrastruc

Los Alamos National Laboratory 23 Jun 3, 2021
high performance fixed decimal place math library for Go

Summary A fixed place numeric library designed for performance. All numbers have a fixed 7 decimal places, and the maximum permitted value is +- 99999

robert engels 268 Jul 14, 2021
Naive Bayesian Classification for Golang.

Naive Bayesian Classification Perform naive Bayesian classification into an arbitrary number of classes on sets of strings. bayesian also supports ter

Jake Brukhman 710 Jul 16, 2021
This is a go implementation of Segmented Sieve and non Segmented sieve to produce prime numbers concurrently.

Prime This is a Go library to produce prime numbers using all available cpu cores. Installation $ go get Usage package main

Kaveh Mousavi Zamani 21 Nov 7, 2020
A well tested and comprehensive Golang statistics library package with no dependencies.

Stats - Golang Statistics Package A well tested and comprehensive Golang statistics library / package / module with no dependencies. If you have any s

Montana Flynn 2k Jul 14, 2021
Go Humans! (formatters for units to human friendly sizes)

Humane Units Just a few functions for helping humanize times and sizes. go get it as, import it as "

Dustin Sallings 2.7k Jul 26, 2021

Tideland Go Library Description The Tideland Go Library contains a larger set of useful Google Go packages for different purposes. ATTENTION: The cell

Tideland 194 Feb 1, 2021
Go bindings for fftw3

Go bindings for FFTW v3.2.2 Maintained by Jonathan Wills: [email protected] Feel free to email me patches, suggestions or bugs. FFTW homepage: htt

null 38 Nov 22, 2020
Parses the Graphviz DOT language in golang

Parses the Graphviz DOT language and creates an interface, in golang, with which to easily create new and manipulate existing graphs which can be writ

Walter Schulze 439 Jul 19, 2021