evaler

package
v0.0.0-...-5fede1b Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2021 License: ISC, BSD-3-Clause Imports: 7 Imported by: 0

README

evaler

Package evaler implements a simple fp arithmetic expression evaluator.

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

This is release 2.0. The previous version that returned results as float64 is in the branch float64.

Usage

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

Operators

The operators supported are:

+ - * / ** () < >

< (less than) and > (greater than) will get lowest precedence, all other precedence is as expected (BODMAS [4]).

< 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

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.

Documentation

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

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

Author

Sonia Hamilton

http://www.snowfrog.net

sonia@snowfrog.net

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 EvaluatePostfix

func EvaluatePostfix(postfix []string) (*big.Rat, error)

evaluatePostfix takes a postfix expression and evaluates it

func FloatToBigrat

func FloatToBigrat(float float64) *big.Rat

FloatToBigrat converts a float64 to a *big.Rat.

func GetPostfix

func GetPostfix(expr string) []string

func IsCellAddr

func IsCellAddr(token string) bool

isCellAddr returns true if token is a cell address

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