FunctionsTable of ContentsbcParserCPP
Math Parser for Go

Math Parser for Go

bcParserGo is a math parser library written in Go. It can be used to build applications for Linux, Mac OS X, Windows.

bcParserGo can parse and evaluate mathematical formulas specified as strings at runtime. It supports variables and functions as well as literals and basic math operators. You can define your own custom variables and define your own functions. Variables can be defined before parsing, or they can be auto discovered in the expression and a callback you provide will decide the value of the newly discovered variable, or throw error if needed.

The help reference for bcParserGo is here .

Here is an example of Go command line program using the bcParserGo math parser library:

package main

import (
        "bestcode/mathparser"
        "flag"
        "fmt"
        "os"
        "strconv"
)

// Returns values for previously undefined variables. In this example we don't rely on it
// for values; but we simply set it to turn off strict parsing to allow undefined variables
// at parse time. Then we ask the parser what those vars are, and then we explicitly set values
// for them. That is why this function never gets called.
func cmdLineVariableResolver(p mathparser.MathParser, varName string) float64 {
        panic(fmt.Sprintf("VariableResolver should never get called in this case, "+
                "but it got called for variable name %s", varName))
}

// This program asks for a mathematical expression to be entered on the command line.
func main() {
        p := mathparser.NewMathParser()
        p.CreateDefaultFuncs()
        flag.Usage = func() {
        fmt.Println("Default functions you can use:")
        for _, funcName := range p.Functions() {
                fmt.Println(funcName)
            }
        }
        flag.Parse(); // Help -h

        var expression string
        for true {
                fmt.Println("Input an expression such as x+sin(y) and press ENTER.")
                fmt.Scan(&expression)
                fmt.Println()

                p.SetExpression(expression)
                // We set variable resolver just to allow undefined variables
                // in this case, so that Parse can succeed. We will set variable
                // values later.
                p.SetVariableResolver(cmdLineVariableResolver)

                err := p.Parse()
                if err == nil {
                        break
                }
        }

        vars, err := p.VariablesUsed()
        if err != nil {
                // Should never happen
                fmt.Println("Error: ", err.Error())
                os.Exit(1)
        }

        for _, variable := range vars {
                var str string
        AGAIN:
                fmt.Println("Input ", variable, " value and press ENTER.")
                fmt.Scan(&str)
                val, err := strconv.ParseFloat(str, 64)
                if err != nil {
                        fmt.Println(str, "is not a valid float64")
                        goto AGAIN
                }
                p.SetVariable(variable, val)
        }

        fmt.Println("-------------------------------------------------------")
        val, err := p.Value()
        if err != nil {
                // Should never happen
                fmt.Println("Error: ", err.Error())
                os.Exit(1)
        }
        fmt.Println("Result of ", p.Expression(), " is: ", val)

        fmt.Println("-------------------------------------------------------")
        fmt.Println("Done.")

        fmt.Println("Type exit and press ENTER to exit.")
        fmt.Scan(&expression)
}
 

 

Math Parser Golang Features

  • Easy to use, simple Go API.
  • Function interface to implement user defined functions.
  • VariableResolver callback interface to provide values for undefined variables.
  • Function/variable names start with letters and can contain letters, numbers and underscore (_).
  • Functions with 0 or more known number of parameters in the form of: f(x,y,z)
  • Functions with unknown number of parameters, e.g. SUM(a,b,c,)
  • Comes with predefined math functions.
  • Arithmetic Operators: +, -, /, *, ^(power), %(mod)
  • Boolean Operators: <, >, =, &, |, ! ,<>, >=, <=
  • Optimization: Constant expression elimination for repeated tasks
  • Paranthesis: ( )
  • Provides localization support which is a map[string]string of message-keys to messages you can set.
  • Optional locale specific decimal separator support (dot or comma).
  • Comes as source code.
  • Royalty free distribution.

Math Parser for Go Library License

Licensing is per developer. Once you purchase the bcParserGo math parser library for Golang, you can ship the binaries royalty free with your applications. The source code is provided so that you can change to fit your needs. bcParserGo license can be viewed here .

Purchasing bcParserGo Math Parser Library

You can pay with credit card and download bcParserGo immediately from our online store for only $29.95. Full version includes Go source code. Upgrades are free for registered users. Licensing is per developer. Site license option is available in the online store page. You can deploy the the component royalty free with your applications as many times as you want. Site license allows any number of developers use the component at your development site. Site License is $290.95 Site licenses can be purchased here.
Online Order Form

For technical questions please contact support@bestcode.com

Math Parser Online Store

Math parser is also available for other languages:

webmaster@bestcode.com