evaler

package module
v2.2.0+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 27, 2018 License: BSD-3-Clause Imports: 7 Imported by: 7 modules, 7 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

evaler

Build Status Coverage GoDoc https://github.com/soniah/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.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:

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.

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

Author

Sonia Hamilton sonia@snowfrog.net

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

License

Modified BSD License (BSD-3)

[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://www.mathsisfun.com/operation-order-bodmas.html

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BigratToBigint

func BigratToBigint(bigrat *big.Rat) *big.Int

BigratToInt converts a *big.Rat to a *big.Int (with truncation)

func BigratToFloat

func BigratToFloat(bigrat *big.Rat) float64

BigratToFloat converts a *big.Rat to a float64 (with loss of precision).

func BigratToInt

func BigratToInt(bigrat *big.Rat) (int64, error)

BigratToInt converts a *big.Rat to an int64 (with truncation); it returns an error for integer overflows.

func Eval

func Eval(expr string) (result *big.Rat, err error)

Eval takes an infix string arithmetic expression, and evaluates it

Usage:

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

Returns: the result of the evaluation, and any errors

func EvalWithVariables

func EvalWithVariables(expr string, variables map[string]string) (result *big.Rat, err error)

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

func FloatToBigrat

func FloatToBigrat(float float64) *big.Rat

FloatToBigrat converts a float64 to a *big.Rat.

func Tokenise

func Tokenise(expr string) []string

Tokenise takes an expr string and converts it to a slice of tokens

Tokenise puts spaces around all non-numbers, removes leading and trailing spaces, then splits on spaces

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL